#document #config-toml #toml #comments #serialization #derive

macro toml-input-derive

# A library to generate toml text with clear options and comments

4 releases

Uses new Rust 2024

new 0.1.4 May 20, 2025
0.1.2 May 3, 2025
0.1.1 Apr 30, 2025
0.1.0 Apr 30, 2025

#28 in #config-toml

Download history 302/week @ 2025-04-28 65/week @ 2025-05-05 28/week @ 2025-05-12

395 downloads per month
Used in 2 crates (via toml-input)

MIT license

21KB
451 lines

A library to generate toml text with clear options and comments

Example 1: toml text from definition (all examples see tests)

rust code:

use serde::{Deserialize, Serialize};
use strum_macros::{AsRefStr, EnumIter};
use toml_input::TomlInput;

/// comment `Test`
#[derive(Debug, TomlInput, Serialize, Deserialize, Default)]
struct Test {
    /// comment `a`
    a: i32,
    /// comment `b`
    b: TestEnum,
}
/// comment `TestEnum`
#[allow(dead_code)]
#[derive(Debug, EnumIter, AsRefStr, TomlInput, Serialize, Deserialize)]
#[derive(Default)]
enum TestEnum {
    /// comment `A`
    A,
    /// comment `B`
    #[default]
    B,
}

let text = Test::schema_to_string().unwrap();

toml text:

# comment `Test`

# comment `a`
a = 0
# comment `A`
#!b = "A"
# comment `B`
b = "B"

Example 2: toml text from value

rust code:

/// comment `Test`
#[derive(Debug, Clone, TomlInput, Serialize, Deserialize, Default, PartialEq)]
struct Test {
    /// comment `a`
    a: i32,
    /// comment `b`
    #[toml_input(enum_style = "fold")]
    b: TestEnum,
}
/// comment `TestEnum`
#[derive(Debug, Clone, EnumIter, AsRefStr, TomlInput, Serialize, Deserialize, PartialEq)]
#[allow(dead_code)]
#[derive(Default)]
enum TestEnum {
    /// comment `A`
    A,
    /// comment `B`
    #[default]
    B,
}

let test = Test {
    a: 0,
    b: TestEnum::B,
};
let text = test.into_string().unwrap();

toml text:

# comment `Test`

# comment `a`
a = 0
# b = "A" | "B"
# comment `B`
b = "B"

Example 3: enum tuple

rust code:

/// comment `Test`
#[derive(Debug, Clone, TomlInput, Serialize, Deserialize, PartialEq, Default)]
struct Test {
    /// comment `a`
    a: i32,
    /// comment `b`
    b: TestEnum,
}
/// comment `TestEnum`
#[derive(Debug, Clone, EnumIter, AsRefStr, TomlInput, Serialize, Deserialize, PartialEq)]
#[allow(dead_code)]
enum TestEnum {
    /// comment `A`
    A,
    /// comment `B`
    B(String),
}
impl Default for TestEnum {
    fn default() -> Self {
        TestEnum::B(String::new())
    }
}
let test = Test {
    a: 0,
    b: TestEnum::B("test B".to_string()),
};
let text = test.into_string().unwrap();

toml text:

# comment `Test`

# comment `a`
a = 0
# comment `A`
#!b = "A"
# comment `B`
b = { B = "test B" }

Dependencies

~3–4.5MB
~80K SLoC