Skip to content

Commit

Permalink
Fix case for structs
Browse files Browse the repository at this point in the history
  • Loading branch information
lennartkloock committed Apr 15, 2023
1 parent 8da4880 commit a4b925f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ proc-macro2 = "1.0.56"
quote = "1.0.26"
syn = "2.0.15"

serde = "1.0.160"
serde_yaml = "0.9.21"
convert_case = "0.6.0"

unic-langid = "0.9.1"
15 changes: 6 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
fs::DirEntry,
path::{Path, PathBuf},
};
use convert_case::{Case, Casing};
use syn::{parse_macro_input, LitStr};
use unic_langid::LanguageIdentifier;

Expand Down Expand Up @@ -102,7 +103,7 @@ fn gen_struct(name: Option<&Ident>, mapping: &serde_yaml::Mapping) -> proc_macro
Value::Mapping(m) => {
let key = key.as_str().expect("key was not a string");
let key_ident = format_ident!("{}", key);
let struct_name = format_ident!("{}", capitalize(key));
let struct_name = format_ident!("{}", struct_name(key));
structs.push(gen_struct(Some(&struct_name), m));
keys.push(quote!(
#key_ident: #struct_name
Expand All @@ -122,12 +123,8 @@ fn gen_struct(name: Option<&Ident>, mapping: &serde_yaml::Mapping) -> proc_macro
)
}

fn capitalize(s: &str) -> String {
let mut c = s.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
}
fn struct_name(s: &str) -> String {
s.to_case(Case::Pascal)
}

fn gen_fn(
Expand Down Expand Up @@ -175,7 +172,7 @@ fn gen_construct(
}
Value::Mapping(m) => {
let construct = gen_construct(
&format_ident!("{}", capitalize(key)),
&format_ident!("{}", struct_name(key)),
None,
m,
value.as_mapping().expect("failed to get value as mapping"),
Expand All @@ -195,7 +192,7 @@ fn gen_construct(
}
Value::Mapping(m) => {
let construct =
gen_construct(&format_ident!("{}", capitalize(key)), None, m, m);
gen_construct(&format_ident!("{}", struct_name(key)), None, m, m);
values.push(quote!(
#key_ident: #construct
));
Expand Down

0 comments on commit a4b925f

Please sign in to comment.