A lib help generate toml example
This crate provides the TomlExample
trait and an accompanying derive macro.
Deriving TomlExample
on a struct will provide to_example
function help generate toml example file base documentation
- support #[serde(default)]
, #[serde(default = "function_name")]
attributes (serde
feature, opt-in)
- provide #[toml_example(default)]
, #[toml_example(default = 0)]
, #[toml_example(default = "default_string")]
attributes
- The order matter of attribute macro, if #[serde(default = ..]
and #[toml_example(default = ..)]
existing at the same time with different value
```rust use toml_example::TomlExample;
/// 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
struct Config {
/// Config.a should be a number
a: usize,
/// Config.b should be a string
b: String,
/// Optional Config.c is a number
c: Option
Config::totomlexample("example.toml"); // write example to a file let example = Config::toml_example(); ```
Toml example base on the doc string of each field ```toml
a = 0
b = ""
e = 7
f = "seven"
g = 7
h = "seven"
```
A nesting struct wrap with Option<T>
, Vec<T>
, HashMap<String, T>
, BTreeMap<String, T>
are handled.
Please add #[toml_example(nesting)]
, or #[toml_example(nesting = prefix)]
on the field.
#[toml_example(nesting)]
rust
/// Service with specific port
#[derive(TomlExample)]
struct Service {
/// port should be a number
#[toml_example(default = 80)]
port: usize,
}
#[derive(TomlExample)]
#[allow(dead_code)]
struct Node {
/// Services are running in the node
#[toml_example(nesting)]
#[toml_example(default = http)]
services: HashMap<String, Service>,
}
Node::toml_example()
will be following string.
```toml
[services.http]
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.
rust
use toml_example::TomlExample;
struct Config {
/// Config.a is an optional number
#[tomlexample(require)]
a: Option
toml
a = 0
b = ""
c = "third"
```