Skip to main content

yazi_shared/data/
de_owned.rs

1use std::borrow::Cow;
2
3use serde::{Deserializer, de::{self, Error, IntoDeserializer, MapAccess, SeqAccess}};
4
5use crate::data::{BytesDeserializer, Data, DataKey, KeyDeserializer};
6
7impl<'de> Deserializer<'de> for Data {
8	type Error = de::value::Error;
9
10	fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
11	where
12		V: de::Visitor<'de>,
13	{
14		match self {
15			Self::Nil => visitor.visit_unit(),
16			Self::Boolean(b) => visitor.visit_bool(b),
17			Self::Integer(i) => visitor.visit_i64(i),
18			Self::Number(n) => visitor.visit_f64(n),
19			Self::String(Cow::Borrowed(s)) => visitor.visit_borrowed_str(s),
20			Self::String(Cow::Owned(s)) => visitor.visit_string(s),
21			Self::List(l) => visitor.visit_seq(SeqDeserializer { iter: l.into_iter() }),
22			Self::Dict(d) => visitor.visit_map(MapDeserializer { iter: d.into_iter(), value: None }),
23			Self::Id(i) => visitor.visit_u64(i.get()),
24			Self::Url(u) => u.into_deserializer().deserialize_any(visitor),
25			Self::Path(_) => Err(Error::custom("path not supported")),
26			Self::Bytes(b) => BytesDeserializer(b.into()).deserialize_any(visitor),
27			Self::Any(_) => Err(Error::custom("any not supported")),
28		}
29	}
30
31	fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
32	where
33		V: de::Visitor<'de>,
34	{
35		visitor.visit_bool((&self).try_into().map_err(Error::custom)?)
36	}
37
38	fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
39	where
40		V: de::Visitor<'de>,
41	{
42		visitor.visit_i8((&self).try_into().map_err(Error::custom)?)
43	}
44
45	fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
46	where
47		V: de::Visitor<'de>,
48	{
49		visitor.visit_i16((&self).try_into().map_err(Error::custom)?)
50	}
51
52	fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
53	where
54		V: de::Visitor<'de>,
55	{
56		visitor.visit_i32((&self).try_into().map_err(Error::custom)?)
57	}
58
59	fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
60	where
61		V: de::Visitor<'de>,
62	{
63		visitor.visit_i64((&self).try_into().map_err(Error::custom)?)
64	}
65
66	fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
67	where
68		V: de::Visitor<'de>,
69	{
70		visitor.visit_u8((&self).try_into().map_err(Error::custom)?)
71	}
72
73	fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
74	where
75		V: de::Visitor<'de>,
76	{
77		visitor.visit_u16((&self).try_into().map_err(Error::custom)?)
78	}
79
80	fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
81	where
82		V: de::Visitor<'de>,
83	{
84		visitor.visit_u32((&self).try_into().map_err(Error::custom)?)
85	}
86
87	fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
88	where
89		V: de::Visitor<'de>,
90	{
91		visitor.visit_u64((&self).try_into().map_err(Error::custom)?)
92	}
93
94	fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
95	where
96		V: de::Visitor<'de>,
97	{
98		visitor.visit_f32((&self).try_into().map_err(Error::custom)?)
99	}
100
101	fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
102	where
103		V: de::Visitor<'de>,
104	{
105		visitor.visit_f64((&self).try_into().map_err(Error::custom)?)
106	}
107
108	fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
109	where
110		V: de::Visitor<'de>,
111	{
112		let s: &str = (&self).try_into().map_err(Error::custom)?;
113		let mut chars = s.chars();
114		match (chars.next(), chars.next()) {
115			(Some(ch), None) => visitor.visit_char(ch),
116			_ => Err(Error::custom("not a char")),
117		}
118	}
119
120	fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
121	where
122		V: de::Visitor<'de>,
123	{
124		match self {
125			Self::String(Cow::Borrowed(s)) => visitor.visit_borrowed_str(s),
126			Self::String(Cow::Owned(s)) => visitor.visit_string(s),
127			Self::Url(u) => visitor.visit_newtype_struct(u.into_deserializer()),
128			_ => Err(Error::custom("not a string")),
129		}
130	}
131
132	fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
133	where
134		V: de::Visitor<'de>,
135	{
136		self.deserialize_str(visitor)
137	}
138
139	fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
140	where
141		V: de::Visitor<'de>,
142	{
143		match self {
144			Self::Bytes(b) => BytesDeserializer(b.into()).deserialize_bytes(visitor),
145			_ => Err(Error::custom("not bytes")),
146		}
147	}
148
149	fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
150	where
151		V: de::Visitor<'de>,
152	{
153		self.deserialize_bytes(visitor)
154	}
155
156	fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
157	where
158		V: de::Visitor<'de>,
159	{
160		match self {
161			Self::Nil => visitor.visit_none(),
162			other => visitor.visit_some(other),
163		}
164	}
165
166	fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
167	where
168		V: de::Visitor<'de>,
169	{
170		match self {
171			Self::Nil => visitor.visit_unit(),
172			_ => Err(Error::custom("expected unit")),
173		}
174	}
175
176	fn deserialize_unit_struct<V>(
177		self,
178		_name: &'static str,
179		visitor: V,
180	) -> Result<V::Value, Self::Error>
181	where
182		V: de::Visitor<'de>,
183	{
184		match self {
185			Self::Nil => visitor.visit_unit(),
186			_ => Err(Error::custom("expected unit struct")),
187		}
188	}
189
190	fn deserialize_newtype_struct<V>(
191		self,
192		_name: &'static str,
193		visitor: V,
194	) -> Result<V::Value, Self::Error>
195	where
196		V: de::Visitor<'de>,
197	{
198		visitor.visit_newtype_struct(self)
199	}
200
201	fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
202	where
203		V: de::Visitor<'de>,
204	{
205		match self {
206			Self::List(l) => visitor.visit_seq(SeqDeserializer { iter: l.into_iter() }),
207			Self::Bytes(b) => BytesDeserializer(b.into()).deserialize_seq(visitor),
208			_ => Err(Error::custom("not a sequence")),
209		}
210	}
211
212	fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
213	where
214		V: de::Visitor<'de>,
215	{
216		self.deserialize_seq(visitor)
217	}
218
219	fn deserialize_tuple_struct<V>(
220		self,
221		_name: &'static str,
222		_len: usize,
223		visitor: V,
224	) -> Result<V::Value, Self::Error>
225	where
226		V: de::Visitor<'de>,
227	{
228		self.deserialize_seq(visitor)
229	}
230
231	fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
232	where
233		V: de::Visitor<'de>,
234	{
235		match self {
236			Self::Dict(d) => visitor.visit_map(MapDeserializer { iter: d.into_iter(), value: None }),
237			Self::Url(u) => Deserializer::deserialize_map(u.into_deserializer(), visitor),
238			_ => Err(Error::custom("not a map")),
239		}
240	}
241
242	fn deserialize_struct<V>(
243		self,
244		_name: &'static str,
245		_fields: &'static [&'static str],
246		visitor: V,
247	) -> Result<V::Value, Self::Error>
248	where
249		V: de::Visitor<'de>,
250	{
251		self.deserialize_map(visitor)
252	}
253
254	fn deserialize_enum<V>(
255		self,
256		_name: &'static str,
257		_variants: &'static [&'static str],
258		visitor: V,
259	) -> Result<V::Value, Self::Error>
260	where
261		V: de::Visitor<'de>,
262	{
263		match self {
264			Self::String(s) => visitor.visit_enum(s.into_deserializer()),
265			_ => Err(Error::custom("not an enum")),
266		}
267	}
268
269	fn deserialize_identifier<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
270	where
271		V: de::Visitor<'de>,
272	{
273		Err(Error::custom("identifier not supported"))
274	}
275
276	fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
277	where
278		V: de::Visitor<'de>,
279	{
280		visitor.visit_unit()
281	}
282}
283
284impl<'de> IntoDeserializer<'de, de::value::Error> for Data {
285	type Deserializer = Self;
286
287	fn into_deserializer(self) -> Self::Deserializer { self }
288}
289
290// --- Seq
291struct SeqDeserializer {
292	iter: std::vec::IntoIter<Data>,
293}
294
295impl<'de> SeqAccess<'de> for SeqDeserializer {
296	type Error = de::value::Error;
297
298	fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
299	where
300		T: de::DeserializeSeed<'de>,
301	{
302		self.iter.next().map(|value| seed.deserialize(value)).transpose()
303	}
304
305	fn size_hint(&self) -> Option<usize> { Some(self.iter.len()) }
306}
307
308// --- Map
309struct MapDeserializer {
310	iter:  hashbrown::hash_map::IntoIter<DataKey, Data>,
311	value: Option<Data>,
312}
313
314impl<'de> MapAccess<'de> for MapDeserializer {
315	type Error = de::value::Error;
316
317	fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
318	where
319		K: de::DeserializeSeed<'de>,
320	{
321		let Some((key, value)) = self.iter.next() else { return Ok(None) };
322		self.value = Some(value);
323
324		seed.deserialize(KeyDeserializer::Owned(key)).map(Some)
325	}
326
327	fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
328	where
329		V: de::DeserializeSeed<'de>,
330	{
331		seed.deserialize(self.value.take().ok_or_else(|| Error::custom("value missing for key"))?)
332	}
333
334	fn size_hint(&self) -> Option<usize> { Some(self.iter.len()) }
335}