Skip to content

Commit

Permalink
Put nesting field example to the last position (#22)
Browse files Browse the repository at this point in the history
* let nesting field example after non-nesting field example

* bounce version
  • Loading branch information
yanganto authored Feb 19, 2024
1 parent f52ee52 commit 5176b24
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 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.1"
version = "0.10.2"
edition = "2021"
authors = ["Antonio Yang <yanganto@gmail.com>"]
license = "MIT"
Expand Down
21 changes: 13 additions & 8 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,11 @@ pub fn derive_patch(item: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(item as syn::DeriveInput);
let struct_name = &input.ident;
let mut struct_doc = "r#\"".to_string();

// Nesting field example will be append after the non nesting field example to avoid #18
let mut field_example = "r#\"".to_string();
let mut nesting_field_example = "".to_string();

push_doc_string(&mut struct_doc, parse_attrs(&input.attrs).0);

let fields = if let syn::Data::Struct(syn::DataStruct { fields, .. }) = &input.data {
Expand All @@ -287,29 +291,29 @@ pub fn derive_patch(item: TokenStream) -> TokenStream {
.unwrap_or_default()
{
if let Some(field_type) = field_type {
field_example.push_str("\"#.to_string()");
nesting_field_example.push_str("\"#.to_string()");
let key = default_key(default);
match nesting_format {
Some(NestingFormat::Section(NestingType::Vec)) if optional => field_example.push_str(&format!(
Some(NestingFormat::Section(NestingType::Vec)) if optional => nesting_field_example.push_str(&format!(
" + &{field_type}::toml_field_example(\"# [[{field_name:}]]\n\", \"# \")"
)),
Some(NestingFormat::Section(NestingType::Vec)) => field_example.push_str(&format!(
Some(NestingFormat::Section(NestingType::Vec)) => nesting_field_example.push_str(&format!(
" + &{field_type}::toml_field_example(\"[[{field_name:}]]\n\", \"\")"
)),
Some(NestingFormat::Section(NestingType::Dict)) if optional => field_example.push_str(&format!(
Some(NestingFormat::Section(NestingType::Dict)) if optional => nesting_field_example.push_str(&format!(
" + &{field_type}::toml_field_example(\"# [{field_name:}.{key}]\n\", \"# \")"
)),
Some(NestingFormat::Section(NestingType::Dict)) => field_example.push_str(&format!(
Some(NestingFormat::Section(NestingType::Dict)) => nesting_field_example.push_str(&format!(
" + &{field_type}::toml_field_example(\"[{field_name:}.{key}]\n\", \"\")"
)),
_ if optional => field_example.push_str(&format!(
_ if optional => nesting_field_example.push_str(&format!(
" + &{field_type}::toml_field_example(\"# [{field_name:}]\n\", \"# \")"
)),
_ => field_example.push_str(&format!(
_ => nesting_field_example.push_str(&format!(
" + &{field_type}::toml_field_example(\"[{field_name:}]\n\", \"\")"
))
};
field_example.push_str(" + &r#\"");
nesting_field_example.push_str(" + &r#\"");
} else {
abort!(&f.ident, "nesting only work on inner structure")
}
Expand Down Expand Up @@ -369,6 +373,7 @@ pub fn derive_patch(item: TokenStream) -> TokenStream {
}
}
struct_doc.push_str("\"#.to_string()");
field_example += &nesting_field_example;
field_example.push_str("\"#.to_string()");

let struct_doc_stream: proc_macro2::TokenStream =
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.1", path = "../derive" }
toml-example-derive = { version = "=0.10.2", path = "../derive" }

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

#[test]
fn non_nesting_field_should_be_first() {
#[derive(TomlExample)]
#[allow(dead_code)]
struct Foo {
a: String,
}

#[derive(TomlExample)]
#[allow(dead_code)]
struct Bar {
#[toml_example(nesting)]
foo: Foo,
b: String,
}

assert_eq!(
Bar::toml_example(),
r#"b = ""
[foo]
a = ""
"#
);
}
Expand Down

0 comments on commit 5176b24

Please sign in to comment.