/rust/registry/src/index.crates.io-1949cf8c6b5b557f/toml-1.1.2+spec-1.1.0/src/value.rs
Line | Count | Source |
1 | | //! Definition of a TOML [value][Value] |
2 | | |
3 | | use alloc::collections::BTreeMap; |
4 | | use alloc::vec; |
5 | | use core::fmt; |
6 | | use core::hash::Hash; |
7 | | use core::mem::discriminant; |
8 | | use core::ops; |
9 | | #[cfg(feature = "std")] |
10 | | use std::collections::HashMap; |
11 | | |
12 | | use serde_core::de; |
13 | | use serde_core::de::IntoDeserializer; |
14 | | use serde_core::ser; |
15 | | |
16 | | use crate::alloc_prelude::*; |
17 | | |
18 | | pub use toml_datetime::{Date, Datetime, DatetimeParseError, Offset, Time}; |
19 | | |
20 | | /// Type representing a TOML array, payload of the `Value::Array` variant |
21 | | pub type Array = Vec<Value>; |
22 | | |
23 | | #[doc(no_inline)] |
24 | | pub use crate::Table; |
25 | | |
26 | | /// Representation of a TOML value. |
27 | | #[derive(PartialEq, Clone, Debug)] |
28 | | pub enum Value { |
29 | | /// Represents a TOML string |
30 | | String(String), |
31 | | /// Represents a TOML integer |
32 | | Integer(i64), |
33 | | /// Represents a TOML float |
34 | | Float(f64), |
35 | | /// Represents a TOML boolean |
36 | | Boolean(bool), |
37 | | /// Represents a TOML datetime |
38 | | Datetime(Datetime), |
39 | | /// Represents a TOML array |
40 | | Array(Array), |
41 | | /// Represents a TOML table |
42 | | Table(Table), |
43 | | } |
44 | | |
45 | | impl Value { |
46 | | /// Convert a `T` into `toml::Value` which is an enum that can represent |
47 | | /// any valid TOML data. |
48 | | /// |
49 | | /// This conversion can fail if `T`'s implementation of `Serialize` decides to |
50 | | /// fail, or if `T` contains a map with non-string keys. |
51 | 0 | pub fn try_from<T>(value: T) -> Result<Self, crate::ser::Error> |
52 | 0 | where |
53 | 0 | T: ser::Serialize, |
54 | | { |
55 | 0 | value.serialize(ValueSerializer) |
56 | 0 | } |
57 | | |
58 | | /// Interpret a `toml::Value` as an instance of type `T`. |
59 | | /// |
60 | | /// This conversion can fail if the structure of the `Value` does not match the |
61 | | /// structure expected by `T`, for example if `T` is a struct type but the |
62 | | /// `Value` contains something other than a TOML table. It can also fail if the |
63 | | /// structure is correct but `T`'s implementation of `Deserialize` decides that |
64 | | /// something is wrong with the data, for example required struct fields are |
65 | | /// missing from the TOML map or some number is too big to fit in the expected |
66 | | /// primitive type. |
67 | 0 | pub fn try_into<'de, T>(self) -> Result<T, crate::de::Error> |
68 | 0 | where |
69 | 0 | T: de::Deserialize<'de>, |
70 | | { |
71 | 0 | de::Deserialize::deserialize(self) |
72 | 0 | } |
73 | | |
74 | | /// Index into a TOML array or map. A string index can be used to access a |
75 | | /// value in a map, and a usize index can be used to access an element of an |
76 | | /// array. |
77 | | /// |
78 | | /// Returns `None` if the type of `self` does not match the type of the |
79 | | /// index, for example if the index is a string and `self` is an array or a |
80 | | /// number. Also returns `None` if the given key does not exist in the map |
81 | | /// or the given index is not within the bounds of the array. |
82 | 0 | pub fn get<I: Index>(&self, index: I) -> Option<&Self> { |
83 | 0 | index.index(self) |
84 | 0 | } Unexecuted instantiation: <toml::value::Value>::get::<_> Unexecuted instantiation: <toml::value::Value>::get::<&str> |
85 | | |
86 | | /// Mutably index into a TOML array or map. A string index can be used to |
87 | | /// access a value in a map, and a usize index can be used to access an |
88 | | /// element of an array. |
89 | | /// |
90 | | /// Returns `None` if the type of `self` does not match the type of the |
91 | | /// index, for example if the index is a string and `self` is an array or a |
92 | | /// number. Also returns `None` if the given key does not exist in the map |
93 | | /// or the given index is not within the bounds of the array. |
94 | 0 | pub fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Self> { |
95 | 0 | index.index_mut(self) |
96 | 0 | } |
97 | | |
98 | | /// Extracts the integer value if it is an integer. |
99 | 0 | pub fn as_integer(&self) -> Option<i64> { |
100 | 0 | match *self { |
101 | 0 | Self::Integer(i) => Some(i), |
102 | 0 | _ => None, |
103 | | } |
104 | 0 | } |
105 | | |
106 | | /// Tests whether this value is an integer. |
107 | 0 | pub fn is_integer(&self) -> bool { |
108 | 0 | self.as_integer().is_some() |
109 | 0 | } |
110 | | |
111 | | /// Extracts the float value if it is a float. |
112 | 0 | pub fn as_float(&self) -> Option<f64> { |
113 | 0 | match *self { |
114 | 0 | Self::Float(f) => Some(f), |
115 | 0 | _ => None, |
116 | | } |
117 | 0 | } |
118 | | |
119 | | /// Tests whether this value is a float. |
120 | 0 | pub fn is_float(&self) -> bool { |
121 | 0 | self.as_float().is_some() |
122 | 0 | } |
123 | | |
124 | | /// Extracts the boolean value if it is a boolean. |
125 | 0 | pub fn as_bool(&self) -> Option<bool> { |
126 | 0 | match *self { |
127 | 0 | Self::Boolean(b) => Some(b), |
128 | 0 | _ => None, |
129 | | } |
130 | 0 | } |
131 | | |
132 | | /// Tests whether this value is a boolean. |
133 | 0 | pub fn is_bool(&self) -> bool { |
134 | 0 | self.as_bool().is_some() |
135 | 0 | } |
136 | | |
137 | | /// Extracts the string of this value if it is a string. |
138 | 0 | pub fn as_str(&self) -> Option<&str> { |
139 | 0 | match *self { |
140 | 0 | Self::String(ref s) => Some(&**s), |
141 | 0 | _ => None, |
142 | | } |
143 | 0 | } |
144 | | |
145 | | /// Tests if this value is a string. |
146 | 0 | pub fn is_str(&self) -> bool { |
147 | 0 | self.as_str().is_some() |
148 | 0 | } |
149 | | |
150 | | /// Extracts the datetime value if it is a datetime. |
151 | | /// |
152 | | /// Note that a parsed TOML value will only contain ISO 8601 dates. An |
153 | | /// example date is: |
154 | | /// |
155 | | /// ```notrust |
156 | | /// 1979-05-27T07:32:00Z |
157 | | /// ``` |
158 | 0 | pub fn as_datetime(&self) -> Option<&Datetime> { |
159 | 0 | match *self { |
160 | 0 | Self::Datetime(ref s) => Some(s), |
161 | 0 | _ => None, |
162 | | } |
163 | 0 | } |
164 | | |
165 | | /// Tests whether this value is a datetime. |
166 | 0 | pub fn is_datetime(&self) -> bool { |
167 | 0 | self.as_datetime().is_some() |
168 | 0 | } |
169 | | |
170 | | /// Extracts the array value if it is an array. |
171 | 0 | pub fn as_array(&self) -> Option<&Vec<Self>> { |
172 | 0 | match *self { |
173 | 0 | Self::Array(ref s) => Some(s), |
174 | 0 | _ => None, |
175 | | } |
176 | 0 | } |
177 | | |
178 | | /// Extracts the array value if it is an array. |
179 | 0 | pub fn as_array_mut(&mut self) -> Option<&mut Vec<Self>> { |
180 | 0 | match *self { |
181 | 0 | Self::Array(ref mut s) => Some(s), |
182 | 0 | _ => None, |
183 | | } |
184 | 0 | } |
185 | | |
186 | | /// Tests whether this value is an array. |
187 | 0 | pub fn is_array(&self) -> bool { |
188 | 0 | self.as_array().is_some() |
189 | 0 | } |
190 | | |
191 | | /// Extracts the table value if it is a table. |
192 | 0 | pub fn as_table(&self) -> Option<&Table> { |
193 | 0 | match *self { |
194 | 0 | Self::Table(ref s) => Some(s), |
195 | 0 | _ => None, |
196 | | } |
197 | 0 | } |
198 | | |
199 | | /// Extracts the table value if it is a table. |
200 | 0 | pub fn as_table_mut(&mut self) -> Option<&mut Table> { |
201 | 0 | match *self { |
202 | 0 | Self::Table(ref mut s) => Some(s), |
203 | 0 | _ => None, |
204 | | } |
205 | 0 | } |
206 | | |
207 | | /// Tests whether this value is a table. |
208 | 0 | pub fn is_table(&self) -> bool { |
209 | 0 | self.as_table().is_some() |
210 | 0 | } |
211 | | |
212 | | /// Tests whether this and another value have the same type. |
213 | 0 | pub fn same_type(&self, other: &Self) -> bool { |
214 | 0 | discriminant(self) == discriminant(other) |
215 | 0 | } |
216 | | |
217 | | /// Returns a human-readable representation of the type of this value. |
218 | 0 | pub fn type_str(&self) -> &'static str { |
219 | 0 | match *self { |
220 | 0 | Self::String(..) => "string", |
221 | 0 | Self::Integer(..) => "integer", |
222 | 0 | Self::Float(..) => "float", |
223 | 0 | Self::Boolean(..) => "boolean", |
224 | 0 | Self::Datetime(..) => "datetime", |
225 | 0 | Self::Array(..) => "array", |
226 | 0 | Self::Table(..) => "table", |
227 | | } |
228 | 0 | } |
229 | | } |
230 | | |
231 | | impl<I> ops::Index<I> for Value |
232 | | where |
233 | | I: Index, |
234 | | { |
235 | | type Output = Self; |
236 | | |
237 | 0 | fn index(&self, index: I) -> &Self { |
238 | 0 | self.get(index).expect("index not found") |
239 | 0 | } |
240 | | } |
241 | | |
242 | | impl<I> ops::IndexMut<I> for Value |
243 | | where |
244 | | I: Index, |
245 | | { |
246 | 0 | fn index_mut(&mut self, index: I) -> &mut Self { |
247 | 0 | self.get_mut(index).expect("index not found") |
248 | 0 | } |
249 | | } |
250 | | |
251 | | impl<'a> From<&'a str> for Value { |
252 | | #[inline] |
253 | 0 | fn from(val: &'a str) -> Self { |
254 | 0 | Self::String(val.to_owned()) |
255 | 0 | } |
256 | | } |
257 | | |
258 | | impl<V: Into<Self>> From<Vec<V>> for Value { |
259 | 0 | fn from(val: Vec<V>) -> Self { |
260 | 0 | Self::Array(val.into_iter().map(|v| v.into()).collect()) |
261 | 0 | } |
262 | | } |
263 | | |
264 | | impl<S: Into<String>, V: Into<Self>> From<BTreeMap<S, V>> for Value { |
265 | 0 | fn from(val: BTreeMap<S, V>) -> Self { |
266 | 0 | let table = val.into_iter().map(|(s, v)| (s.into(), v.into())).collect(); |
267 | | |
268 | 0 | Self::Table(table) |
269 | 0 | } |
270 | | } |
271 | | |
272 | | #[cfg(feature = "std")] |
273 | | impl<S: Into<String> + Hash + Eq, V: Into<Self>> From<HashMap<S, V>> for Value { |
274 | 0 | fn from(val: HashMap<S, V>) -> Self { |
275 | 0 | let table = val.into_iter().map(|(s, v)| (s.into(), v.into())).collect(); |
276 | | |
277 | 0 | Self::Table(table) |
278 | 0 | } |
279 | | } |
280 | | |
281 | | macro_rules! impl_into_value { |
282 | | ($variant:ident : $T:ty) => { |
283 | | impl From<$T> for Value { |
284 | | #[inline] |
285 | 0 | fn from(val: $T) -> Value { |
286 | 0 | Value::$variant(val.into()) |
287 | 0 | } Unexecuted instantiation: <toml::value::Value as core::convert::From<alloc::string::String>>::from Unexecuted instantiation: <toml::value::Value as core::convert::From<i64>>::from Unexecuted instantiation: <toml::value::Value as core::convert::From<i32>>::from Unexecuted instantiation: <toml::value::Value as core::convert::From<i8>>::from Unexecuted instantiation: <toml::value::Value as core::convert::From<u8>>::from Unexecuted instantiation: <toml::value::Value as core::convert::From<u32>>::from Unexecuted instantiation: <toml::value::Value as core::convert::From<f64>>::from Unexecuted instantiation: <toml::value::Value as core::convert::From<f32>>::from Unexecuted instantiation: <toml::value::Value as core::convert::From<bool>>::from Unexecuted instantiation: <toml::value::Value as core::convert::From<toml_datetime::datetime::Datetime>>::from Unexecuted instantiation: <toml::value::Value as core::convert::From<toml::map::Map<alloc::string::String, toml::value::Value>>>::from |
288 | | } |
289 | | }; |
290 | | } |
291 | | |
292 | | impl_into_value!(String: String); |
293 | | impl_into_value!(Integer: i64); |
294 | | impl_into_value!(Integer: i32); |
295 | | impl_into_value!(Integer: i8); |
296 | | impl_into_value!(Integer: u8); |
297 | | impl_into_value!(Integer: u32); |
298 | | impl_into_value!(Float: f64); |
299 | | impl_into_value!(Float: f32); |
300 | | impl_into_value!(Boolean: bool); |
301 | | impl_into_value!(Datetime: Datetime); |
302 | | impl_into_value!(Table: Table); |
303 | | |
304 | | /// Types that can be used to index a `toml::Value` |
305 | | /// |
306 | | /// Currently this is implemented for `usize` to index arrays and `str` to index |
307 | | /// tables. |
308 | | /// |
309 | | /// This trait is sealed and not intended for implementation outside of the |
310 | | /// `toml` crate. |
311 | | pub trait Index: Sealed { |
312 | | #[doc(hidden)] |
313 | | fn index<'a>(&self, val: &'a Value) -> Option<&'a Value>; |
314 | | #[doc(hidden)] |
315 | | fn index_mut<'a>(&self, val: &'a mut Value) -> Option<&'a mut Value>; |
316 | | } |
317 | | |
318 | | /// An implementation detail that should not be implemented, this will change in |
319 | | /// the future and break code otherwise. |
320 | | #[doc(hidden)] |
321 | | pub trait Sealed {} |
322 | | impl Sealed for usize {} |
323 | | impl Sealed for str {} |
324 | | impl Sealed for String {} |
325 | | impl<T: Sealed + ?Sized> Sealed for &T {} |
326 | | |
327 | | impl Index for usize { |
328 | 0 | fn index<'a>(&self, val: &'a Value) -> Option<&'a Value> { |
329 | 0 | match *val { |
330 | 0 | Value::Array(ref a) => a.get(*self), |
331 | 0 | _ => None, |
332 | | } |
333 | 0 | } |
334 | | |
335 | 0 | fn index_mut<'a>(&self, val: &'a mut Value) -> Option<&'a mut Value> { |
336 | 0 | match *val { |
337 | 0 | Value::Array(ref mut a) => a.get_mut(*self), |
338 | 0 | _ => None, |
339 | | } |
340 | 0 | } |
341 | | } |
342 | | |
343 | | impl Index for str { |
344 | 0 | fn index<'a>(&self, val: &'a Value) -> Option<&'a Value> { |
345 | 0 | match *val { |
346 | 0 | Value::Table(ref a) => a.get(self), |
347 | 0 | _ => None, |
348 | | } |
349 | 0 | } |
350 | | |
351 | 0 | fn index_mut<'a>(&self, val: &'a mut Value) -> Option<&'a mut Value> { |
352 | 0 | match *val { |
353 | 0 | Value::Table(ref mut a) => a.get_mut(self), |
354 | 0 | _ => None, |
355 | | } |
356 | 0 | } |
357 | | } |
358 | | |
359 | | impl Index for String { |
360 | 0 | fn index<'a>(&self, val: &'a Value) -> Option<&'a Value> { |
361 | 0 | self[..].index(val) |
362 | 0 | } |
363 | | |
364 | 0 | fn index_mut<'a>(&self, val: &'a mut Value) -> Option<&'a mut Value> { |
365 | 0 | self[..].index_mut(val) |
366 | 0 | } |
367 | | } |
368 | | |
369 | | impl<T> Index for &T |
370 | | where |
371 | | T: Index + ?Sized, |
372 | | { |
373 | 0 | fn index<'a>(&self, val: &'a Value) -> Option<&'a Value> { |
374 | 0 | (**self).index(val) |
375 | 0 | } Unexecuted instantiation: <&_ as toml::value::Index>::index Unexecuted instantiation: <&str as toml::value::Index>::index |
376 | | |
377 | 0 | fn index_mut<'a>(&self, val: &'a mut Value) -> Option<&'a mut Value> { |
378 | 0 | (**self).index_mut(val) |
379 | 0 | } |
380 | | } |
381 | | |
382 | | #[cfg(feature = "display")] |
383 | | impl fmt::Display for Value { |
384 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
385 | | use serde_core::Serialize as _; |
386 | | |
387 | 0 | let mut output = String::new(); |
388 | 0 | let serializer = crate::ser::ValueSerializer::new(&mut output); |
389 | 0 | self.serialize(serializer).unwrap(); |
390 | 0 | output.fmt(f) |
391 | 0 | } |
392 | | } |
393 | | |
394 | | #[cfg(feature = "parse")] |
395 | | impl core::str::FromStr for Value { |
396 | | type Err = crate::de::Error; |
397 | 0 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
398 | | use serde_core::Deserialize as _; |
399 | 0 | Self::deserialize(crate::de::ValueDeserializer::parse(s)?) |
400 | 0 | } |
401 | | } |
402 | | |
403 | | impl ser::Serialize for Value { |
404 | 0 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
405 | 0 | where |
406 | 0 | S: ser::Serializer, |
407 | | { |
408 | 0 | match *self { |
409 | 0 | Self::String(ref s) => serializer.serialize_str(s), |
410 | 0 | Self::Integer(i) => serializer.serialize_i64(i), |
411 | 0 | Self::Float(f) => serializer.serialize_f64(f), |
412 | 0 | Self::Boolean(b) => serializer.serialize_bool(b), |
413 | 0 | Self::Datetime(ref s) => s.serialize(serializer), |
414 | 0 | Self::Array(ref a) => a.serialize(serializer), |
415 | 0 | Self::Table(ref t) => t.serialize(serializer), |
416 | | } |
417 | 0 | } Unexecuted instantiation: <toml::value::Value as serde_core::ser::Serialize>::serialize::<serde_json::value::ser::Serializer> Unexecuted instantiation: <toml::value::Value as serde_core::ser::Serialize>::serialize::<toml::ser::value::ValueSerializer> Unexecuted instantiation: <toml::value::Value as serde_core::ser::Serialize>::serialize::<toml::ser::document::Serializer> Unexecuted instantiation: <toml::value::Value as serde_core::ser::Serialize>::serialize::<toml::ser::value::map::MapValueSerializer> Unexecuted instantiation: <toml::value::Value as serde_core::ser::Serialize>::serialize::<toml::ser::document::array_of_tables::ArrayOfTablesSerializer> Unexecuted instantiation: <toml::value::Value as serde_core::ser::Serialize>::serialize::<toml::ser::document::strategy::WalkValue> |
418 | | } |
419 | | |
420 | | impl<'de> de::Deserialize<'de> for Value { |
421 | 0 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
422 | 0 | where |
423 | 0 | D: de::Deserializer<'de>, |
424 | | { |
425 | | struct ValueVisitor; |
426 | | |
427 | | impl<'de> de::Visitor<'de> for ValueVisitor { |
428 | | type Value = Value; |
429 | | |
430 | 0 | fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
431 | 0 | formatter.write_str("any valid TOML value") |
432 | 0 | } |
433 | | |
434 | 0 | fn visit_bool<E>(self, value: bool) -> Result<Value, E> { |
435 | 0 | Ok(Value::Boolean(value)) |
436 | 0 | } |
437 | | |
438 | 0 | fn visit_i64<E>(self, value: i64) -> Result<Value, E> { |
439 | 0 | Ok(Value::Integer(value)) |
440 | 0 | } |
441 | | |
442 | 0 | fn visit_u64<E: de::Error>(self, value: u64) -> Result<Value, E> { |
443 | 0 | if i64::try_from(value).is_ok() { |
444 | 0 | Ok(Value::Integer(value as i64)) |
445 | | } else { |
446 | 0 | Err(de::Error::custom("u64 value was too large")) |
447 | | } |
448 | 0 | } |
449 | | |
450 | 0 | fn visit_u32<E>(self, value: u32) -> Result<Value, E> { |
451 | 0 | Ok(Value::Integer(value.into())) |
452 | 0 | } |
453 | | |
454 | 0 | fn visit_i32<E>(self, value: i32) -> Result<Value, E> { |
455 | 0 | Ok(Value::Integer(value.into())) |
456 | 0 | } |
457 | | |
458 | 0 | fn visit_f64<E>(self, value: f64) -> Result<Value, E> { |
459 | 0 | Ok(Value::Float(value)) |
460 | 0 | } |
461 | | |
462 | 0 | fn visit_str<E>(self, value: &str) -> Result<Value, E> { |
463 | 0 | Ok(Value::String(value.into())) |
464 | 0 | } |
465 | | |
466 | 0 | fn visit_string<E>(self, value: String) -> Result<Value, E> { |
467 | 0 | Ok(Value::String(value)) |
468 | 0 | } |
469 | | |
470 | 0 | fn visit_some<D>(self, deserializer: D) -> Result<Value, D::Error> |
471 | 0 | where |
472 | 0 | D: de::Deserializer<'de>, |
473 | | { |
474 | 0 | de::Deserialize::deserialize(deserializer) |
475 | 0 | } |
476 | | |
477 | 0 | fn visit_seq<V>(self, mut visitor: V) -> Result<Value, V::Error> |
478 | 0 | where |
479 | 0 | V: de::SeqAccess<'de>, |
480 | | { |
481 | 0 | let mut vec = Vec::new(); |
482 | 0 | while let Some(elem) = visitor.next_element()? { |
483 | 0 | vec.push(elem); |
484 | 0 | } |
485 | 0 | Ok(Value::Array(vec)) |
486 | 0 | } |
487 | | |
488 | 0 | fn visit_map<V>(self, mut visitor: V) -> Result<Value, V::Error> |
489 | 0 | where |
490 | 0 | V: de::MapAccess<'de>, |
491 | | { |
492 | 0 | let key = match toml_datetime::de::VisitMap::next_key_seed(&mut visitor)? { |
493 | 0 | Some(toml_datetime::de::VisitMap::Datetime(datetime)) => { |
494 | 0 | return Ok(Value::Datetime(datetime)); |
495 | | } |
496 | 0 | None => return Ok(Value::Table(Table::new())), |
497 | 0 | Some(toml_datetime::de::VisitMap::Key(key)) => key, |
498 | | }; |
499 | 0 | let mut map = Table::new(); |
500 | 0 | map.insert(key.into_owned(), visitor.next_value()?); |
501 | 0 | while let Some(key) = visitor.next_key::<String>()? { |
502 | 0 | if let crate::map::Entry::Vacant(vacant) = map.entry(&key) { |
503 | 0 | vacant.insert(visitor.next_value()?); |
504 | | } else { |
505 | 0 | let msg = format!("duplicate key: `{key}`"); |
506 | 0 | return Err(de::Error::custom(msg)); |
507 | | } |
508 | | } |
509 | 0 | Ok(Value::Table(map)) |
510 | 0 | } Unexecuted instantiation: <<toml::value::Value as serde_core::de::Deserialize>::deserialize::ValueVisitor as serde_core::de::Visitor>::visit_map::<toml_datetime::de::DatetimeDeserializer<toml::de::error::Error>> Unexecuted instantiation: <<toml::value::Value as serde_core::de::Deserialize>::deserialize::ValueVisitor as serde_core::de::Visitor>::visit_map::<toml::de::deserializer::table::TableMapAccess> |
511 | | } |
512 | | |
513 | 0 | deserializer.deserialize_any(ValueVisitor) |
514 | 0 | } Unexecuted instantiation: <toml::value::Value as serde_core::de::Deserialize>::deserialize::<toml::de::deserializer::Deserializer> Unexecuted instantiation: <toml::value::Value as serde_core::de::Deserialize>::deserialize::<serde_core::de::value::StringDeserializer<toml::de::error::Error>> Unexecuted instantiation: <toml::value::Value as serde_core::de::Deserialize>::deserialize::<toml::de::deserializer::value::ValueDeserializer> |
515 | | } |
516 | | |
517 | | // This is wrapped by `Table` and any trait methods implemented here need to be wrapped there. |
518 | | impl<'de> de::Deserializer<'de> for Value { |
519 | | type Error = crate::de::Error; |
520 | | |
521 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, crate::de::Error> |
522 | 0 | where |
523 | 0 | V: de::Visitor<'de>, |
524 | | { |
525 | 0 | match self { |
526 | 0 | Self::Boolean(v) => visitor.visit_bool(v), |
527 | 0 | Self::Integer(n) => visitor.visit_i64(n), |
528 | 0 | Self::Float(n) => visitor.visit_f64(n), |
529 | 0 | Self::String(v) => visitor.visit_string(v), |
530 | 0 | Self::Datetime(v) => visitor.visit_string(v.to_string()), |
531 | 0 | Self::Array(v) => { |
532 | 0 | let len = v.len(); |
533 | 0 | let mut deserializer = SeqDeserializer::new(v); |
534 | 0 | let seq = visitor.visit_seq(&mut deserializer)?; |
535 | 0 | let remaining = deserializer.iter.len(); |
536 | 0 | if remaining == 0 { |
537 | 0 | Ok(seq) |
538 | | } else { |
539 | 0 | Err(de::Error::invalid_length(len, &"fewer elements in array")) |
540 | | } |
541 | | } |
542 | 0 | Self::Table(v) => { |
543 | 0 | let len = v.len(); |
544 | 0 | let mut deserializer = MapDeserializer::new(v); |
545 | 0 | let map = visitor.visit_map(&mut deserializer)?; |
546 | 0 | let remaining = deserializer.iter.len(); |
547 | 0 | if remaining == 0 { |
548 | 0 | Ok(map) |
549 | | } else { |
550 | 0 | Err(de::Error::invalid_length(len, &"fewer elements in map")) |
551 | | } |
552 | | } |
553 | | } |
554 | 0 | } |
555 | | |
556 | | #[inline] |
557 | 0 | fn deserialize_enum<V>( |
558 | 0 | self, |
559 | 0 | _name: &'static str, |
560 | 0 | _variants: &'static [&'static str], |
561 | 0 | visitor: V, |
562 | 0 | ) -> Result<V::Value, crate::de::Error> |
563 | 0 | where |
564 | 0 | V: de::Visitor<'de>, |
565 | | { |
566 | 0 | match self { |
567 | 0 | Self::String(variant) => visitor.visit_enum(variant.into_deserializer()), |
568 | 0 | Self::Table(variant) => { |
569 | 0 | if variant.is_empty() { |
570 | 0 | Err(crate::de::Error::custom( |
571 | 0 | "wanted exactly 1 element, found 0 elements", |
572 | 0 | None, |
573 | 0 | )) |
574 | 0 | } else if variant.len() != 1 { |
575 | 0 | Err(crate::de::Error::custom( |
576 | 0 | "wanted exactly 1 element, more than 1 element", |
577 | 0 | None, |
578 | 0 | )) |
579 | | } else { |
580 | 0 | let deserializer = MapDeserializer::new(variant); |
581 | 0 | visitor.visit_enum(deserializer) |
582 | | } |
583 | | } |
584 | 0 | _ => Err(de::Error::invalid_type( |
585 | 0 | de::Unexpected::UnitVariant, |
586 | 0 | &"string only", |
587 | 0 | )), |
588 | | } |
589 | 0 | } |
590 | | |
591 | | // `None` is interpreted as a missing field so be sure to implement `Some` |
592 | | // as a present field. |
593 | 0 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, crate::de::Error> |
594 | 0 | where |
595 | 0 | V: de::Visitor<'de>, |
596 | | { |
597 | 0 | visitor.visit_some(self) |
598 | 0 | } |
599 | | |
600 | 0 | fn deserialize_newtype_struct<V>( |
601 | 0 | self, |
602 | 0 | _name: &'static str, |
603 | 0 | visitor: V, |
604 | 0 | ) -> Result<V::Value, crate::de::Error> |
605 | 0 | where |
606 | 0 | V: de::Visitor<'de>, |
607 | | { |
608 | 0 | visitor.visit_newtype_struct(self) |
609 | 0 | } |
610 | | |
611 | | serde_core::forward_to_deserialize_any! { |
612 | | bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string unit seq |
613 | | bytes byte_buf map unit_struct tuple_struct struct |
614 | | tuple ignored_any identifier |
615 | | } |
616 | | } |
617 | | |
618 | | pub(crate) struct SeqDeserializer { |
619 | | iter: vec::IntoIter<Value>, |
620 | | } |
621 | | |
622 | | impl SeqDeserializer { |
623 | 0 | fn new(vec: Vec<Value>) -> Self { |
624 | 0 | Self { |
625 | 0 | iter: vec.into_iter(), |
626 | 0 | } |
627 | 0 | } |
628 | | } |
629 | | |
630 | | impl<'de> de::SeqAccess<'de> for SeqDeserializer { |
631 | | type Error = crate::de::Error; |
632 | | |
633 | 0 | fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, crate::de::Error> |
634 | 0 | where |
635 | 0 | T: de::DeserializeSeed<'de>, |
636 | | { |
637 | 0 | match self.iter.next() { |
638 | 0 | Some(value) => seed.deserialize(value).map(Some), |
639 | 0 | None => Ok(None), |
640 | | } |
641 | 0 | } |
642 | | |
643 | 0 | fn size_hint(&self) -> Option<usize> { |
644 | 0 | match self.iter.size_hint() { |
645 | 0 | (lower, Some(upper)) if lower == upper => Some(upper), |
646 | 0 | _ => None, |
647 | | } |
648 | 0 | } |
649 | | } |
650 | | |
651 | | pub(crate) struct MapDeserializer { |
652 | | iter: <Table as IntoIterator>::IntoIter, |
653 | | value: Option<(String, Value)>, |
654 | | } |
655 | | |
656 | | impl MapDeserializer { |
657 | 0 | fn new(map: Table) -> Self { |
658 | 0 | Self { |
659 | 0 | iter: map.into_iter(), |
660 | 0 | value: None, |
661 | 0 | } |
662 | 0 | } |
663 | | } |
664 | | |
665 | | impl<'de> de::MapAccess<'de> for MapDeserializer { |
666 | | type Error = crate::de::Error; |
667 | | |
668 | 0 | fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, crate::de::Error> |
669 | 0 | where |
670 | 0 | T: de::DeserializeSeed<'de>, |
671 | | { |
672 | 0 | match self.iter.next() { |
673 | 0 | Some((key, value)) => { |
674 | 0 | self.value = Some((key.clone(), value)); |
675 | 0 | seed.deserialize(Value::String(key)).map(Some) |
676 | | } |
677 | 0 | None => Ok(None), |
678 | | } |
679 | 0 | } |
680 | | |
681 | 0 | fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, crate::de::Error> |
682 | 0 | where |
683 | 0 | T: de::DeserializeSeed<'de>, |
684 | | { |
685 | 0 | let (key, res) = match self.value.take() { |
686 | 0 | Some((key, value)) => (key, seed.deserialize(value)), |
687 | 0 | None => return Err(de::Error::custom("value is missing")), |
688 | | }; |
689 | 0 | res.map_err(|mut error| { |
690 | 0 | error.add_key(key); |
691 | 0 | error |
692 | 0 | }) |
693 | 0 | } |
694 | | |
695 | 0 | fn size_hint(&self) -> Option<usize> { |
696 | 0 | match self.iter.size_hint() { |
697 | 0 | (lower, Some(upper)) if lower == upper => Some(upper), |
698 | 0 | _ => None, |
699 | | } |
700 | 0 | } |
701 | | } |
702 | | |
703 | | impl<'de> de::EnumAccess<'de> for MapDeserializer { |
704 | | type Error = crate::de::Error; |
705 | | type Variant = MapEnumDeserializer; |
706 | | |
707 | 0 | fn variant_seed<V>(mut self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> |
708 | 0 | where |
709 | 0 | V: de::DeserializeSeed<'de>, |
710 | | { |
711 | | use de::Error; |
712 | 0 | let (key, value) = match self.iter.next() { |
713 | 0 | Some(pair) => pair, |
714 | | None => { |
715 | 0 | return Err(Error::custom( |
716 | 0 | "expected table with exactly 1 entry, found empty table", |
717 | 0 | )); |
718 | | } |
719 | | }; |
720 | | |
721 | 0 | let val = seed.deserialize(key.into_deserializer())?; |
722 | | |
723 | 0 | let variant = MapEnumDeserializer::new(value); |
724 | | |
725 | 0 | Ok((val, variant)) |
726 | 0 | } |
727 | | } |
728 | | |
729 | | /// Deserializes table values into enum variants. |
730 | | pub(crate) struct MapEnumDeserializer { |
731 | | value: Value, |
732 | | } |
733 | | |
734 | | impl MapEnumDeserializer { |
735 | 0 | pub(crate) fn new(value: Value) -> Self { |
736 | 0 | Self { value } |
737 | 0 | } |
738 | | } |
739 | | |
740 | | impl<'de> de::VariantAccess<'de> for MapEnumDeserializer { |
741 | | type Error = crate::de::Error; |
742 | | |
743 | 0 | fn unit_variant(self) -> Result<(), Self::Error> { |
744 | | use de::Error; |
745 | 0 | match self.value { |
746 | 0 | Value::Array(values) => { |
747 | 0 | if values.is_empty() { |
748 | 0 | Ok(()) |
749 | | } else { |
750 | 0 | Err(Error::custom("expected empty array")) |
751 | | } |
752 | | } |
753 | 0 | Value::Table(values) => { |
754 | 0 | if values.is_empty() { |
755 | 0 | Ok(()) |
756 | | } else { |
757 | 0 | Err(Error::custom("expected empty table")) |
758 | | } |
759 | | } |
760 | 0 | e => Err(Error::custom(format!( |
761 | 0 | "expected table, found {}", |
762 | 0 | e.type_str() |
763 | 0 | ))), |
764 | | } |
765 | 0 | } |
766 | | |
767 | 0 | fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error> |
768 | 0 | where |
769 | 0 | T: de::DeserializeSeed<'de>, |
770 | | { |
771 | 0 | seed.deserialize(self.value.into_deserializer()) |
772 | 0 | } |
773 | | |
774 | 0 | fn tuple_variant<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error> |
775 | 0 | where |
776 | 0 | V: de::Visitor<'de>, |
777 | | { |
778 | | use de::Error; |
779 | 0 | match self.value { |
780 | 0 | Value::Array(values) => { |
781 | 0 | if values.len() == len { |
782 | 0 | de::Deserializer::deserialize_seq(values.into_deserializer(), visitor) |
783 | | } else { |
784 | 0 | Err(Error::custom(format!("expected tuple with length {len}"))) |
785 | | } |
786 | | } |
787 | 0 | Value::Table(values) => { |
788 | 0 | let tuple_values: Result<Vec<_>, _> = values |
789 | 0 | .into_iter() |
790 | 0 | .enumerate() |
791 | 0 | .map(|(index, (key, value))| match key.parse::<usize>() { |
792 | 0 | Ok(key_index) if key_index == index => Ok(value), |
793 | 0 | Ok(_) | Err(_) => Err(Error::custom(format!( |
794 | 0 | "expected table key `{index}`, but was `{key}`" |
795 | 0 | ))), |
796 | 0 | }) |
797 | 0 | .collect(); |
798 | 0 | let tuple_values = tuple_values?; |
799 | | |
800 | 0 | if tuple_values.len() == len { |
801 | 0 | de::Deserializer::deserialize_seq(tuple_values.into_deserializer(), visitor) |
802 | | } else { |
803 | 0 | Err(Error::custom(format!("expected tuple with length {len}"))) |
804 | | } |
805 | | } |
806 | 0 | e => Err(Error::custom(format!( |
807 | 0 | "expected table, found {}", |
808 | 0 | e.type_str() |
809 | 0 | ))), |
810 | | } |
811 | 0 | } |
812 | | |
813 | 0 | fn struct_variant<V>( |
814 | 0 | self, |
815 | 0 | fields: &'static [&'static str], |
816 | 0 | visitor: V, |
817 | 0 | ) -> Result<V::Value, Self::Error> |
818 | 0 | where |
819 | 0 | V: de::Visitor<'de>, |
820 | | { |
821 | 0 | de::Deserializer::deserialize_struct( |
822 | 0 | self.value.into_deserializer(), |
823 | | "", // TODO: this should be the variant name |
824 | 0 | fields, |
825 | 0 | visitor, |
826 | | ) |
827 | 0 | } |
828 | | } |
829 | | |
830 | | impl IntoDeserializer<'_, crate::de::Error> for Value { |
831 | | type Deserializer = Self; |
832 | | |
833 | 0 | fn into_deserializer(self) -> Self { |
834 | 0 | self |
835 | 0 | } |
836 | | } |
837 | | |
838 | | pub(crate) struct ValueSerializer; |
839 | | |
840 | | impl ser::Serializer for ValueSerializer { |
841 | | type Ok = Value; |
842 | | type Error = crate::ser::Error; |
843 | | |
844 | | type SerializeSeq = ValueSerializeVec; |
845 | | type SerializeTuple = ValueSerializeVec; |
846 | | type SerializeTupleStruct = ValueSerializeVec; |
847 | | type SerializeTupleVariant = ValueSerializeTupleVariant; |
848 | | type SerializeMap = ValueSerializeMap; |
849 | | type SerializeStruct = ValueSerializeMap; |
850 | | type SerializeStructVariant = ValueSerializeStructVariant; |
851 | | |
852 | 0 | fn serialize_bool(self, value: bool) -> Result<Value, crate::ser::Error> { |
853 | 0 | Ok(Value::Boolean(value)) |
854 | 0 | } |
855 | | |
856 | 0 | fn serialize_i8(self, value: i8) -> Result<Value, crate::ser::Error> { |
857 | 0 | self.serialize_i64(value.into()) |
858 | 0 | } |
859 | | |
860 | 0 | fn serialize_i16(self, value: i16) -> Result<Value, crate::ser::Error> { |
861 | 0 | self.serialize_i64(value.into()) |
862 | 0 | } |
863 | | |
864 | 0 | fn serialize_i32(self, value: i32) -> Result<Value, crate::ser::Error> { |
865 | 0 | self.serialize_i64(value.into()) |
866 | 0 | } |
867 | | |
868 | 0 | fn serialize_i64(self, value: i64) -> Result<Value, crate::ser::Error> { |
869 | 0 | Ok(Value::Integer(value)) |
870 | 0 | } |
871 | | |
872 | 0 | fn serialize_u8(self, value: u8) -> Result<Value, crate::ser::Error> { |
873 | 0 | self.serialize_i64(value.into()) |
874 | 0 | } |
875 | | |
876 | 0 | fn serialize_u16(self, value: u16) -> Result<Value, crate::ser::Error> { |
877 | 0 | self.serialize_i64(value.into()) |
878 | 0 | } |
879 | | |
880 | 0 | fn serialize_u32(self, value: u32) -> Result<Value, crate::ser::Error> { |
881 | 0 | self.serialize_i64(value.into()) |
882 | 0 | } |
883 | | |
884 | 0 | fn serialize_u64(self, value: u64) -> Result<Value, crate::ser::Error> { |
885 | 0 | if i64::try_from(value).is_ok() { |
886 | 0 | self.serialize_i64(value as i64) |
887 | | } else { |
888 | 0 | Err(ser::Error::custom("u64 value was too large")) |
889 | | } |
890 | 0 | } |
891 | | |
892 | 0 | fn serialize_f32(self, value: f32) -> Result<Value, crate::ser::Error> { |
893 | 0 | self.serialize_f64(value as f64) |
894 | 0 | } |
895 | | |
896 | 0 | fn serialize_f64(self, mut value: f64) -> Result<Value, crate::ser::Error> { |
897 | | // Discard sign of NaN. See ValueSerializer::serialize_f64. |
898 | 0 | if value.is_nan() { |
899 | 0 | value = value.copysign(1.0); |
900 | 0 | } |
901 | 0 | Ok(Value::Float(value)) |
902 | 0 | } |
903 | | |
904 | 0 | fn serialize_char(self, value: char) -> Result<Value, crate::ser::Error> { |
905 | 0 | let mut s = String::new(); |
906 | 0 | s.push(value); |
907 | 0 | self.serialize_str(&s) |
908 | 0 | } |
909 | | |
910 | 0 | fn serialize_str(self, value: &str) -> Result<Value, crate::ser::Error> { |
911 | 0 | Ok(Value::String(value.to_owned())) |
912 | 0 | } |
913 | | |
914 | 0 | fn serialize_bytes(self, value: &[u8]) -> Result<Value, crate::ser::Error> { |
915 | 0 | let vec = value.iter().map(|&b| Value::Integer(b.into())).collect(); |
916 | 0 | Ok(Value::Array(vec)) |
917 | 0 | } |
918 | | |
919 | 0 | fn serialize_unit(self) -> Result<Value, crate::ser::Error> { |
920 | 0 | Err(crate::ser::Error::unsupported_type(Some("unit"))) |
921 | 0 | } |
922 | | |
923 | 0 | fn serialize_unit_struct(self, name: &'static str) -> Result<Value, crate::ser::Error> { |
924 | 0 | Err(crate::ser::Error::unsupported_type(Some(name))) |
925 | 0 | } |
926 | | |
927 | 0 | fn serialize_unit_variant( |
928 | 0 | self, |
929 | 0 | _name: &'static str, |
930 | 0 | _variant_index: u32, |
931 | 0 | _variant: &'static str, |
932 | 0 | ) -> Result<Value, crate::ser::Error> { |
933 | 0 | self.serialize_str(_variant) |
934 | 0 | } |
935 | | |
936 | 0 | fn serialize_newtype_struct<T>( |
937 | 0 | self, |
938 | 0 | _name: &'static str, |
939 | 0 | value: &T, |
940 | 0 | ) -> Result<Value, crate::ser::Error> |
941 | 0 | where |
942 | 0 | T: ser::Serialize + ?Sized, |
943 | | { |
944 | 0 | value.serialize(self) |
945 | 0 | } |
946 | | |
947 | 0 | fn serialize_newtype_variant<T>( |
948 | 0 | self, |
949 | 0 | _name: &'static str, |
950 | 0 | _variant_index: u32, |
951 | 0 | variant: &'static str, |
952 | 0 | value: &T, |
953 | 0 | ) -> Result<Value, crate::ser::Error> |
954 | 0 | where |
955 | 0 | T: ser::Serialize + ?Sized, |
956 | | { |
957 | 0 | let value = value.serialize(Self)?; |
958 | 0 | let mut table = Table::new(); |
959 | 0 | table.insert(variant.to_owned(), value); |
960 | 0 | Ok(table.into()) |
961 | 0 | } |
962 | | |
963 | 0 | fn serialize_none(self) -> Result<Value, crate::ser::Error> { |
964 | 0 | Err(crate::ser::Error::unsupported_none()) |
965 | 0 | } |
966 | | |
967 | 0 | fn serialize_some<T>(self, value: &T) -> Result<Value, crate::ser::Error> |
968 | 0 | where |
969 | 0 | T: ser::Serialize + ?Sized, |
970 | | { |
971 | 0 | value.serialize(self) |
972 | 0 | } |
973 | | |
974 | 0 | fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, crate::ser::Error> { |
975 | 0 | Ok(ValueSerializeVec { |
976 | 0 | vec: Vec::with_capacity(len.unwrap_or(0)), |
977 | 0 | }) |
978 | 0 | } |
979 | | |
980 | 0 | fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, crate::ser::Error> { |
981 | 0 | self.serialize_seq(Some(len)) |
982 | 0 | } |
983 | | |
984 | 0 | fn serialize_tuple_struct( |
985 | 0 | self, |
986 | 0 | _name: &'static str, |
987 | 0 | len: usize, |
988 | 0 | ) -> Result<Self::SerializeTupleStruct, crate::ser::Error> { |
989 | 0 | self.serialize_seq(Some(len)) |
990 | 0 | } |
991 | | |
992 | 0 | fn serialize_tuple_variant( |
993 | 0 | self, |
994 | 0 | _name: &'static str, |
995 | 0 | _variant_index: u32, |
996 | 0 | variant: &'static str, |
997 | 0 | len: usize, |
998 | 0 | ) -> Result<Self::SerializeTupleVariant, crate::ser::Error> { |
999 | 0 | Ok(ValueSerializeTupleVariant::tuple(variant, len)) |
1000 | 0 | } |
1001 | | |
1002 | 0 | fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, crate::ser::Error> { |
1003 | 0 | Ok(ValueSerializeMap { |
1004 | 0 | ser: crate::table::SerializeMap::new(), |
1005 | 0 | }) |
1006 | 0 | } |
1007 | | |
1008 | 0 | fn serialize_struct( |
1009 | 0 | self, |
1010 | 0 | _name: &'static str, |
1011 | 0 | len: usize, |
1012 | 0 | ) -> Result<Self::SerializeStruct, crate::ser::Error> { |
1013 | 0 | self.serialize_map(Some(len)) |
1014 | 0 | } |
1015 | | |
1016 | 0 | fn serialize_struct_variant( |
1017 | 0 | self, |
1018 | 0 | _name: &'static str, |
1019 | 0 | _variant_index: u32, |
1020 | 0 | variant: &'static str, |
1021 | 0 | len: usize, |
1022 | 0 | ) -> Result<Self::SerializeStructVariant, crate::ser::Error> { |
1023 | 0 | Ok(ValueSerializeStructVariant::struct_(variant, len)) |
1024 | 0 | } |
1025 | | } |
1026 | | |
1027 | | pub(crate) struct ValueSerializeVec { |
1028 | | vec: Vec<Value>, |
1029 | | } |
1030 | | |
1031 | | impl ser::SerializeSeq for ValueSerializeVec { |
1032 | | type Ok = Value; |
1033 | | type Error = crate::ser::Error; |
1034 | | |
1035 | 0 | fn serialize_element<T>(&mut self, value: &T) -> Result<(), crate::ser::Error> |
1036 | 0 | where |
1037 | 0 | T: ser::Serialize + ?Sized, |
1038 | | { |
1039 | 0 | self.vec.push(Value::try_from(value)?); |
1040 | 0 | Ok(()) |
1041 | 0 | } |
1042 | | |
1043 | 0 | fn end(self) -> Result<Value, crate::ser::Error> { |
1044 | 0 | Ok(Value::Array(self.vec)) |
1045 | 0 | } |
1046 | | } |
1047 | | |
1048 | | impl ser::SerializeTuple for ValueSerializeVec { |
1049 | | type Ok = Value; |
1050 | | type Error = crate::ser::Error; |
1051 | | |
1052 | 0 | fn serialize_element<T>(&mut self, value: &T) -> Result<(), crate::ser::Error> |
1053 | 0 | where |
1054 | 0 | T: ser::Serialize + ?Sized, |
1055 | | { |
1056 | 0 | ser::SerializeSeq::serialize_element(self, value) |
1057 | 0 | } |
1058 | | |
1059 | 0 | fn end(self) -> Result<Value, crate::ser::Error> { |
1060 | 0 | ser::SerializeSeq::end(self) |
1061 | 0 | } |
1062 | | } |
1063 | | |
1064 | | impl ser::SerializeTupleStruct for ValueSerializeVec { |
1065 | | type Ok = Value; |
1066 | | type Error = crate::ser::Error; |
1067 | | |
1068 | 0 | fn serialize_field<T>(&mut self, value: &T) -> Result<(), crate::ser::Error> |
1069 | 0 | where |
1070 | 0 | T: ser::Serialize + ?Sized, |
1071 | | { |
1072 | 0 | ser::SerializeSeq::serialize_element(self, value) |
1073 | 0 | } |
1074 | | |
1075 | 0 | fn end(self) -> Result<Value, crate::ser::Error> { |
1076 | 0 | ser::SerializeSeq::end(self) |
1077 | 0 | } |
1078 | | } |
1079 | | |
1080 | | impl ser::SerializeTupleVariant for ValueSerializeVec { |
1081 | | type Ok = Value; |
1082 | | type Error = crate::ser::Error; |
1083 | | |
1084 | 0 | fn serialize_field<T>(&mut self, value: &T) -> Result<(), crate::ser::Error> |
1085 | 0 | where |
1086 | 0 | T: ser::Serialize + ?Sized, |
1087 | | { |
1088 | 0 | ser::SerializeSeq::serialize_element(self, value) |
1089 | 0 | } |
1090 | | |
1091 | 0 | fn end(self) -> Result<Value, crate::ser::Error> { |
1092 | 0 | ser::SerializeSeq::end(self) |
1093 | 0 | } |
1094 | | } |
1095 | | |
1096 | | pub(crate) struct ValueSerializeMap { |
1097 | | ser: crate::table::SerializeMap, |
1098 | | } |
1099 | | |
1100 | | impl ser::SerializeMap for ValueSerializeMap { |
1101 | | type Ok = Value; |
1102 | | type Error = crate::ser::Error; |
1103 | | |
1104 | 0 | fn serialize_key<T>(&mut self, key: &T) -> Result<(), crate::ser::Error> |
1105 | 0 | where |
1106 | 0 | T: ser::Serialize + ?Sized, |
1107 | | { |
1108 | 0 | self.ser.serialize_key(key) |
1109 | 0 | } |
1110 | | |
1111 | 0 | fn serialize_value<T>(&mut self, value: &T) -> Result<(), crate::ser::Error> |
1112 | 0 | where |
1113 | 0 | T: ser::Serialize + ?Sized, |
1114 | | { |
1115 | 0 | self.ser.serialize_value(value) |
1116 | 0 | } |
1117 | | |
1118 | 0 | fn end(self) -> Result<Value, crate::ser::Error> { |
1119 | 0 | self.ser.end().map(Value::Table) |
1120 | 0 | } |
1121 | | } |
1122 | | |
1123 | | impl ser::SerializeStruct for ValueSerializeMap { |
1124 | | type Ok = Value; |
1125 | | type Error = crate::ser::Error; |
1126 | | |
1127 | 0 | fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), crate::ser::Error> |
1128 | 0 | where |
1129 | 0 | T: ser::Serialize + ?Sized, |
1130 | | { |
1131 | 0 | ser::SerializeMap::serialize_key(self, key)?; |
1132 | 0 | ser::SerializeMap::serialize_value(self, value) |
1133 | 0 | } |
1134 | | |
1135 | 0 | fn end(self) -> Result<Value, crate::ser::Error> { |
1136 | 0 | ser::SerializeMap::end(self) |
1137 | 0 | } |
1138 | | } |
1139 | | |
1140 | | type ValueSerializeTupleVariant = ValueSerializeVariant<ValueSerializeVec>; |
1141 | | type ValueSerializeStructVariant = ValueSerializeVariant<ValueSerializeMap>; |
1142 | | |
1143 | | pub(crate) struct ValueSerializeVariant<T> { |
1144 | | variant: &'static str, |
1145 | | inner: T, |
1146 | | } |
1147 | | |
1148 | | impl ValueSerializeVariant<ValueSerializeVec> { |
1149 | 0 | pub(crate) fn tuple(variant: &'static str, len: usize) -> Self { |
1150 | 0 | Self { |
1151 | 0 | variant, |
1152 | 0 | inner: ValueSerializeVec { |
1153 | 0 | vec: Vec::with_capacity(len), |
1154 | 0 | }, |
1155 | 0 | } |
1156 | 0 | } |
1157 | | } |
1158 | | |
1159 | | impl ValueSerializeVariant<ValueSerializeMap> { |
1160 | 0 | pub(crate) fn struct_(variant: &'static str, len: usize) -> Self { |
1161 | 0 | Self { |
1162 | 0 | variant, |
1163 | 0 | inner: ValueSerializeMap { |
1164 | 0 | ser: crate::table::SerializeMap::with_capacity(len), |
1165 | 0 | }, |
1166 | 0 | } |
1167 | 0 | } |
1168 | | } |
1169 | | |
1170 | | impl ser::SerializeTupleVariant for ValueSerializeVariant<ValueSerializeVec> { |
1171 | | type Ok = Value; |
1172 | | type Error = crate::ser::Error; |
1173 | | |
1174 | 0 | fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error> |
1175 | 0 | where |
1176 | 0 | T: ser::Serialize + ?Sized, |
1177 | | { |
1178 | 0 | ser::SerializeSeq::serialize_element(&mut self.inner, value) |
1179 | 0 | } |
1180 | | |
1181 | 0 | fn end(self) -> Result<Self::Ok, Self::Error> { |
1182 | 0 | let inner = ser::SerializeSeq::end(self.inner)?; |
1183 | 0 | let mut table = Table::new(); |
1184 | 0 | table.insert(self.variant.to_owned(), inner); |
1185 | 0 | Ok(Value::Table(table)) |
1186 | 0 | } |
1187 | | } |
1188 | | |
1189 | | impl ser::SerializeStructVariant for ValueSerializeVariant<ValueSerializeMap> { |
1190 | | type Ok = Value; |
1191 | | type Error = crate::ser::Error; |
1192 | | |
1193 | | #[inline] |
1194 | 0 | fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> |
1195 | 0 | where |
1196 | 0 | T: ser::Serialize + ?Sized, |
1197 | | { |
1198 | 0 | ser::SerializeStruct::serialize_field(&mut self.inner, key, value) |
1199 | 0 | } |
1200 | | |
1201 | | #[inline] |
1202 | 0 | fn end(self) -> Result<Self::Ok, Self::Error> { |
1203 | 0 | let inner = ser::SerializeStruct::end(self.inner)?; |
1204 | 0 | let mut table = Table::new(); |
1205 | 0 | table.insert(self.variant.to_owned(), inner); |
1206 | 0 | Ok(Value::Table(table)) |
1207 | 0 | } |
1208 | | } |