Skip to main content

toml_writer/
write.rs

1pub trait TomlWrite: core::fmt::Write {
2    fn open_table_header(&mut self) -> core::fmt::Result {
3        self.write_str("[")
4    }
5    fn close_table_header(&mut self) -> core::fmt::Result {
6        self.write_str("]")
7    }
8
9    fn open_array_of_tables_header(&mut self) -> core::fmt::Result {
10        self.write_str("[[")
11    }
12    fn close_array_of_tables_header(&mut self) -> core::fmt::Result {
13        self.write_str("]]")
14    }
15
16    fn open_inline_table(&mut self) -> core::fmt::Result {
17        self.write_str("{")
18    }
19    fn close_inline_table(&mut self) -> core::fmt::Result {
20        self.write_str("}")
21    }
22
23    fn open_array(&mut self) -> core::fmt::Result {
24        self.write_str("[")
25    }
26    fn close_array(&mut self) -> core::fmt::Result {
27        self.write_str("]")
28    }
29
30    fn key_sep(&mut self) -> core::fmt::Result {
31        self.write_str(".")
32    }
33
34    fn keyval_sep(&mut self) -> core::fmt::Result {
35        self.write_str("=")
36    }
37
38    /// Write an encoded TOML key
39    ///
40    /// To customize the encoding, see [`TomlStringBuilder`][crate::TomlStringBuilder].
41    fn key(&mut self, value: impl crate::WriteTomlKey) -> core::fmt::Result {
42        value.write_toml_key(self)
43    }
44
45    /// Write an encoded TOML scalar value
46    ///
47    /// To customize the encoding, see
48    /// - [`TomlStringBuilder`][crate::TomlStringBuilder]
49    /// - [`TomlIntegerFormat`][crate::TomlIntegerFormat]
50    ///
51    /// <div class="warning">
52    ///
53    /// For floats, this preserves the sign bit for [`f32::NAN`] / [`f64::NAN`] for the sake of
54    /// format-preserving editing.
55    /// However, in most cases the sign bit is indeterminate and outputting signed NANs can be a
56    /// cause of non-repeatable behavior.
57    ///
58    /// For general serialization, you should discard the sign bit.  For example:
59    /// ```
60    /// # let mut v = f64::NAN;
61    /// if v.is_nan() {
62    ///     v = v.copysign(1.0);
63    /// }
64    /// ```
65    ///
66    /// </div>
67    fn value(&mut self, value: impl crate::WriteTomlValue) -> core::fmt::Result {
68        value.write_toml_value(self)
69    }
70
71    fn val_sep(&mut self) -> core::fmt::Result {
72        self.write_str(",")
73    }
74
75    fn space(&mut self) -> core::fmt::Result {
76        self.write_str(" ")
77    }
78
79    fn open_comment(&mut self) -> core::fmt::Result {
80        self.write_str("#")
81    }
82
83    fn newline(&mut self) -> core::fmt::Result {
84        self.write_str("\n")
85    }
86}
87
88impl<W> TomlWrite for W where W: core::fmt::Write {}