1mod array;
6mod key;
7mod map;
8mod pretty;
9mod value;
10
11use crate::visit_mut::VisitMut as _;
12#[allow(clippy::wildcard_imports)]
13use array::*;
14#[allow(clippy::wildcard_imports)]
15use map::*;
16
17pub use value::ValueSerializer;
18
19#[cfg(feature = "display")]
25pub fn to_vec<T>(value: &T) -> Result<Vec<u8>, Error>
26where
27 T: serde::ser::Serialize + ?Sized,
28{
29 to_string(value).map(|e| e.into_bytes())
30}
31
32#[cfg(feature = "display")]
69pub fn to_string<T>(value: &T) -> Result<String, Error>
70where
71 T: serde::ser::Serialize + ?Sized,
72{
73 to_document(value).map(|e| e.to_string())
74}
75
76#[cfg(feature = "display")]
81pub fn to_string_pretty<T>(value: &T) -> Result<String, Error>
82where
83 T: serde::ser::Serialize + ?Sized,
84{
85 let mut document = to_document(value)?;
86 pretty::Pretty::new().visit_document_mut(&mut document);
87 Ok(document.to_string())
88}
89
90pub fn to_document<T>(value: &T) -> Result<crate::DocumentMut, Error>
94where
95 T: serde::ser::Serialize + ?Sized,
96{
97 let value = value.serialize(ValueSerializer::new())?;
98 let item = crate::Item::Value(value);
99 let root = item
100 .into_table()
101 .map_err(|_| Error::UnsupportedType(None))?;
102 Ok(root.into())
103}
104
105#[derive(Debug, Clone, PartialEq, Eq)]
107#[non_exhaustive]
108pub enum Error {
109 UnsupportedType(Option<&'static str>),
111 OutOfRange(Option<&'static str>),
113 UnsupportedNone,
115 KeyNotString,
117 DateInvalid,
119 Custom(String),
121}
122
123impl Error {
124 pub(crate) fn custom<T>(msg: T) -> Self
125 where
126 T: std::fmt::Display,
127 {
128 Error::Custom(msg.to_string())
129 }
130
131 pub(crate) fn unsupported_type(t: Option<&'static str>) -> Self {
132 Error::UnsupportedType(t)
133 }
134
135 fn out_of_range(t: Option<&'static str>) -> Self {
136 Error::OutOfRange(t)
137 }
138
139 pub(crate) fn unsupported_none() -> Self {
140 Error::UnsupportedNone
141 }
142
143 pub(crate) fn key_not_string() -> Self {
144 Error::KeyNotString
145 }
146
147 fn date_invalid() -> Self {
148 Error::DateInvalid
149 }
150}
151
152impl serde::ser::Error for Error {
153 fn custom<T>(msg: T) -> Self
154 where
155 T: std::fmt::Display,
156 {
157 Self::custom(msg)
158 }
159}
160
161impl std::fmt::Display for Error {
162 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
163 match self {
164 Self::UnsupportedType(Some(t)) => write!(formatter, "unsupported {t} type"),
165 Self::UnsupportedType(None) => write!(formatter, "unsupported rust type"),
166 Self::OutOfRange(Some(t)) => write!(formatter, "out-of-range value for {t} type"),
167 Self::OutOfRange(None) => write!(formatter, "out-of-range value"),
168 Self::UnsupportedNone => "unsupported None value".fmt(formatter),
169 Self::KeyNotString => "map key was not a string".fmt(formatter),
170 Self::DateInvalid => "a serialized date was invalid".fmt(formatter),
171 Self::Custom(s) => s.fmt(formatter),
172 }
173 }
174}
175
176impl From<crate::TomlError> for Error {
177 fn from(e: crate::TomlError) -> Error {
178 Self::custom(e)
179 }
180}
181
182impl From<Error> for crate::TomlError {
183 fn from(e: Error) -> crate::TomlError {
184 Self::custom(e.to_string(), None)
185 }
186}
187
188impl std::error::Error for Error {}