Skip to content

Commit

Permalink
support renaming field (#23)
Browse files Browse the repository at this point in the history
* support `rename`

* bounce version
  • Loading branch information
yanganto committed Feb 19, 2024
1 parent 5176b24 commit 54de9c7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ members = [
]

[workspace.package]
version = "0.10.2"
version = "0.10.3"
edition = "2021"
authors = ["Antonio Yang <yanganto@gmail.com>"]
license = "MIT"
Expand Down
25 changes: 20 additions & 5 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fn parse_type(
r#type
}

/// return (doc, default, nesting, require, skip)
/// return (doc, default, nesting, require, skip, rename)
fn parse_attrs(
attrs: &[Attribute],
) -> (
Expand All @@ -118,12 +118,15 @@ fn parse_attrs(
Option<NestingFormat>,
bool,
bool,
Option<String>,
) {
let mut docs = Vec::new();
let mut default_source = None;
let mut nesting_format = None;
let mut require = false;
let mut skip = false;
let mut rename = None;

for attr in attrs.iter() {
match (attr.style, &attr.meta) {
(Outer, NameValue(MetaNameValue { path, value, .. })) => {
Expand Down Expand Up @@ -166,6 +169,11 @@ fn parse_attrs(
if token_str == "skip_deserializing" || token_str == "skip" {
skip = true;
}
if token_str.starts_with("rename") {
if let Some((_, s)) = token_str.split_once('=') {
rename = Some(s.trim().trim_matches('"').into());
}
}
}
}
(Outer, List(MetaList { path, tokens, .. }))
Expand Down Expand Up @@ -203,7 +211,7 @@ fn parse_attrs(
_ => (),
}
}
(docs, default_source, nesting_format, require, skip)
(docs, default_source, nesting_format, require, skip, rename)
}

fn parse_field(
Expand All @@ -214,10 +222,12 @@ fn parse_field(
bool,
Option<NestingFormat>,
bool,
Option<String>,
) {
let mut default_value = String::new();
let mut optional = false;
let (docs, default_source, mut nesting_format, require, skip) = parse_attrs(&field.attrs);
let (docs, default_source, mut nesting_format, require, skip, rename) =
parse_attrs(&field.attrs);
let ty = parse_type(
&field.ty,
&mut default_value,
Expand All @@ -236,6 +246,7 @@ fn parse_field(
optional && !require,
nesting_format,
skip,
rename,
)
}

Expand Down Expand Up @@ -278,11 +289,14 @@ pub fn derive_patch(item: TokenStream) -> TokenStream {
if let Named(fields_named) = fields {
for f in fields_named.named.iter() {
let field_type = parse_type(&f.ty, &mut String::new(), &mut false, &mut None);
if let Some(field_name) = f.ident.as_ref().map(|i| i.to_string()) {
let (default, doc_str, optional, nesting_format, skip) = parse_field(f);
if let Some(mut field_name) = f.ident.as_ref().map(|i| i.to_string()) {
let (default, doc_str, optional, nesting_format, skip, rename) = parse_field(f);
if skip {
continue;
}
if let Some(rename) = rename {
field_name = rename;
}
push_doc_string(&mut field_example, doc_str);

if nesting_format
Expand Down Expand Up @@ -340,6 +354,7 @@ pub fn derive_patch(item: TokenStream) -> TokenStream {
match default {
DefaultSource::DefaultValue(default) => {
field_example.push_str("\"#.to_string() + prefix + &r#\"");
// TODO rename here
field_example.push_str(field_name.trim_start_matches("r#"));
field_example.push_str(" = ");
field_example.push_str(&default);
Expand Down
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license.workspace = true
readme.workspace = true

[dependencies]
toml-example-derive = { version = "=0.10.2", path = "../derive" }
toml-example-derive = { version = "=0.10.3", path = "../derive" }

[dev-dependencies]
serde = { version = "1.0", features = ["derive"] }
Expand Down
17 changes: 17 additions & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,23 @@ type = 0
[foo]
a = ""
"#
);
}

#[test]
fn rename() {
use serde::Serialize;

#[derive(Deserialize, Serialize, TomlExample)]
struct Config {
#[serde(rename = "bb")]
b: usize,
}
assert_eq!(
Config::toml_example(),
r#"bb = 0
"#
);
}
Expand Down

0 comments on commit 54de9c7

Please sign in to comment.