1use alloc::{
6 string::{String, ToString},
7 sync::Arc,
8 vec::Vec,
9};
10use core::fmt;
11
12use serde::ser::{self, Serialize};
13
14use crate::{compat::HashMap, value::Value};
15
16pub fn to_value<T: Serialize>(value: &T) -> Result<Value, SerError> {
46 value.serialize(ValueSerializer)
47}
48
49#[derive(Debug)]
51pub struct SerError(String);
52
53impl fmt::Display for SerError {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 f.write_str(&self.0)
56 }
57}
58
59impl core::error::Error for SerError {}
60
61impl ser::Error for SerError {
62 fn custom<T: fmt::Display>(msg: T) -> Self {
63 Self(msg.to_string())
64 }
65}
66
67struct ValueSerializer;
72
73impl ser::Serializer for ValueSerializer {
74 type Ok = Value;
75 type Error = SerError;
76 type SerializeSeq = SeqBuilder;
77 type SerializeTuple = SeqBuilder;
78 type SerializeTupleStruct = SeqBuilder;
79 type SerializeTupleVariant = SeqBuilder;
80 type SerializeMap = MapBuilder;
81 type SerializeStruct = MapBuilder;
82 type SerializeStructVariant = MapBuilder;
83
84 fn serialize_bool(self, v: bool) -> Result<Value, SerError> {
85 Ok(Value::Bool(v))
86 }
87
88 fn serialize_i8(self, v: i8) -> Result<Value, SerError> {
89 Ok(Value::Int(i64::from(v)))
90 }
91 fn serialize_i16(self, v: i16) -> Result<Value, SerError> {
92 Ok(Value::Int(i64::from(v)))
93 }
94 fn serialize_i32(self, v: i32) -> Result<Value, SerError> {
95 Ok(Value::Int(i64::from(v)))
96 }
97 fn serialize_i64(self, v: i64) -> Result<Value, SerError> {
98 Ok(Value::Int(v))
99 }
100 fn serialize_u8(self, v: u8) -> Result<Value, SerError> {
101 Ok(Value::Int(i64::from(v)))
102 }
103 fn serialize_u16(self, v: u16) -> Result<Value, SerError> {
104 Ok(Value::Int(i64::from(v)))
105 }
106 fn serialize_u32(self, v: u32) -> Result<Value, SerError> {
107 Ok(Value::Int(i64::from(v)))
108 }
109 fn serialize_u64(self, v: u64) -> Result<Value, SerError> {
110 let i = i64::try_from(v).map_err(|_| {
111 <SerError as ser::Error>::custom(format!("u64 value {v} exceeds i64::MAX"))
112 })?;
113 Ok(Value::Int(i))
114 }
115 fn serialize_f32(self, v: f32) -> Result<Value, SerError> {
116 Ok(Value::Float(f64::from(v)))
117 }
118 fn serialize_f64(self, v: f64) -> Result<Value, SerError> {
119 Ok(Value::Float(v))
120 }
121 fn serialize_char(self, v: char) -> Result<Value, SerError> {
122 Ok(Value::Str(v.to_string()))
123 }
124 fn serialize_str(self, v: &str) -> Result<Value, SerError> {
125 Ok(Value::Str(v.to_string()))
126 }
127 fn serialize_bytes(self, _v: &[u8]) -> Result<Value, SerError> {
128 Err(SerError("byte arrays are not supported".into()))
129 }
130
131 fn serialize_none(self) -> Result<Value, SerError> {
132 Ok(Value::None)
134 }
135 fn serialize_some<T: ?Sized + Serialize>(self, value: &T) -> Result<Value, SerError> {
136 value.serialize(self)
137 }
138 fn serialize_unit(self) -> Result<Value, SerError> {
139 Ok(Value::Str(String::new()))
140 }
141 fn serialize_unit_struct(self, _name: &'static str) -> Result<Value, SerError> {
142 Ok(Value::Str(String::new()))
143 }
144 fn serialize_unit_variant(
145 self,
146 _name: &'static str,
147 _idx: u32,
148 variant: &'static str,
149 ) -> Result<Value, SerError> {
150 Ok(Value::Str(variant.to_string()))
151 }
152
153 fn serialize_newtype_struct<T: ?Sized + Serialize>(
154 self,
155 _name: &'static str,
156 value: &T,
157 ) -> Result<Value, SerError> {
158 value.serialize(self)
159 }
160
161 fn serialize_newtype_variant<T: ?Sized + Serialize>(
162 self,
163 _name: &'static str,
164 _idx: u32,
165 variant: &'static str,
166 value: &T,
167 ) -> Result<Value, SerError> {
168 let inner = value.serialize(ValueSerializer)?;
169 Ok(Value::Struct(Arc::new(HashMap::from([(
170 variant.to_string(),
171 inner,
172 )]))))
173 }
174
175 fn serialize_seq(self, len: Option<usize>) -> Result<SeqBuilder, SerError> {
176 Ok(SeqBuilder(Vec::with_capacity(len.unwrap_or(0))))
177 }
178 fn serialize_tuple(self, len: usize) -> Result<SeqBuilder, SerError> {
179 Ok(SeqBuilder(Vec::with_capacity(len)))
180 }
181 fn serialize_tuple_struct(
182 self,
183 _name: &'static str,
184 len: usize,
185 ) -> Result<SeqBuilder, SerError> {
186 Ok(SeqBuilder(Vec::with_capacity(len)))
187 }
188 fn serialize_tuple_variant(
189 self,
190 _name: &'static str,
191 _idx: u32,
192 _variant: &'static str,
193 len: usize,
194 ) -> Result<SeqBuilder, SerError> {
195 Ok(SeqBuilder(Vec::with_capacity(len)))
196 }
197
198 fn serialize_map(self, len: Option<usize>) -> Result<MapBuilder, SerError> {
199 Ok(MapBuilder {
200 map: HashMap::with_capacity(len.unwrap_or(0)),
201 pending_key: None,
202 })
203 }
204 fn serialize_struct(self, _name: &'static str, len: usize) -> Result<MapBuilder, SerError> {
205 Ok(MapBuilder {
206 map: HashMap::with_capacity(len),
207 pending_key: None,
208 })
209 }
210 fn serialize_struct_variant(
211 self,
212 _name: &'static str,
213 _idx: u32,
214 variant: &'static str,
215 len: usize,
216 ) -> Result<MapBuilder, SerError> {
217 let mut map = HashMap::with_capacity(len + 1); map.insert(
219 crate::consts::ENUM_TAG_KEY.to_string(),
220 Value::Str(variant.to_string()),
221 );
222 Ok(MapBuilder {
223 map,
224 pending_key: None,
225 })
226 }
227}
228
229struct SeqBuilder(Vec<Value>);
234
235impl ser::SerializeSeq for SeqBuilder {
236 type Ok = Value;
237 type Error = SerError;
238 fn serialize_element<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), SerError> {
239 self.0.push(value.serialize(ValueSerializer)?);
240 Ok(())
241 }
242 fn end(self) -> Result<Value, SerError> {
243 Ok(Value::List(Arc::new(self.0)))
244 }
245}
246
247impl ser::SerializeTuple for SeqBuilder {
248 type Ok = Value;
249 type Error = SerError;
250 fn serialize_element<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), SerError> {
251 ser::SerializeSeq::serialize_element(self, value)
252 }
253 fn end(self) -> Result<Value, SerError> {
254 ser::SerializeSeq::end(self)
255 }
256}
257
258impl ser::SerializeTupleStruct for SeqBuilder {
259 type Ok = Value;
260 type Error = SerError;
261 fn serialize_field<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), SerError> {
262 ser::SerializeSeq::serialize_element(self, value)
263 }
264 fn end(self) -> Result<Value, SerError> {
265 ser::SerializeSeq::end(self)
266 }
267}
268
269impl ser::SerializeTupleVariant for SeqBuilder {
270 type Ok = Value;
271 type Error = SerError;
272 fn serialize_field<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), SerError> {
273 ser::SerializeSeq::serialize_element(self, value)
274 }
275 fn end(self) -> Result<Value, SerError> {
276 ser::SerializeSeq::end(self)
277 }
278}
279
280struct MapBuilder {
285 map: HashMap<String, Value>,
286 pending_key: Option<String>,
287}
288
289impl ser::SerializeMap for MapBuilder {
290 type Ok = Value;
291 type Error = SerError;
292 fn serialize_key<T: ?Sized + Serialize>(&mut self, key: &T) -> Result<(), SerError> {
293 let key_val = key.serialize(ValueSerializer)?;
294 match key_val {
295 Value::Str(s) => {
296 self.pending_key = Some(s);
297 Ok(())
298 }
299 Value::Int(i) => {
300 self.pending_key = Some(i.to_string());
301 Ok(())
302 }
303 other => Err(SerError(format!(
304 "map keys must be strings or integers, got {}",
305 other.type_name()
306 ))),
307 }
308 }
309 fn serialize_value<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), SerError> {
310 let key = self
311 .pending_key
312 .take()
313 .ok_or_else(|| SerError("serialize_value called without serialize_key".into()))?;
314 self.map.insert(key, value.serialize(ValueSerializer)?);
315 Ok(())
316 }
317 fn end(self) -> Result<Value, SerError> {
318 Ok(Value::Struct(Arc::new(self.map)))
319 }
320}
321
322impl ser::SerializeStruct for MapBuilder {
323 type Ok = Value;
324 type Error = SerError;
325 fn serialize_field<T: ?Sized + Serialize>(
326 &mut self,
327 key: &'static str,
328 value: &T,
329 ) -> Result<(), SerError> {
330 self.map
331 .insert(key.to_string(), value.serialize(ValueSerializer)?);
332 Ok(())
333 }
334 fn end(self) -> Result<Value, SerError> {
335 Ok(Value::Struct(Arc::new(self.map)))
336 }
337}
338
339impl ser::SerializeStructVariant for MapBuilder {
340 type Ok = Value;
341 type Error = SerError;
342 fn serialize_field<T: ?Sized + Serialize>(
343 &mut self,
344 key: &'static str,
345 value: &T,
346 ) -> Result<(), SerError> {
347 ser::SerializeStruct::serialize_field(self, key, value)
348 }
349 fn end(self) -> Result<Value, SerError> {
350 ser::SerializeStruct::end(self)
351 }
352}
353
354use serde::de::{self, Deserialize};
359
360#[derive(Debug)]
362pub struct DeError(String);
363
364impl fmt::Display for DeError {
365 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
366 f.write_str(&self.0)
367 }
368}
369
370impl core::error::Error for DeError {}
371
372impl de::Error for DeError {
373 fn custom<T: fmt::Display>(msg: T) -> Self {
374 Self(msg.to_string())
375 }
376}
377
378pub fn from_value<'de, T: Deserialize<'de>>(value: &'de Value) -> Result<T, DeError> {
414 T::deserialize(ValueDeserializer(value))
415}
416
417struct ValueDeserializer<'de>(&'de Value);
418
419impl<'de> de::Deserializer<'de> for ValueDeserializer<'de> {
420 type Error = DeError;
421
422 fn deserialize_any<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, DeError> {
423 match self.0 {
424 Value::Str(s) => visitor.visit_borrowed_str(s),
425 Value::Int(i) => visitor.visit_i64(*i),
426 Value::Float(f) => visitor.visit_f64(*f),
427 Value::Bool(b) => visitor.visit_bool(*b),
428 Value::List(v) => visitor.visit_seq(SeqDeserializer::new(v)),
429 Value::Struct(m) => visitor.visit_map(MapDeserializer::new(m)),
430 Value::Tmpl(_) => Err(DeError(
431 "cannot deserialize a Tmpl value — templates are not data".into(),
432 )),
433 Value::None => visitor.visit_none(),
434 }
435 }
436
437 fn deserialize_bool<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, DeError> {
438 match self.0 {
439 Value::Bool(b) => visitor.visit_bool(*b),
440 other => Err(DeError(format!("expected bool, got {}", other.type_name()))),
441 }
442 }
443
444 fn deserialize_i64<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, DeError> {
445 match self.0 {
446 Value::Int(i) => visitor.visit_i64(*i),
447 other => Err(DeError(format!("expected int, got {}", other.type_name()))),
448 }
449 }
450
451 fn deserialize_f64<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, DeError> {
452 match self.0 {
453 Value::Float(f) => visitor.visit_f64(*f),
454 other => Err(DeError(format!(
455 "expected float, got {}",
456 other.type_name()
457 ))),
458 }
459 }
460
461 fn deserialize_str<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, DeError> {
462 match self.0 {
463 Value::Str(s) => visitor.visit_borrowed_str(s),
464 other => Err(DeError(format!("expected str, got {}", other.type_name()))),
465 }
466 }
467
468 fn deserialize_string<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, DeError> {
469 self.deserialize_str(visitor)
470 }
471
472 fn deserialize_seq<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, DeError> {
473 match self.0 {
474 Value::List(v) => visitor.visit_seq(SeqDeserializer::new(v)),
475 other => Err(DeError(format!("expected list, got {}", other.type_name()))),
476 }
477 }
478
479 fn deserialize_map<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, DeError> {
480 match self.0 {
481 Value::Struct(m) => visitor.visit_map(MapDeserializer::new(m)),
482 other => Err(DeError(format!("expected dict, got {}", other.type_name()))),
483 }
484 }
485
486 fn deserialize_struct<V: de::Visitor<'de>>(
487 self,
488 _name: &'static str,
489 _fields: &'static [&'static str],
490 visitor: V,
491 ) -> Result<V::Value, DeError> {
492 self.deserialize_map(visitor)
493 }
494
495 fn deserialize_enum<V: de::Visitor<'de>>(
496 self,
497 _name: &'static str,
498 _variants: &'static [&'static str],
499 visitor: V,
500 ) -> Result<V::Value, DeError> {
501 match self.0 {
502 Value::Str(s) => visitor.visit_enum(EnumDeserializer::Unit(s)),
504 Value::Struct(m) => {
506 let tag_key = crate::consts::ENUM_TAG_KEY;
507 let tag = match m.get(tag_key) {
508 Some(Value::Str(s)) => s.as_str(),
509 _ => {
510 return Err(DeError(format!(
511 "enum dict missing '{tag_key}' string field"
512 )));
513 }
514 };
515 visitor.visit_enum(EnumDeserializer::Struct { tag, fields: m })
516 }
517 other => Err(DeError(format!(
518 "expected str or dict for enum, got {}",
519 other.type_name()
520 ))),
521 }
522 }
523
524 fn deserialize_option<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, DeError> {
525 match self.0 {
526 Value::None => visitor.visit_none(),
527 Value::Str(s) if s.is_empty() || s == crate::consts::OPTION_NONE => {
529 visitor.visit_none()
530 }
531 _ => visitor.visit_some(self),
532 }
533 }
534
535 fn deserialize_newtype_struct<V: de::Visitor<'de>>(
536 self,
537 _name: &'static str,
538 visitor: V,
539 ) -> Result<V::Value, DeError> {
540 visitor.visit_newtype_struct(self)
541 }
542
543 fn deserialize_unit<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, DeError> {
544 visitor.visit_unit()
545 }
546
547 fn deserialize_unit_struct<V: de::Visitor<'de>>(
548 self,
549 _name: &'static str,
550 visitor: V,
551 ) -> Result<V::Value, DeError> {
552 visitor.visit_unit()
553 }
554
555 fn deserialize_tuple<V: de::Visitor<'de>>(
556 self,
557 _len: usize,
558 visitor: V,
559 ) -> Result<V::Value, DeError> {
560 self.deserialize_seq(visitor)
561 }
562
563 fn deserialize_tuple_struct<V: de::Visitor<'de>>(
564 self,
565 _name: &'static str,
566 _len: usize,
567 visitor: V,
568 ) -> Result<V::Value, DeError> {
569 self.deserialize_seq(visitor)
570 }
571
572 fn deserialize_identifier<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, DeError> {
573 self.deserialize_str(visitor)
574 }
575
576 fn deserialize_ignored_any<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value, DeError> {
577 visitor.visit_unit()
578 }
579
580 serde::forward_to_deserialize_any! { i8 i16 i32 u8 u16 u32 u64 f32 char bytes byte_buf }
582}
583
584struct SeqDeserializer<'de> {
587 iter: core::slice::Iter<'de, Value>,
588}
589
590impl<'de> SeqDeserializer<'de> {
591 fn new(v: &'de [Value]) -> Self {
592 Self { iter: v.iter() }
593 }
594}
595
596impl<'de> de::SeqAccess<'de> for SeqDeserializer<'de> {
597 type Error = DeError;
598 fn next_element_seed<T: de::DeserializeSeed<'de>>(
599 &mut self,
600 seed: T,
601 ) -> Result<Option<T::Value>, DeError> {
602 match self.iter.next() {
603 Some(val) => seed.deserialize(ValueDeserializer(val)).map(Some),
604 None => Ok(None),
605 }
606 }
607}
608
609struct MapDeserializer<'de> {
612 iter: crate::compat::hash_map::Iter<'de, String, Value>,
613 current_value: Option<&'de Value>,
614}
615
616impl<'de> MapDeserializer<'de> {
617 fn new(m: &'de HashMap<String, Value>) -> Self {
618 Self {
619 iter: m.iter(),
620 current_value: None,
621 }
622 }
623}
624
625impl<'de> de::MapAccess<'de> for MapDeserializer<'de> {
626 type Error = DeError;
627
628 fn next_key_seed<K: de::DeserializeSeed<'de>>(
629 &mut self,
630 seed: K,
631 ) -> Result<Option<K::Value>, DeError> {
632 match self.iter.next() {
633 Some((key, val)) => {
634 self.current_value = Some(val);
635 seed.deserialize(de::value::BorrowedStrDeserializer::new(key.as_str()))
636 .map(Some)
637 }
638 None => Ok(None),
639 }
640 }
641
642 fn next_value_seed<V: de::DeserializeSeed<'de>>(
643 &mut self,
644 seed: V,
645 ) -> Result<V::Value, DeError> {
646 let val = self
647 .current_value
648 .take()
649 .ok_or_else(|| DeError("map value without key".into()))?;
650 seed.deserialize(ValueDeserializer(val))
651 }
652}
653
654enum EnumDeserializer<'de> {
657 Unit(&'de str),
658 Struct {
659 tag: &'de str,
660 fields: &'de HashMap<String, Value>,
661 },
662}
663
664impl<'de> de::EnumAccess<'de> for EnumDeserializer<'de> {
665 type Error = DeError;
666 type Variant = VariantDeserializer<'de>;
667
668 fn variant_seed<V: de::DeserializeSeed<'de>>(
669 self,
670 seed: V,
671 ) -> Result<(V::Value, Self::Variant), DeError> {
672 match self {
673 Self::Unit(tag) => {
674 let variant = seed.deserialize(de::value::BorrowedStrDeserializer::new(tag))?;
675 Ok((variant, VariantDeserializer::Unit))
676 }
677 Self::Struct { tag, fields } => {
678 let variant = seed.deserialize(de::value::BorrowedStrDeserializer::new(tag))?;
679 Ok((variant, VariantDeserializer::Struct(fields)))
680 }
681 }
682 }
683}
684
685enum VariantDeserializer<'de> {
686 Unit,
687 Struct(&'de HashMap<String, Value>),
688}
689
690impl<'de> de::VariantAccess<'de> for VariantDeserializer<'de> {
691 type Error = DeError;
692
693 fn unit_variant(self) -> Result<(), DeError> {
694 Ok(())
695 }
696
697 fn newtype_variant_seed<T: de::DeserializeSeed<'de>>(
698 self,
699 _seed: T,
700 ) -> Result<T::Value, DeError> {
701 Err(DeError(
702 "newtype variants not supported in from_value".into(),
703 ))
704 }
705
706 fn tuple_variant<V: de::Visitor<'de>>(
707 self,
708 _len: usize,
709 _visitor: V,
710 ) -> Result<V::Value, DeError> {
711 Err(DeError("tuple variants not supported in from_value".into()))
712 }
713
714 fn struct_variant<V: de::Visitor<'de>>(
715 self,
716 _fields: &'static [&'static str],
717 visitor: V,
718 ) -> Result<V::Value, DeError> {
719 match self {
720 Self::Struct(fields) => {
721 visitor.visit_map(FilteredMapDeserializer::new(fields))
723 }
724 Self::Unit => Err(DeError("expected struct variant, got unit".into())),
725 }
726 }
727}
728
729struct FilteredMapDeserializer<'de> {
732 iter: crate::compat::hash_map::Iter<'de, String, Value>,
733 current_value: Option<&'de Value>,
734}
735
736impl<'de> FilteredMapDeserializer<'de> {
737 fn new(m: &'de HashMap<String, Value>) -> Self {
738 Self {
739 iter: m.iter(),
740 current_value: None,
741 }
742 }
743}
744
745impl<'de> de::MapAccess<'de> for FilteredMapDeserializer<'de> {
746 type Error = DeError;
747
748 fn next_key_seed<K: de::DeserializeSeed<'de>>(
749 &mut self,
750 seed: K,
751 ) -> Result<Option<K::Value>, DeError> {
752 loop {
753 match self.iter.next() {
754 Some((key, val)) => {
755 if key == crate::consts::ENUM_TAG_KEY {
756 continue; }
758 self.current_value = Some(val);
759 return seed
760 .deserialize(de::value::BorrowedStrDeserializer::new(key.as_str()))
761 .map(Some);
762 }
763 None => return Ok(None),
764 }
765 }
766 }
767
768 fn next_value_seed<V: de::DeserializeSeed<'de>>(
769 &mut self,
770 seed: V,
771 ) -> Result<V::Value, DeError> {
772 let val = self
773 .current_value
774 .take()
775 .ok_or_else(|| DeError("map value without key".into()))?;
776 seed.deserialize(ValueDeserializer(val))
777 }
778}
779
780impl<'de> serde::Deserialize<'de> for Value {
785 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
786 where
787 D: serde::Deserializer<'de>,
788 {
789 struct ValueVisitor;
790 impl<'de> serde::de::Visitor<'de> for ValueVisitor {
791 type Value = Value;
792 fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
793 formatter.write_str("any valid template value")
794 }
795 fn visit_bool<E: serde::de::Error>(self, v: bool) -> Result<Self::Value, E> {
796 Ok(Value::Bool(v))
797 }
798 fn visit_i64<E: serde::de::Error>(self, v: i64) -> Result<Self::Value, E> {
799 Ok(Value::Int(v))
800 }
801 fn visit_u64<E: serde::de::Error>(self, v: u64) -> Result<Self::Value, E> {
802 Ok(Value::Int(v.try_into().map_err(serde::de::Error::custom)?))
803 }
804 fn visit_f64<E: serde::de::Error>(self, v: f64) -> Result<Self::Value, E> {
805 Ok(Value::Float(v))
806 }
807 fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Self::Value, E> {
808 Ok(Value::Str(v.into()))
809 }
810 fn visit_string<E: serde::de::Error>(self, v: String) -> Result<Self::Value, E> {
811 Ok(Value::Str(v))
812 }
813 fn visit_seq<A: serde::de::SeqAccess<'de>>(
814 self,
815 mut seq: A,
816 ) -> Result<Self::Value, A::Error> {
817 let mut vec = Vec::with_capacity(seq.size_hint().unwrap_or(0));
818 while let Some(elem) = seq.next_element()? {
819 vec.push(elem);
820 }
821 Ok(Value::List(Arc::new(vec)))
822 }
823 fn visit_map<A: serde::de::MapAccess<'de>>(
824 self,
825 mut map: A,
826 ) -> Result<Self::Value, A::Error> {
827 let mut hashmap =
828 crate::compat::HashMap::with_capacity(map.size_hint().unwrap_or(0));
829 while let Some((key, value)) = map.next_entry::<String, Value>()? {
830 hashmap.insert(key, value);
831 }
832 Ok(Value::Struct(Arc::new(hashmap)))
833 }
834 fn visit_unit<E: serde::de::Error>(self) -> Result<Self::Value, E> {
835 Ok(Value::None)
837 }
838 fn visit_none<E: serde::de::Error>(self) -> Result<Self::Value, E> {
839 Ok(Value::None)
841 }
842 }
843 deserializer.deserialize_any(ValueVisitor)
844 }
845}
846
847#[cfg(test)]
852mod tests {
853 use serde::{Deserialize, Serialize};
854
855 use super::*;
856
857 #[test]
858 fn struct_to_dict() {
859 #[derive(Serialize)]
860 struct Agent {
861 name: String,
862 score: i64,
863 }
864 let agent = Agent {
865 name: "Alice".into(),
866 score: 95,
867 };
868 let val = to_value(&agent).unwrap();
869 assert_eq!(val.get_field("name").unwrap().to_string(), "Alice");
870 assert_eq!(*val.get_field("score").unwrap(), Value::Int(95));
871 }
872
873 #[test]
874 fn vec_to_list() {
875 let items = vec!["alpha", "beta", "gamma"];
876 let val = to_value(&items).unwrap();
877 match &val {
878 Value::List(v) => assert_eq!(v.len(), 3),
879 other => panic!("expected List, got {}", other.type_name()),
880 }
881 }
882
883 #[test]
884 fn nested_structs() {
885 #[derive(Serialize)]
886 struct Inner {
887 label: String,
888 }
889 #[derive(Serialize)]
890 struct Outer {
891 items: Vec<Inner>,
892 active: bool,
893 }
894 let data = Outer {
895 items: vec![Inner { label: "a".into() }, Inner { label: "b".into() }],
896 active: true,
897 };
898 let val = to_value(&data).unwrap();
899 assert_eq!(*val.get_field("active").unwrap(), Value::Bool(true));
900 match val.get_field("items").unwrap() {
901 Value::List(v) => assert_eq!(v.len(), 2),
902 other => panic!("expected List, got {}", other.type_name()),
903 }
904 }
905
906 #[test]
907 fn hashmap_to_dict() {
908 let mut map = HashMap::new();
909 map.insert("key".to_string(), 42_i64);
910 let val = to_value(&map).unwrap();
911 assert_eq!(*val.get_field("key").unwrap(), Value::Int(42));
912 }
913
914 #[test]
915 fn option_some() {
916 let val = to_value(&Some("hello")).unwrap();
917 assert_eq!(val, Value::Str("hello".into()));
918 }
919
920 #[test]
921 fn option_none() {
922 let val = to_value(&Option::<String>::None).unwrap();
923 assert_eq!(val, Value::None);
924 }
925
926 #[test]
927 fn enum_unit_variant() {
928 #[derive(Serialize)]
929 enum Status {
930 Active,
931 }
932 let val = to_value(&Status::Active).unwrap();
933 assert_eq!(val, Value::Str("Active".into()));
934 }
935
936 #[test]
937 fn primitives() {
938 assert_eq!(to_value(&true).unwrap(), Value::Bool(true));
939 assert_eq!(to_value(&42_i64).unwrap(), Value::Int(42));
940 assert_eq!(to_value(&2.5_f64).unwrap(), Value::Float(2.5));
941 assert_eq!(to_value(&"hello").unwrap(), Value::Str("hello".into()));
942 }
943
944 #[test]
945 fn enum_struct_variant_auto_tags() {
946 #[derive(Serialize)]
947 enum Severity {
948 Critical { reason: String },
949 High,
950 }
951
952 let val = to_value(&Severity::Critical {
954 reason: "urgent".into(),
955 })
956 .unwrap();
957 let dict = match &val {
958 Value::Struct(m) => m,
959 other => panic!("expected Struct, got {}", other.type_name()),
960 };
961 assert_eq!(
962 dict.get(crate::consts::ENUM_TAG_KEY),
963 Some(&Value::Str("Critical".into()))
964 );
965 assert_eq!(dict.get("reason"), Some(&Value::Str("urgent".into())));
966
967 let val = to_value(&Severity::High).unwrap();
969 assert_eq!(val, Value::Str("High".into()));
970 }
971
972 #[test]
975 fn from_value_struct() {
976 #[derive(Deserialize, Debug, PartialEq)]
977 struct Agent {
978 name: String,
979 score: i64,
980 }
981 let val = Value::new_struct([
982 ("name", Value::Str("Alice".into())),
983 ("score", Value::Int(95)),
984 ]);
985 let agent: Agent = from_value(&val).unwrap();
986 assert_eq!(
987 agent,
988 Agent {
989 name: "Alice".into(),
990 score: 95
991 }
992 );
993 }
994
995 #[test]
996 fn from_value_primitives() {
997 assert!(from_value::<bool>(&Value::Bool(true)).unwrap());
998 assert_eq!(from_value::<i64>(&Value::Int(42)).unwrap(), 42);
999 assert!((from_value::<f64>(&Value::Float(2.5)).unwrap() - 2.5).abs() < f64::EPSILON);
1000 assert_eq!(
1001 from_value::<String>(&Value::Str("hello".into())).unwrap(),
1002 "hello"
1003 );
1004 }
1005
1006 #[test]
1007 fn from_value_vec() {
1008 let val = Value::List(Arc::new(vec![Value::Int(1), Value::Int(2), Value::Int(3)]));
1009 let v: Vec<i64> = from_value(&val).unwrap();
1010 assert_eq!(v, vec![1, 2, 3]);
1011 }
1012
1013 #[test]
1014 fn from_value_enum_unit_variant() {
1015 #[derive(Deserialize, Debug, PartialEq)]
1016 enum Status {
1017 Active,
1018 Paused,
1019 }
1020 let val = Value::Str("Active".into());
1021 assert_eq!(from_value::<Status>(&val).unwrap(), Status::Active);
1022 }
1023
1024 #[test]
1025 fn from_value_enum_struct_variant() {
1026 #[derive(Deserialize, Debug, PartialEq)]
1027 enum Severity {
1028 Critical { reason: String },
1029 High,
1030 }
1031 let val = Value::new_struct([
1032 (crate::consts::ENUM_TAG_KEY, Value::Str("Critical".into())),
1033 ("reason", Value::Str("urgent".into())),
1034 ]);
1035 let sev: Severity = from_value(&val).unwrap();
1036 assert_eq!(
1037 sev,
1038 Severity::Critical {
1039 reason: "urgent".into()
1040 }
1041 );
1042 }
1043
1044 #[test]
1045 fn roundtrip_enum_struct_variant() {
1046 #[derive(Serialize, Deserialize, Debug, PartialEq)]
1047 enum Severity {
1048 Critical { reason: String },
1049 High,
1050 }
1051
1052 let original = Severity::Critical {
1053 reason: "critical issue".into(),
1054 };
1055 let val = to_value(&original).unwrap();
1056 let restored: Severity = from_value(&val).unwrap();
1057 assert_eq!(original, restored);
1058 }
1059
1060 #[test]
1061 fn roundtrip_enum_unit_variant() {
1062 #[derive(Serialize, Deserialize, Debug, PartialEq)]
1063 enum Severity {
1064 Critical { reason: String },
1065 High,
1066 }
1067
1068 let original = Severity::High;
1069 let val = to_value(&original).unwrap();
1070 let restored: Severity = from_value(&val).unwrap();
1071 assert_eq!(original, restored);
1072 }
1073
1074 #[test]
1075 fn roundtrip_struct() {
1076 #[derive(Serialize, Deserialize, Debug, PartialEq)]
1077 struct Agent {
1078 name: String,
1079 score: i64,
1080 active: bool,
1081 }
1082
1083 let original = Agent {
1084 name: "Bob".into(),
1085 score: 100,
1086 active: true,
1087 };
1088 let val = to_value(&original).unwrap();
1089 let restored: Agent = from_value(&val).unwrap();
1090 assert_eq!(original, restored);
1091 }
1092
1093 #[test]
1094 fn from_value_type_mismatch_error() {
1095 let result = from_value::<i64>(&Value::Str("not a number".into()));
1096 assert!(result.is_err());
1097 let err = result.unwrap_err();
1098 assert!(
1099 err.to_string().contains("expected int"),
1100 "error should mention expected type: {err}"
1101 );
1102 }
1103
1104 #[test]
1105 fn from_value_enum_missing_tag_error() {
1106 #[derive(Deserialize, Debug)]
1107 enum Status {
1108 Active,
1109 }
1110 let val = Value::new_struct([("name", Value::Str("oops".into()))]);
1112 let result = from_value::<Status>(&val);
1113 assert!(result.is_err());
1114 assert!(
1115 result
1116 .unwrap_err()
1117 .to_string()
1118 .contains(crate::consts::ENUM_TAG_KEY),
1119 "error should mention missing tag key"
1120 );
1121 }
1122
1123 #[test]
1126 fn value_rust_value_roundtrip_struct() {
1127 #[derive(Serialize, Deserialize)]
1128 struct Agent {
1129 name: String,
1130 score: i64,
1131 }
1132
1133 let original = Value::new_struct([
1134 ("name", Value::Str("Alice".into())),
1135 ("score", Value::Int(95)),
1136 ]);
1137 let agent: Agent = from_value(&original).unwrap();
1138 let restored = to_value(&agent).unwrap();
1139 assert_eq!(original, restored);
1140 }
1141
1142 #[test]
1143 fn value_rust_value_roundtrip_enum_struct() {
1144 #[derive(Serialize, Deserialize)]
1145 enum Severity {
1146 Critical { reason: String },
1147 High,
1148 }
1149
1150 let original = Value::new_struct([
1151 (crate::consts::ENUM_TAG_KEY, Value::Str("Critical".into())),
1152 ("reason", Value::Str("urgent".into())),
1153 ]);
1154 let sev: Severity = from_value(&original).unwrap();
1155 let restored = to_value(&sev).unwrap();
1156 assert_eq!(original, restored);
1157 }
1158
1159 #[test]
1160 fn value_rust_value_roundtrip_enum_unit() {
1161 #[derive(Serialize, Deserialize)]
1162 enum Severity {
1163 Critical { reason: String },
1164 High,
1165 }
1166
1167 let original = Value::Str("High".into());
1168 let sev: Severity = from_value(&original).unwrap();
1169 let restored = to_value(&sev).unwrap();
1170 assert_eq!(original, restored);
1171 }
1172
1173 #[test]
1176 fn value_from_serialize() {
1177 #[derive(Serialize)]
1178 struct Agent {
1179 name: String,
1180 }
1181 let val = Value::from_serialize(&Agent { name: "Bob".into() }).unwrap();
1182 assert_eq!(val.get_field("name"), Some(&Value::Str("Bob".into())));
1183 }
1184
1185 #[test]
1186 fn value_deserialize_into() {
1187 #[derive(Deserialize, Debug, PartialEq)]
1188 struct Agent {
1189 name: String,
1190 }
1191 let val = Value::new_struct([("name", Value::Str("Bob".into()))]);
1192 let agent: Agent = val.deserialize_into().unwrap();
1193 assert_eq!(agent, Agent { name: "Bob".into() });
1194 }
1195
1196 #[test]
1197 fn value_deserialize_into_enum() {
1198 #[derive(Deserialize, Debug, PartialEq)]
1199 enum Status {
1200 Active,
1201 Paused,
1202 }
1203 let val = Value::Str("Paused".into());
1204 let status: Status = val.deserialize_into().unwrap();
1205 assert_eq!(status, Status::Paused);
1206 }
1207}