Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 68 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Config::to_toml_example("example.toml"); // write example to a file
let example = Config::toml_example();
```

Toml example base on the doc string of each field
Toml example base on the docstring of each field
```toml
# Config is to arrange something or change the controls on a computer or other device
# so that it can be used in a particular way
Expand Down Expand Up @@ -135,8 +135,7 @@ Please add `#[toml_example(nesting)]`, or `#[toml_example(nesting = prefix)]` on
#[allow(dead_code)]
struct Node {
/// Services are running in the node
#[toml_example(nesting)]
#[toml_example(default = http)]
#[toml_example(default = http, nesting)]
services: HashMap<String, Service>,
}
```
Expand All @@ -149,43 +148,52 @@ Please add `#[toml_example(nesting)]`, or `#[toml_example(nesting = prefix)]` on
port = 80

```
If you want an optional field become a required field in example,
place the `#[toml_example(require)]` on the field.
If you want to skip some field you can use `#[toml_example(skip)]`,
the `#[serde(skip)]`, `#[serde(skip_deserializing)]` also works.

## Flattening
Flattening means treating the fields of a nested struct as if they were defined directly in the wrapping struct.
```rust
use toml_example::TomlExample;
#[derive(TomlExample)]
struct Config {
/// Config.a is an optional number
#[toml_example(require)]
a: Option<usize>,
/// Config.b is an optional string
#[toml_example(require)]
b: Option<String>,
#[toml_example(require)]
#[toml_example(default = "third")]
c: Option<String>,
#[toml_example(skip)]
d: usize,
struct ItemWrapper {
#[toml_example(flatten, nesting)]
item: Item,
}
#[derive(TomlExample)]
struct Item {
value: String,
}

assert_eq!(ItemWrapper::toml_example(), Item::toml_example());
```
```toml
# Config.a is an optional number
a = 0

# Config.b is an optional string
b = ""
This works with maps too!

c = "third"
```rust
#[derive(TomlExample, Deserialize)]
struct MainConfig {
#[serde(flatten)]
#[toml_example(nesting)]
nested: HashMap<String, ConfigItem>,
}
#[derive(TomlExample, Deserialize)]
struct ConfigItem {
#[toml_example(default = false)]
enabled: bool,
}

let example = MainConfig::toml_example();
assert!(toml::from_str::<MainConfig>(&example).is_ok());
println!("{example}");
```
```toml
[example]
enabled = false
```

## Enum Field
You can also use fieldless enums, but you have to annotate them with `#[toml_example(enum)]` or
`#[toml_example(is_enum)]` if you mind the keyword highlight you likely get when writing "enum".
When annotating a field with `#[toml_example(default)]` it will use the [Debug](core::fmt::Debug) implementation.
However for non-TOML datatypes like enums, this does not work as the value needs to be treated as a string in TOML.
However for non-TOML data types like enums, this does not work as the value needs to be treated as a string in TOML.
The `#[toml_example(enum)]` attribute just adds the needed quotes around the [Debug](core::fmt::Debug) implementation
and can be omitted if a custom [Debug](core::fmt::Debug) already includes those.

Expand All @@ -194,8 +202,7 @@ use toml_example::TomlExample;
#[derive(TomlExample)]
struct Config {
/// Config.priority is an enum
#[toml_example(default)]
#[toml_example(enum)]
#[toml_example(enum, default)]
priority: Priority,
}
#[derive(Debug, Default)]
Expand All @@ -211,6 +218,38 @@ priority = "Important"
"#)
```

## More
If you want an optional field become a required field in example,
place the `#[toml_example(require)]` on the field.
If you want to skip some field you can use `#[toml_example(skip)]`,
the `#[serde(skip)]`, `#[serde(skip_deserializing)]` also works.
```rust
use toml_example::TomlExample;
#[derive(TomlExample)]
struct Config {
/// Config.a is an optional number
#[toml_example(require)]
a: Option<usize>,
/// Config.b is an optional string
#[toml_example(require)]
b: Option<String>,
#[toml_example(require, default = "third")]
c: Option<String>,
#[toml_example(skip)]
d: usize,
}
```
```toml
# Config.a is an optional number
a = 0

# Config.b is an optional string
b = ""

c = "third"

```

[crates-badge]: https://img.shields.io/crates/v/toml-example.svg
[crate-url]: https://crates.io/crates/toml-example
[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg
Expand Down
Loading