#toml-serialization #struct #pretty #tabs #serde-serializable

toml_pretty

a function to pretty serialize a serde-serializable value to toml

11 releases (stable)

1.2.0 Aug 14, 2025
1.1.2 Jun 11, 2024
0.3.0 Jun 11, 2024
0.2.0 Jun 11, 2024
0.1.0 Jun 10, 2024

#786 in Encoding

Download history 40/week @ 2025-06-04 37/week @ 2025-06-11 16/week @ 2025-06-18 105/week @ 2025-06-25 8/week @ 2025-07-02 50/week @ 2025-07-09 22/week @ 2025-07-16 54/week @ 2025-07-23 37/week @ 2025-07-30 75/week @ 2025-08-06 217/week @ 2025-08-13 104/week @ 2025-08-20 46/week @ 2025-08-27 50/week @ 2025-09-03 34/week @ 2025-09-10 67/week @ 2025-09-17

213 downloads per month

MIT license

11KB
240 lines

toml_pretty

A function to pretty serialize a serde-serializable value to toml.

Can serialize structs to toml in a single block (unlike the toml crate, which is great for deserialization but not so great for pretty serialization)

Nested array fields more than 2 arrays deep are not supported.

Note. All items in arrays are on a new line and indented. toml_pretty::to_string uses \t by default as tab. An alternal tab symbol can be used (eg. 2 spaces) using the Options (shown in example).

Example

Given serializable structs:

#[derive(Serialize)]
struct User {
  name: String,
  nicknames: Vec<String>,
  birthday: Birthday,
  more: Vec<Birthday>,
}

#[derive(Serialize)]
struct Birthday {
  day: u8,
  month: u8,
  year: u16,
}

Can use toml_pretty::to_string:

let user = User {
	name: String::from("Jonathan"),
	nicknames: vec![String::from("Jack"), String::from("Jon")],
	birthday: Birthday {
		day: 0,
		month: 0,
		year: 1980,
	},
	more: vec![
		Birthday {
			day: 0,
			month: 0,
			year: 1980,
		},
		Birthday {
			day: 0,
			month: 0,
			year: 1980,
		},
	],
};
println!(
	"{}",
	toml_pretty::to_string(&user, toml_pretty::Options::default().tab("  "))
		.context("failed to serialize user pretty")
		.unwrap()
);

Dependencies

~2.2–3MB
~56K SLoC