Toml-scaffold: Generate Commented TOML Config Files from Rust Structs

I'm excited to share toml-scaffold v0.1, a library that generates TOML configuration scaffold files directly from your Rust structs while preserving doc comments.

What It Does

Turn this:

#[derive(Serialize, JsonSchema, TomlScaffold)]
struct Config {
    /// Server host address
    host: String,
    /// Server port
    port: u16,
}

let config = Config {
    host: "localhost".to_string(),
    port: 8080,
};

Into this:

# Server host address
host = "localhost"
# Server port
port = 8080

Key Features

  • Doc comments become TOML comments - Your documentation flows directly into
    config files
  • Field order preserved - Output matches your struct definition
  • Comprehensive type support - Primitives, Option, HashMap, Vec, nested
    structs, and serde_json::Value

Use Cases

  • Generate example configuration files for your CLI tools
  • Create documented config templates for users
  • Bootstrap configuration with sensible defaults

Check it out on crates.io and GitHub.

2 Likes

Congrats! I know many people are interested in such a library.

Another common feature people want but I didn't see is control over what TOML syntax is used for rendering.

Also, I'd recommend toml_writer - Rust for handling strings and keys. It is what is used by toml.

1 Like

Thanks for the suggestion! Just released v0.2 to adopt the toml_writer and fix those escape issues.

The output currently follows opinionated formatting rules to ensure good initial results. I'm open to implementing this if there's more demand for it :slight_smile: