Debian Codegraph @

debian/rust-vendor/toml_edit/src/ser/mod.rs

raw
1 //! Serializing Rust structures into TOML.
2 //!
3 //! This module contains all the Serde support for serializing Rust structures into TOML.
4
5 mod array;
6 mod error;
7 mod key;
8 mod map;
9 mod pretty;
10 mod value;
11
12 use crate::visit_mut::VisitMut as _;
13 #[allow(clippy::wildcard_imports)]
14 use array::*;
15 #[allow(clippy::wildcard_imports)]
16 use map::*;
17
18 pub use error::Error;
19 pub use value::ValueSerializer;
20
21 /// Serialize the given data structure as a TOML byte vector.
22 ///
23 /// Serialization can fail if `T`'s implementation of `Serialize` decides to
24 /// fail, if `T` contains a map with non-string keys, or if `T` attempts to
25 /// serialize an unsupported datatype such as an enum, tuple, or tuple struct.
26 #[cfg(feature = "display")]
27 pub fn to_vec<T>(value: &T) -> Result<Vec<u8>, Error>
28 where
29 T: serde_core::ser::Serialize + ?Sized,
30 {
31 to_string(value).map(|e| e.into_bytes())
32 }
33
34 /// Serialize the given data structure as a String of TOML.
35 ///
36 /// Serialization can fail if `T`'s implementation of `Serialize` decides to
37 /// fail, if `T` contains a map with non-string keys, or if `T` attempts to
38 /// serialize an unsupported datatype such as an enum, tuple, or tuple struct.
39 ///
40 /// # Examples
41 ///
42 /// ```
43 /// use serde::Serialize;
44 ///
45 /// #[derive(Serialize)]
46 /// struct Config {
47 /// database: Database,
48 /// }
49 ///
50 /// #[derive(Serialize)]
51 /// struct Database {
52 /// ip: String,
53 /// port: Vec<u16>,
54 /// connection_max: u32,
55 /// enabled: bool,
56 /// }
57 ///
58 /// let config = Config {
59 /// database: Database {
60 /// ip: "192.168.1.1".to_string(),
61 /// port: vec![8001, 8002, 8003],
62 /// connection_max: 5000,
63 /// enabled: false,
64 /// },
65 /// };
66 ///
67 /// let toml = toml_edit::ser::to_string(&config).unwrap();
68 /// println!("{}", toml)
69 /// ```
70 #[cfg(feature = "display")]
71 pub fn to_string<T>(value: &T) -> Result<String, Error>
72 where
73 T: serde_core::ser::Serialize + ?Sized,
74 {
75 to_document(value).map(|e| e.to_string())
76 }
77
78 /// Serialize the given data structure as a "pretty" String of TOML.
79 ///
80 /// This is identical to `to_string` except the output string has a more
81 /// "pretty" output. See `ValueSerializer::pretty` for more details.
82 #[cfg(feature = "display")]
83 pub fn to_string_pretty<T>(value: &T) -> Result<String, Error>
84 where
85 T: serde_core::ser::Serialize + ?Sized,
86 {
87 let mut document = to_document(value)?;
88 pretty::Pretty::new().visit_document_mut(&mut document);
89 Ok(document.to_string())
90 }
91
92 /// Serialize the given data structure into a TOML document.
93 ///
94 /// This would allow custom formatting to be applied, mixing with format preserving edits, etc.
95 pub fn to_document<T>(value: &T) -> Result<crate::DocumentMut, Error>
96 where
97 T: serde_core::ser::Serialize + ?Sized,
98 {
99 let value = value.serialize(ValueSerializer::new())?;
100 let item = crate::Item::Value(value);
101 let root = item
102 .into_table()
103 .map_err(|_| Error::UnsupportedType(None))?;
104 Ok(root.into())
105 }