Skip to main content

toml_comment/
lib.rs

1pub use toml_comment_derive::TomlComment;
2
3pub trait TomlComment: serde::Serialize + Default {
4    fn default_toml() -> String;
5    fn to_commented_toml(&self) -> String;
6
7    #[doc(hidden)]
8    fn _render(&self, out: &mut String, prefix: &str);
9}
10
11pub fn fmt_value(val: &toml::Value) -> String {
12    match val {
13        toml::Value::String(s) => format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\"")),
14        toml::Value::Integer(i) => i.to_string(),
15        toml::Value::Float(f) => {
16            let s = f.to_string();
17            if s.contains('.') { s } else { format!("{s}.0") }
18        }
19        toml::Value::Boolean(b) => b.to_string(),
20        toml::Value::Array(arr) => {
21            format!(
22                "[{}]",
23                arr.iter().map(fmt_value).collect::<Vec<_>>().join(", ")
24            )
25        }
26        toml::Value::Table(t) => {
27            let pairs = t.iter().map(|(k, v)| format!("{k} = {}", fmt_value(v)));
28            format!("{{ {} }}", pairs.collect::<Vec<_>>().join(", "))
29        }
30        toml::Value::Datetime(dt) => dt.to_string(),
31    }
32}