toml-comment
Derive macro that turns /// doc comments and Default values into commented TOML.
Why
The toml crate can't write comments. If your app ships a default config, you end up maintaining a hand-written TOML string that duplicates your Default impl, and the two inevitably drift apart.
toml_edit preserves comments but it's a round-trip parser for modifying existing files, not generating new ones. toml-scaffold can generate configs but depends on schemars and JsonSchema, which is a heavy dependency tree for what's a pretty simple problem.
The goal of this crate is to stay as small as possible. It reads your doc comments and defaults and produces commented TOML. Compile-time deps are syn/quote, runtime deps are toml + serde, and that's all there will ever be.
Usage
[]
= "0.1"
= { = "1", = ["derive"] }
use Serialize;
use TomlComment;
write.unwrap;
Output:
# Port to listen on
= 8080
# Bind address
= "127.0.0.1"
Nested structs become [section] headers automatically. to_commented_toml() serializes non-default values.
Supported types
- Primitives (
bool, integers, floats,usize,isize) StringOption<T>-- omitted whenNoneVec<T>-- inline arrays- Nested structs -- become
[section]tables, must also deriveTomlComment #[toml_comment(inline)]forces a struct field to serialize as an inline value
How it works
The derive macro extracts /// doc comments (rustc stores these as #[doc = "..."] attributes), classifies each field as a leaf or nested struct, and generates a _render method that serializes fields one by one through toml::Value::try_from.
The trait requires Serialize + Default. default_toml() calls Self::default().to_commented_toml().