]> git.proxmox.com Git - cargo.git/blob - vendor/toml_edit/src/de/table.rs
New upstream version 0.63.1
[cargo.git] / vendor / toml_edit / src / de / table.rs
1 use serde::de::IntoDeserializer;
2
3 use crate::de::Error;
4
5 impl<'de, 'a> serde::Deserializer<'de> for crate::Table {
6 type Error = Error;
7
8 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
9 where
10 V: serde::de::Visitor<'de>,
11 {
12 visitor.visit_map(crate::de::TableMapAccess::new(self))
13 }
14
15 // `None` is interpreted as a missing field so be sure to implement `Some`
16 // as a present field.
17 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error>
18 where
19 V: serde::de::Visitor<'de>,
20 {
21 visitor.visit_some(self)
22 }
23
24 fn deserialize_newtype_struct<V>(
25 self,
26 _name: &'static str,
27 visitor: V,
28 ) -> Result<V::Value, Error>
29 where
30 V: serde::de::Visitor<'de>,
31 {
32 visitor.visit_newtype_struct(self)
33 }
34
35 fn deserialize_struct<V>(
36 self,
37 _name: &'static str,
38 _fields: &'static [&'static str],
39 visitor: V,
40 ) -> Result<V::Value, Error>
41 where
42 V: serde::de::Visitor<'de>,
43 {
44 self.deserialize_any(visitor)
45 }
46
47 // Called when the type to deserialize is an enum, as opposed to a field in the type.
48 fn deserialize_enum<V>(
49 self,
50 _name: &'static str,
51 _variants: &'static [&'static str],
52 visitor: V,
53 ) -> Result<V::Value, Error>
54 where
55 V: serde::de::Visitor<'de>,
56 {
57 if self.is_empty() {
58 Err(crate::de::Error::custom(
59 "wanted exactly 1 element, found 0 elements",
60 ))
61 } else if self.len() != 1 {
62 Err(crate::de::Error::custom(
63 "wanted exactly 1 element, more than 1 element",
64 ))
65 } else {
66 visitor.visit_enum(crate::de::TableMapAccess::new(self))
67 }
68 }
69
70 serde::forward_to_deserialize_any! {
71 bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
72 bytes byte_buf map unit
73 ignored_any unit_struct tuple_struct tuple identifier
74 }
75 }
76
77 impl<'de> serde::de::IntoDeserializer<'de, crate::de::Error> for crate::Table {
78 type Deserializer = Self;
79
80 fn into_deserializer(self) -> Self {
81 self
82 }
83 }
84
85 pub(crate) struct TableMapAccess {
86 iter: indexmap::map::IntoIter<crate::InternalString, crate::table::TableKeyValue>,
87 value: Option<(crate::InternalString, crate::Item)>,
88 }
89
90 impl TableMapAccess {
91 pub(crate) fn new(input: crate::Table) -> Self {
92 Self {
93 iter: input.items.into_iter(),
94 value: None,
95 }
96 }
97 }
98
99 impl<'de> serde::de::MapAccess<'de> for TableMapAccess {
100 type Error = Error;
101
102 fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
103 where
104 K: serde::de::DeserializeSeed<'de>,
105 {
106 match self.iter.next() {
107 Some((k, v)) => {
108 let ret = seed.deserialize(k.into_deserializer()).map(Some);
109 self.value = Some((k, v.value));
110 ret
111 }
112 None => Ok(None),
113 }
114 }
115
116 fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
117 where
118 V: serde::de::DeserializeSeed<'de>,
119 {
120 match self.value.take() {
121 Some((k, v)) => seed
122 .deserialize(crate::de::ItemDeserializer::new(v))
123 .map_err(|mut err| {
124 err.parent_key(k);
125 err
126 }),
127 None => {
128 panic!("no more values in next_value_seed, internal error in ItemDeserializer")
129 }
130 }
131 }
132 }
133
134 impl<'de> serde::de::EnumAccess<'de> for TableMapAccess {
135 type Error = Error;
136 type Variant = super::TableEnumDeserializer;
137
138 fn variant_seed<V>(mut self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
139 where
140 V: serde::de::DeserializeSeed<'de>,
141 {
142 let (key, value) = match self.iter.next() {
143 Some(pair) => pair,
144 None => {
145 return Err(Error::custom(
146 "expected table with exactly 1 entry, found empty table",
147 ));
148 }
149 };
150
151 seed.deserialize(key.into_deserializer())
152 .map(|val| (val, super::TableEnumDeserializer::new(value.value)))
153 }
154 }