Skip to main content

sciforge_parser/toml/
writer.rs

1pub fn push_toml_escaped(out: &mut String, s: &str) {
2    for c in s.chars() {
3        match c {
4            '"' => out.push_str("\\\""),
5            '\\' => out.push_str("\\\\"),
6            '\n' => out.push_str("\\n"),
7            '\r' => out.push_str("\\r"),
8            '\t' => out.push_str("\\t"),
9            c if (c as u32) < 0x20 => {
10                out.push_str(&format!("\\u{:04X}", c as u32));
11            }
12            _ => out.push(c),
13        }
14    }
15}
16
17pub fn push_toml_str(out: &mut String, key: &str, val: &str) {
18    out.push_str(key);
19    out.push_str(" = \"");
20    push_toml_escaped(out, val);
21    out.push_str("\"\n");
22}
23
24pub fn push_toml_num(out: &mut String, key: &str, val: &str) {
25    out.push_str(key);
26    out.push_str(" = ");
27    out.push_str(val);
28    out.push('\n');
29}
30
31pub fn push_toml_section(out: &mut String, name: &str) {
32    out.push('[');
33    out.push_str(name);
34    out.push_str("]\n");
35}
36
37pub fn push_toml_array_section(out: &mut String, name: &str) {
38    out.push_str("[[");
39    out.push_str(name);
40    out.push_str("]]\n");
41}