1use super::Entry;
2use crate::entry::{string_is_array, ParseItem, Statement};
3use crate::error::{ParseStringError, SerdeParseError};
4use crate::VdfError;
5use serde_core::de::{Error, Visitor};
6use serde_core::{Deserialize, Deserializer, Serialize, Serializer};
7use std::borrow::Cow;
8use std::fmt::Formatter;
9use std::ops::{Deref, DerefMut};
10
11#[derive(Clone, PartialEq, Eq, Debug, Default)]
12pub struct Value(String);
13
14impl Serialize for Value {
15 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
16 where
17 S: Serializer,
18 {
19 self.0.serialize(serializer)
20 }
21}
22
23impl From<Cow<'_, str>> for Value {
24 fn from(value: Cow<'_, str>) -> Value {
25 Value(value.into())
26 }
27}
28impl From<&'_ str> for Value {
29 fn from(value: &str) -> Value {
30 Value(value.into())
31 }
32}
33
34impl From<String> for Value {
35 fn from(value: String) -> Value {
36 Value(value)
37 }
38}
39
40impl From<Statement> for Value {
41 fn from(value: Statement) -> Value {
42 Value(value.into())
43 }
44}
45
46impl From<Value> for Entry {
47 fn from(value: Value) -> Self {
48 Entry::Value(value)
49 }
50}
51
52impl From<Value> for String {
53 fn from(value: Value) -> Self {
54 value.0
55 }
56}
57
58impl Deref for Value {
59 type Target = str;
60
61 fn deref(&self) -> &Self::Target {
62 &self.0
63 }
64}
65
66impl DerefMut for Value {
67 fn deref_mut(&mut self) -> &mut Self::Target {
68 &mut self.0
69 }
70}
71
72impl Value {
73 pub fn to<T: ParseItem>(self) -> Result<T, ParseStringError> {
75 T::from_str(&self.0)
76 }
77}
78
79impl<'de> Deserialize<'de> for Value {
80 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
81 where
82 D: Deserializer<'de>,
83 {
84 struct ValueVisitor;
85
86 impl Visitor<'_> for ValueVisitor {
87 type Value = String;
88
89 fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
90 write!(formatter, "any string like value")
91 }
92
93 fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
94 where
95 E: Error,
96 {
97 Ok(if v { "1".into() } else { "0".into() })
98 }
99
100 fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
101 where
102 E: Error,
103 {
104 Ok(v.to_string())
105 }
106
107 fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
108 where
109 E: Error,
110 {
111 Ok(v.to_string())
112 }
113
114 fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
115 where
116 E: Error,
117 {
118 Ok(v.to_string())
119 }
120
121 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
122 where
123 E: Error,
124 {
125 Ok(v.into())
126 }
127
128 fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
129 where
130 E: Error,
131 {
132 Ok(v)
133 }
134 }
135
136 deserializer.deserialize_str(ValueVisitor).map(Value)
137 }
138}
139
140impl<'de> Deserializer<'de> for Value {
141 type Error = VdfError;
142
143 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
144 where
145 V: Visitor<'de>,
146 {
147 if let Ok(int) = i64::from_str(&self.0) {
148 return visitor.visit_i64(int);
149 }
150 if let Ok(float) = f64::from_str(&self.0) {
151 return visitor.visit_f64(float);
152 }
153 if string_is_array(&self.0) {
154 return self.deserialize_seq(visitor);
155 }
156 visitor.visit_string(self.0)
157 }
158
159 fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
160 where
161 V: Visitor<'de>,
162 {
163 visitor.visit_bool(self.to()?)
164 }
165
166 fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
167 where
168 V: Visitor<'de>,
169 {
170 visitor.visit_i8(self.to()?)
171 }
172
173 fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
174 where
175 V: Visitor<'de>,
176 {
177 visitor.visit_i16(self.to()?)
178 }
179
180 fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
181 where
182 V: Visitor<'de>,
183 {
184 visitor.visit_i32(self.to()?)
185 }
186
187 fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
188 where
189 V: Visitor<'de>,
190 {
191 visitor.visit_i64(self.to()?)
192 }
193
194 fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
195 where
196 V: Visitor<'de>,
197 {
198 visitor.visit_u8(self.to()?)
199 }
200
201 fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
202 where
203 V: Visitor<'de>,
204 {
205 visitor.visit_u16(self.to()?)
206 }
207
208 fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
209 where
210 V: Visitor<'de>,
211 {
212 visitor.visit_u32(self.to()?)
213 }
214
215 fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
216 where
217 V: Visitor<'de>,
218 {
219 visitor.visit_u64(self.to()?)
220 }
221
222 fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
223 where
224 V: Visitor<'de>,
225 {
226 visitor.visit_f32(self.to()?)
227 }
228
229 fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
230 where
231 V: Visitor<'de>,
232 {
233 visitor.visit_f64(self.to()?)
234 }
235
236 fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
237 where
238 V: Visitor<'de>,
239 {
240 let mut chars = self.0.chars();
241 match (chars.next(), chars.next()) {
242 (Some(_), None) => Ok(()),
243 _ => Err(SerdeParseError::new("char", &self.0, 0..0, "")),
244 }?;
245
246 visitor.visit_str(&self.0)
247 }
248
249 fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
250 where
251 V: Visitor<'de>,
252 {
253 visitor.visit_str(&self.0)
254 }
255
256 fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
257 where
258 V: Visitor<'de>,
259 {
260 visitor.visit_string(self.0)
261 }
262
263 fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
264 where
265 V: Visitor<'de>,
266 {
267 visitor.visit_bytes(self.0.as_bytes())
268 }
269
270 fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
271 where
272 V: Visitor<'de>,
273 {
274 visitor.visit_byte_buf(self.0.into_bytes())
275 }
276
277 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
278 where
279 V: Visitor<'de>,
280 {
281 if self.0.is_empty() {
282 return visitor.visit_none();
283 }
284 visitor.visit_some(self)
285 }
286
287 fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
288 where
289 V: Visitor<'de>,
290 {
291 if !self.0.is_empty() {
292 return Err(SerdeParseError::new("unit", self.0.as_ref(), 0..0, "").into());
293 }
294 visitor.visit_unit()
295 }
296
297 fn deserialize_unit_struct<V>(
298 self,
299 _name: &'static str,
300 visitor: V,
301 ) -> Result<V::Value, Self::Error>
302 where
303 V: Visitor<'de>,
304 {
305 if !self.0.is_empty() {
306 return Err(SerdeParseError::new("unit", self.0.as_ref(), 0..0, "").into());
307 }
308 visitor.visit_unit()
309 }
310
311 fn deserialize_newtype_struct<V>(
312 self,
313 _name: &'static str,
314 visitor: V,
315 ) -> Result<V::Value, Self::Error>
316 where
317 V: Visitor<'de>,
318 {
319 visitor.visit_newtype_struct(self)
320 }
321
322 fn deserialize_seq<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
323 where
324 V: Visitor<'de>,
325 {
326 Err(SerdeParseError::new("seq", self.0.as_ref(), 0..0, "").into())
327 }
328
329 fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
330 where
331 V: Visitor<'de>,
332 {
333 self.deserialize_seq(visitor)
334 }
335
336 fn deserialize_tuple_struct<V>(
337 self,
338 _name: &'static str,
339 _len: usize,
340 visitor: V,
341 ) -> Result<V::Value, Self::Error>
342 where
343 V: Visitor<'de>,
344 {
345 self.deserialize_seq(visitor)
346 }
347
348 fn deserialize_map<V>(self, _visitor: V) -> Result<V::Value, Self::Error>
349 where
350 V: Visitor<'de>,
351 {
352 Err(SerdeParseError::new("map", self.0.as_ref(), 0..0, "").into())
353 }
354
355 fn deserialize_struct<V>(
356 self,
357 _name: &'static str,
358 _fields: &'static [&'static str],
359 _visitor: V,
360 ) -> Result<V::Value, Self::Error>
361 where
362 V: Visitor<'de>,
363 {
364 Err(SerdeParseError::new("struct", self.0.as_ref(), 0..0, "").into())
365 }
366
367 fn deserialize_enum<V>(
368 self,
369 _name: &'static str,
370 _variants: &'static [&'static str],
371 _visitor: V,
372 ) -> Result<V::Value, Self::Error>
373 where
374 V: Visitor<'de>,
375 {
376 Err(SerdeParseError::new("map", self.0.as_ref(), 0..0, "").into())
377 }
378
379 fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
380 where
381 V: Visitor<'de>,
382 {
383 self.deserialize_str(visitor)
384 }
385
386 fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
387 where
388 V: Visitor<'de>,
389 {
390 self.deserialize_any(visitor)
391 }
392}
393
394#[cfg(test)]
395#[track_caller]
396fn unwrap_err<T>(r: Result<T, crate::VdfError>) -> T {
397 r.map_err(miette::Error::from).unwrap()
398}
399
400#[test]
401fn test_serde_value() {
402 let j = r#"1"#;
403 assert_eq!(Value("1".into()), unwrap_err(crate::from_str(j)));
404
405 let j = r#""foo bar""#;
406 assert_eq!(Value("foo bar".into()), unwrap_err(crate::from_str(j)));
407}
408
409#[test]
410fn test_serde_from_value() {
411 let j = Value::from("1");
412 assert!(unwrap_err(crate::from_entry(j.into())));
413}