Skip to content

Conversation

@faervan
Copy link
Contributor

@faervan faervan commented Sep 8, 2025

use std::collections::HashMap;

use serde::Serialize;
use toml_example::TomlExample;

fn main() {
    let example = ConfigWrapper::toml_example();
    println!("{example}");
}

#[derive(TomlExample, Serialize, PartialEq)]
struct ConfigWrapper {
    #[serde(flatten, default)]
    #[toml_example(nesting)]
    config: Config,

    #[serde(flatten)]
    #[toml_example(nesting, default = hello)]
    map: HashMap<String, NestedNum>,

    #[toml_example(default = ["hello", "{nice :)\""], require)]
    list: Option<Vec<String>>,

    #[toml_example(default = (9999, "super (fancy\\! :-) )"), require)]
    tuple: Option<(usize, String)>,
}

#[derive(TomlExample, Serialize, Default, PartialEq)]
struct Config {
    #[toml_example(default = "abc, de")]
    number: String,
}

#[derive(TomlExample, Serialize, Default, PartialEq)]
struct NestedNum {
    value: usize,
}

The output:

list = ["hello", "{nice :)\""]

tuple = (9999, "super (fancy\\! :-) )")

number = "abc, de"

[hello]
value = 0

Notice the extra line between tuple and number: Should this be or would removing this be the better choice?


close #48

To-do:

  • Add tests
  • Update crate docs
  • Update README

The tokens in `#[serde(tokens)]` are split by `,` and handled
individually in order to allow attributes like
`#[serde(default, flatten)]`
as in `#[toml_example(default = "\"hello!\", he shouted", require)]`
or `#[serde(flatten, default)]`
@faervan faervan marked this pull request as ready for review September 9, 2025 17:34
@faervan
Copy link
Contributor Author

faervan commented Sep 9, 2025

@yanganto This PR should be ready for review now

Or it should not, just detected a non-working case that needs fixing tomorrow

@faervan faervan marked this pull request as draft September 9, 2025 20:46
@faervan
Copy link
Contributor Author

faervan commented Sep 9, 2025

Or rather, recursive nesting does not work regardless of the flattening which I added, so that should be a separate issue probably

@faervan faervan marked this pull request as ready for review September 9, 2025 21:22
Copy link
Owner

@yanganto yanganto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thank you. ❤️

@yanganto
Copy link
Owner

Or rather, recursive nesting does not work regardless of the flattening which I added, so that should be a separate issue probably

Yeh, I think deep nesting and flatten are not the feature people using at the same time now. We can keep this in a separate issue.

@yanganto yanganto merged commit 1666506 into yanganto:main Sep 10, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Field name should be skipped for flattened items

2 participants