1use std::fmt::Display;
2
3use crate::{
4 BigDecimal, BigInt, ByteBuffer, IndexMap, Instant,
5 schema::{
6 Document, DocumentError, NULL, SchemaRef, ShapeId, ShapeType, StaticSchemaShape,
7 default::Value,
8 },
9 serde::{
10 ShapeBuilder,
11 de::Deserializer,
12 se::{ListSerializer, MapSerializer, Serializer, StructSerializer},
13 serializers::{Error, SerializeWithSchema},
14 },
15};
16impl SerializeWithSchema for Box<dyn Document> {
21 fn serialize_with_schema<S: Serializer>(
22 &self,
23 schema: &SchemaRef,
24 serializer: S,
25 ) -> Result<S::Ok, S::Error> {
26 match self.get_type() {
28 Some(ShapeType::Blob) => serializer.write_blob(schema, self.as_blob().unwrap()),
29 Some(ShapeType::Boolean) => serializer.write_boolean(schema, self.as_bool().unwrap()),
30 Some(ShapeType::String | ShapeType::Enum) => {
31 serializer.write_string(schema, self.as_string().unwrap())
32 }
33 Some(ShapeType::Timestamp) => {
34 serializer.write_timestamp(schema, self.as_timestamp().unwrap())
35 }
36 Some(ShapeType::Byte) => serializer.write_byte(schema, self.as_byte().unwrap()),
37 Some(ShapeType::Short) => serializer.write_short(schema, self.as_short().unwrap()),
38 Some(ShapeType::Integer | ShapeType::IntEnum) => {
39 serializer.write_integer(schema, self.as_integer().unwrap())
40 }
41 Some(ShapeType::Long) => serializer.write_long(schema, self.as_long().unwrap()),
42 Some(ShapeType::Float) => serializer.write_float(schema, self.as_float().unwrap()),
43 Some(ShapeType::Double) => serializer.write_double(schema, self.as_double().unwrap()),
44 Some(ShapeType::BigInteger) => {
45 serializer.write_big_integer(schema, self.as_big_integer().unwrap())
46 }
47 Some(ShapeType::BigDecimal) => {
48 serializer.write_big_decimal(schema, self.as_big_decimal().unwrap())
49 }
50 Some(ShapeType::List) => self
51 .as_list()
52 .unwrap()
53 .serialize_with_schema(schema, serializer),
54 Some(ShapeType::Map) => self
55 .as_map()
56 .unwrap()
57 .serialize_with_schema(schema, serializer),
58 Some(ShapeType::Structure | ShapeType::Union) => {
59 let document_map = self.as_map().unwrap();
60 let mut struct_serializer = serializer.write_struct(schema, self.size())?;
61 if let Some(discriminator) = &self.discriminator() {
62 struct_serializer.serialize_discriminator(discriminator)?;
63 }
64 for (key, value) in document_map {
65 if let Some(member_schema) = schema.get_member(key) {
66 struct_serializer.serialize_member(member_schema, value)?;
67 } else {
68 todo!("Add some logging on unknown members");
70 }
71 }
72 struct_serializer.end(schema)
73 }
74 None => serializer.write_null(schema),
75 _ => Err(Error::custom("Unsupported shape type")),
76 }
77 }
78}
79
80impl<T> From<T> for Box<dyn Document>
81where
82 T: StaticSchemaShape + SerializeWithSchema,
83{
84 fn from(shape: T) -> Self {
85 shape
86 .serialize_with_schema(T::schema(), DocumentParser)
87 .expect(
88 "Infallible conversion from StaticSchemaShape to Document failed - this is a bug",
89 )
90 }
91}
92
93impl dyn Document {
96 #[inline]
108 pub(crate) fn into_builder<'de, B: ShapeBuilder<'de, S>, S: StaticSchemaShape>(
109 self: Box<Self>,
110 ) -> Result<B, DocumentError> {
111 B::deserialize_with_schema(S::schema(), &mut DocumentDeserializer::new(self))
112 }
113}
114
115impl Error for DocumentError {
116 fn custom<T: Display>(msg: T) -> Self {
117 DocumentError::CustomError(msg.to_string())
118 }
119}
120
121struct DocumentParser;
122impl Serializer for DocumentParser {
124 type Error = DocumentError;
125 type Ok = Box<dyn Document>;
126 type SerializeList = DocumentListAccumulator;
127 type SerializeMap = DocumentMapAccumulator;
128 type SerializeStruct = DocumentMapAccumulator;
129
130 fn write_struct(
131 self,
132 schema: &SchemaRef,
133 len: usize,
134 ) -> Result<Self::SerializeStruct, Self::Error> {
135 Ok(DocumentMapAccumulator {
136 schema: schema.clone(),
137 values: IndexMap::with_capacity(len),
138 discriminator: Some(schema.id().clone()),
139 })
140 }
141
142 fn write_map(self, schema: &SchemaRef, len: usize) -> Result<Self::SerializeMap, Self::Error> {
143 Ok(DocumentMapAccumulator {
144 schema: schema.clone(),
145 values: IndexMap::with_capacity(len),
146 discriminator: Some(schema.id().clone()),
147 })
148 }
149
150 fn write_list(
151 self,
152 schema: &SchemaRef,
153 len: usize,
154 ) -> Result<Self::SerializeList, Self::Error> {
155 Ok(DocumentListAccumulator {
156 schema: schema.clone(),
157 values: Vec::with_capacity(len),
158 discriminator: Some(schema.id().clone()),
159 })
160 }
161
162 #[inline]
163 fn write_boolean(self, _schema: &SchemaRef, value: bool) -> Result<Self::Ok, Self::Error> {
164 Ok(value.into())
165 }
166
167 #[inline]
168 fn write_byte(self, _schema: &SchemaRef, value: i8) -> Result<Self::Ok, Self::Error> {
169 Ok(value.into())
170 }
171
172 #[inline]
173 fn write_short(self, _schema: &SchemaRef, value: i16) -> Result<Self::Ok, Self::Error> {
174 Ok(value.into())
175 }
176
177 #[inline]
178 fn write_integer(self, _schema: &SchemaRef, value: i32) -> Result<Self::Ok, Self::Error> {
179 Ok(value.into())
180 }
181
182 #[inline]
183 fn write_long(self, _schema: &SchemaRef, value: i64) -> Result<Self::Ok, Self::Error> {
184 Ok(value.into())
185 }
186
187 #[inline]
188 fn write_float(self, _schema: &SchemaRef, value: f32) -> Result<Self::Ok, Self::Error> {
189 Ok(value.into())
190 }
191
192 #[inline]
193 fn write_double(self, _schema: &SchemaRef, value: f64) -> Result<Self::Ok, Self::Error> {
194 Ok(value.into())
195 }
196
197 #[inline]
198 fn write_big_integer(
199 self,
200 _schema: &SchemaRef,
201 value: &BigInt,
202 ) -> Result<Self::Ok, Self::Error> {
203 Ok(value.clone().into())
204 }
205
206 #[inline]
207 fn write_big_decimal(
208 self,
209 _schema: &SchemaRef,
210 value: &BigDecimal,
211 ) -> Result<Self::Ok, Self::Error> {
212 Ok(value.clone().into())
213 }
214
215 #[inline]
216 fn write_string(self, _schema: &SchemaRef, value: &str) -> Result<Self::Ok, Self::Error> {
217 Ok(value.into())
218 }
219
220 #[inline]
221 fn write_blob(self, _schema: &SchemaRef, value: &ByteBuffer) -> Result<Self::Ok, Self::Error> {
222 Ok(value.clone().into())
223 }
224
225 #[inline]
226 fn write_timestamp(
227 self,
228 _schema: &SchemaRef,
229 value: &Instant,
230 ) -> Result<Self::Ok, Self::Error> {
231 Ok(value.into())
232 }
233
234 #[inline]
235 fn write_document(
236 self,
237 _schema: &SchemaRef,
238 value: &Box<dyn Document>,
239 ) -> Result<Self::Ok, Self::Error> {
240 Ok(value.clone())
241 }
242
243 #[inline]
244 fn write_null(self, _schema: &SchemaRef) -> Result<Self::Ok, Self::Error> {
245 Ok(NULL.clone())
246 }
247
248 #[inline]
249 fn skip(self, _schema: &SchemaRef) -> Result<Self::Ok, Self::Error> {
250 Ok(NULL.clone())
252 }
253}
254
255#[doc(hidden)]
256pub struct DocumentListAccumulator {
257 schema: SchemaRef,
258 values: Vec<Box<dyn Document>>,
259 discriminator: Option<ShapeId>,
260}
261impl ListSerializer for DocumentListAccumulator {
262 type Error = DocumentError;
263 type Ok = Box<dyn Document>;
264
265 fn serialize_element<T>(
266 &mut self,
267 element_schema: &SchemaRef,
268 value: &T,
269 ) -> Result<(), Self::Error>
270 where
271 T: SerializeWithSchema,
272 {
273 let el = value.serialize_with_schema(element_schema, DocumentParser)?;
274 self.values.push(el);
275 Ok(())
276 }
277
278 #[inline]
279 fn end(self, _schema: &SchemaRef) -> Result<Self::Ok, Self::Error> {
280 Ok(crate::schema::default::Document {
281 schema: self.schema,
282 value: Value::List(self.values),
283 discriminator: self.discriminator,
284 }
285 .into())
286 }
287}
288
289#[doc(hidden)]
290pub struct DocumentMapAccumulator {
291 schema: SchemaRef,
292 values: IndexMap<String, Box<dyn Document>>,
293 discriminator: Option<ShapeId>,
294}
295impl MapSerializer for DocumentMapAccumulator {
296 type Error = DocumentError;
297 type Ok = Box<dyn Document>;
298
299 fn serialize_entry<K, V>(
300 &mut self,
301 key_schema: &SchemaRef,
302 value_schema: &SchemaRef,
303 key: &K,
304 value: &V,
305 ) -> Result<(), Self::Error>
306 where
307 K: SerializeWithSchema,
308 V: SerializeWithSchema,
309 {
310 let key_doc = key.serialize_with_schema(key_schema, DocumentParser)?;
312 let key_str = key_doc
313 .as_string()
314 .ok_or_else(|| {
315 DocumentError::DocumentConversion("Map key must be a string".to_string())
316 })?
317 .to_string();
318
319 let val = value.serialize_with_schema(value_schema, DocumentParser)?;
320 self.values.insert(key_str, val);
321 Ok(())
322 }
323
324 #[inline]
325 fn end(self, _schema: &SchemaRef) -> Result<Self::Ok, Self::Error> {
326 Ok(crate::schema::default::Document {
327 schema: self.schema,
328 value: Value::Map(self.values),
329 discriminator: self.discriminator,
330 }
331 .into())
332 }
333}
334
335impl StructSerializer for DocumentMapAccumulator {
336 type Error = DocumentError;
337 type Ok = Box<dyn Document>;
338
339 fn serialize_member<T>(
340 &mut self,
341 member_schema: &SchemaRef,
342 value: &T,
343 ) -> Result<(), Self::Error>
344 where
345 T: SerializeWithSchema,
346 {
347 let Some(me) = member_schema.as_member() else {
348 return Err(DocumentError::DocumentConversion(
349 "Expected member schema!".to_string(),
350 ));
351 };
352 let val = value.serialize_with_schema(member_schema, DocumentParser)?;
353 self.values.insert(me.name.clone(), val);
354 Ok(())
355 }
356
357 #[inline]
358 fn end(self, _schema: &SchemaRef) -> Result<Self::Ok, Self::Error> {
359 Ok(crate::schema::default::Document {
360 schema: self.schema,
361 value: Value::Map(self.values),
362 discriminator: self.discriminator,
363 }
364 .into())
365 }
366}
367
368struct DocumentDeserializer {
374 document: Option<Box<dyn Document>>,
375}
376
377impl DocumentDeserializer {
378 pub fn new(document: Box<dyn Document>) -> Self {
379 Self {
380 document: Some(document),
381 }
382 }
383
384 #[inline]
385 fn get_inner<T: TryFrom<Box<dyn Document>, Error = DocumentError>>(
386 &mut self,
387 ) -> Result<T, DocumentError> {
388 self.document
389 .take()
390 .ok_or_else(|| {
391 DocumentError::DocumentConversion(
392 "Encountered empty document deserializer".to_string(),
393 )
394 })?
395 .try_into()
396 }
397}
398
399impl Deserializer<'_> for DocumentDeserializer {
400 type Error = DocumentError;
401
402 #[inline]
403 fn read_bool(&mut self, _schema: &SchemaRef) -> Result<bool, Self::Error> {
404 self.get_inner()
405 }
406
407 #[inline]
408 fn read_byte(&mut self, _schema: &SchemaRef) -> Result<i8, Self::Error> {
409 self.get_inner()
410 }
411
412 #[inline]
413 fn read_short(&mut self, _schema: &SchemaRef) -> Result<i16, Self::Error> {
414 self.get_inner()
415 }
416
417 #[inline]
418 fn read_integer(&mut self, _schema: &SchemaRef) -> Result<i32, Self::Error> {
419 self.get_inner()
420 }
421
422 #[inline]
423 fn read_long(&mut self, _schema: &SchemaRef) -> Result<i64, Self::Error> {
424 self.get_inner()
425 }
426
427 #[inline]
428 fn read_float(&mut self, _schema: &SchemaRef) -> Result<f32, Self::Error> {
429 self.get_inner()
430 }
431
432 #[inline]
433 fn read_double(&mut self, _schema: &SchemaRef) -> Result<f64, Self::Error> {
434 self.get_inner()
435 }
436
437 #[inline]
438 fn read_big_integer(&mut self, _schema: &SchemaRef) -> Result<BigInt, Self::Error> {
439 self.get_inner()
440 }
441
442 #[inline]
443 fn read_big_decimal(&mut self, _schema: &SchemaRef) -> Result<BigDecimal, Self::Error> {
444 self.get_inner()
445 }
446
447 #[inline]
448 fn read_string(&mut self, _schema: &SchemaRef) -> Result<String, Self::Error> {
449 self.get_inner()
450 }
451
452 #[inline]
453 fn read_blob(&mut self, _schema: &SchemaRef) -> Result<ByteBuffer, Self::Error> {
454 self.get_inner()
455 }
456
457 #[inline]
458 fn read_timestamp(&mut self, _schema: &SchemaRef) -> Result<Instant, Self::Error> {
459 self.get_inner()
460 }
461
462 #[inline]
463 fn read_document(&mut self, _schema: &SchemaRef) -> Result<Box<dyn Document>, Self::Error> {
464 self.document.take().ok_or_else(|| {
465 DocumentError::DocumentConversion("Encountered empty document deserializer".to_string())
466 })
467 }
468
469 fn read_struct<B, F>(
470 &mut self,
471 schema: &SchemaRef,
472 mut builder: B,
473 mut consumer: F,
474 ) -> Result<B, Self::Error>
475 where
476 F: FnMut(B, &SchemaRef, &mut Self) -> Result<B, Self::Error>,
477 {
478 let map: IndexMap<String, Box<dyn Document>> = self.get_inner()?;
479
480 for (key, value) in map {
483 if let Some(member_schema) = schema.members().get(&key) {
484 let mut field_deser = DocumentDeserializer::new(value);
485 builder = consumer(builder, member_schema, &mut field_deser)?;
486 }
487 }
488
489 Ok(builder)
490 }
491
492 fn read_list<T, F>(
493 &mut self,
494 schema: &SchemaRef,
495 state: &mut T,
496 mut consumer: F,
497 ) -> Result<(), Self::Error>
498 where
499 F: FnMut(&mut T, &SchemaRef, &mut Self) -> Result<(), Self::Error>,
500 {
501 let list: Vec<Box<dyn Document>> = self.get_inner()?;
502
503 let member_schema = schema.get_member("member").ok_or_else(|| {
504 DocumentError::DocumentConversion("List missing member schema".to_string())
505 })?;
506
507 for element_doc in list {
508 let mut elem_deser = DocumentDeserializer::new(element_doc);
509 consumer(state, member_schema, &mut elem_deser)?;
510 }
511
512 Ok(())
513 }
514
515 fn read_map<T, F>(
516 &mut self,
517 _schema: &SchemaRef,
518 state: &mut T,
519 mut consumer: F,
520 ) -> Result<(), Self::Error>
521 where
522 F: FnMut(&mut T, String, &mut Self) -> Result<(), Self::Error>,
523 {
524 let map: IndexMap<String, Box<dyn Document>> = self.get_inner()?;
525 for (key_str, value_doc) in map {
526 let mut value_deser = DocumentDeserializer::new(value_doc);
528 consumer(state, key_str.clone(), &mut value_deser)?;
529 }
530 Ok(())
531 }
532
533 fn is_null(&mut self) -> bool {
534 self.document
535 .as_ref()
536 .expect("Empty document deserializer")
537 .is_null()
538 }
539
540 fn read_null(&mut self) -> Result<(), Self::Error> {
541 if self.is_null() {
542 Ok(())
543 } else {
544 Err(DocumentError::DocumentConversion(
545 "Expected null document".to_string(),
546 ))
547 }
548 }
549}
550
551#[cfg(test)]
552mod tests {
553 use std::str::FromStr;
554
555 use super::*;
556 use crate::{derive::SmithyShape, schema::prelude::*, smithy};
557
558 smithy!("com.example#Map": {
559 map MAP_SCHEMA {
560 key: STRING
561 value: STRING
562 }
563 });
564 smithy!("com.example#List": {
565 list LIST_SCHEMA {
566 member: STRING
567 }
568 });
569 smithy!("com.example#Shape": {
570 structure SCHEMA {
571 A: STRING = "a"
572 B: STRING = "b"
573 C: STRING = "c"
574 LIST: LIST_SCHEMA = "list"
575 MAP: MAP_SCHEMA = "map"
576 }
577 });
578
579 #[derive(SmithyShape, Clone, PartialEq)]
580 #[smithy_schema(SCHEMA)]
581 pub struct SerializeMe {
582 #[smithy_schema(A)]
583 pub member_a: String,
584 #[smithy_schema(B)]
585 pub member_b: String,
586 #[smithy_schema(C)]
587 pub member_optional: Option<String>,
588 #[smithy_schema(LIST)]
589 pub member_list: Vec<String>,
590 #[smithy_schema(MAP)]
591 pub member_map: IndexMap<String, String>,
592 }
593
594 #[test]
595 fn struct_to_document() {
596 let mut map = IndexMap::new();
597 map.insert(String::from("a"), String::from("b"));
598 let list = vec!["a".to_string(), "b".to_string()];
599 let struct_to_convert = SerializeMe {
600 member_a: "a".to_string(),
601 member_b: "b".to_string(),
602 member_optional: Some("c".to_string()),
603 member_map: map,
604 member_list: list,
605 };
606 let document: Box<dyn Document> = struct_to_convert.into();
607 assert_eq!(document.discriminator().unwrap(), SCHEMA.id());
608 if let Some(members) = document.as_map() {
609 let doc_a = &members.get("a").unwrap();
610 assert_eq!(doc_a.as_string().unwrap(), "a");
611 let doc_b = &members.get("b").unwrap();
612 assert_eq!(doc_b.as_string().unwrap(), "b");
613 let doc_c = &members.get("c").unwrap();
614 assert_eq!(doc_c.as_string().unwrap(), "c");
615 assert!(members.contains_key("map"));
616 assert!(members.contains_key("list"));
617 } else {
618 panic!("Expected document");
619 }
620 }
621
622 #[test]
623 fn string_document_value() {
624 let document_str: Box<dyn Document> = "MyStr".into();
625 let output_str = document_str.as_string().expect("string");
626 assert_eq!(output_str, &"MyStr".to_string());
627 let val: &SchemaRef = &STRING;
628 assert_eq!(document_str.schema(), val);
629 }
630
631 #[test]
632 #[ignore]
633 fn number_document_values() {
634 }
636
637 #[test]
640 fn roundtrip_shape() {
641 let mut map = IndexMap::new();
642 map.insert(String::from("a"), String::from("b"));
643 let list = vec!["a".to_string(), "b".to_string()];
644 let struct_to_convert = SerializeMe {
645 member_a: "a".to_string(),
646 member_b: "b".to_string(),
647 member_optional: Some("c".to_string()),
648 member_map: map,
649 member_list: list,
650 };
651 let document: Box<dyn Document> = struct_to_convert.clone().into();
652 let builder: SerializeMeBuilder = document.into_builder().unwrap();
653 let result: SerializeMe = builder.build().unwrap();
654 assert_eq!(result, struct_to_convert);
655 }
656
657 #[test]
658 fn roundtrip_bool() {
659 let original = true;
660 let doc: Box<dyn Document> = original.into();
661 let result: bool = doc.try_into().unwrap();
662 assert_eq!(original, result);
663 }
664
665 #[test]
666 fn roundtrip_string() {
667 let doc: Box<dyn Document> = "hello world".into();
668 let result: String = doc.try_into().unwrap();
669 assert_eq!("hello world".to_string(), result);
670 }
671
672 #[test]
673 fn roundtrip_integers() {
674 let original_byte = 127i8;
676 let doc: Box<dyn Document> = original_byte.into();
677 let result: i8 = doc.try_into().unwrap();
678 assert_eq!(127i8, result);
679
680 let original_short = 32000i16;
682 let doc: Box<dyn Document> = original_short.into();
683 let result: i16 = doc.try_into().unwrap();
684 assert_eq!(original_short, result);
685
686 let original_int = 123_456_i32;
688 let doc: Box<dyn Document> = original_int.into();
689 let result: i32 = doc.try_into().unwrap();
690 assert_eq!(original_int, result);
691
692 let original_long = 9_876_543_210_i64;
694 let doc: Box<dyn Document> = original_long.into();
695 let result: i64 = doc.try_into().unwrap();
696 assert_eq!(original_long, result);
697 }
698
699 #[test]
700 fn roundtrip_floats() {
701 let original_float = 1.2345f32;
703 let doc: Box<dyn Document> = original_float.into();
704 let result: f32 = doc.try_into().unwrap();
705 assert_eq!(original_float, result);
706
707 let original_double = 1.23456789f64;
709 let doc: Box<dyn Document> = original_double.into();
710 let result: f64 = doc.try_into().unwrap();
711 assert_eq!(original_double, result);
712 }
713
714 #[test]
715 #[ignore = "BigDecimal/BigInteger serialization not yet implemented"]
716 fn roundtrip_big_numbers() {
717 let original_big_int = BigInt::from(123_456_789);
719 let doc: Box<dyn Document> = original_big_int.clone().into();
720 let result: BigInt = doc.try_into().unwrap();
721 assert_eq!(original_big_int, result);
722
723 let original_big_dec = BigDecimal::from_str("123.456").unwrap();
725 let doc: Box<dyn Document> = original_big_dec.clone().into();
726 let result: BigDecimal = doc.try_into().unwrap();
727 assert_eq!(original_big_dec, result);
728 }
729
730 #[test]
731 fn roundtrip_list() {
732 let original: Vec<String> = vec![
733 "first".to_string(),
734 "second".to_string(),
735 "third".to_string(),
736 ];
737 let doc: Box<dyn Document> = original.clone().into();
738 let result: Vec<String> = doc.try_into().unwrap();
739 assert_eq!(original, result);
740 }
741
742 #[test]
743 fn roundtrip_map() {
744 let mut original = IndexMap::new();
745 original.insert("key1".to_string(), "value1".to_string());
746 original.insert("key2".to_string(), "value2".to_string());
747 original.insert("key3".to_string(), "value3".to_string());
748
749 let doc: Box<dyn Document> = original.clone().into();
750 let result: IndexMap<String, String> = doc.try_into().unwrap();
751 assert_eq!(original, result);
752 }
753
754 #[test]
755 fn roundtrip_struct() {
756 let mut original_map = IndexMap::new();
757 original_map.insert("mapkey1".to_string(), "mapvalue1".to_string());
758 original_map.insert("mapkey2".to_string(), "mapvalue2".to_string());
759
760 let original_list = vec![
761 "listitem1".to_string(),
762 "listitem2".to_string(),
763 "listitem3".to_string(),
764 ];
765
766 let original = SerializeMe {
767 member_a: "value_a".to_string(),
768 member_b: "value_b".to_string(),
769 member_optional: Some("value_c".to_string()),
770 member_map: original_map.clone(),
771 member_list: original_list.clone(),
772 };
773
774 let doc: Box<dyn Document> = original.into();
776
777 let doc_map = doc.as_map().expect("Should be a map");
779
780 let member_a_doc = doc_map.get("a").expect("Should have member a").clone();
782 let a_value: String = member_a_doc.try_into().unwrap();
783 assert_eq!(a_value, "value_a".to_string());
784
785 let member_b_doc = doc_map.get("b").expect("Should have member b").clone();
787 let b_value: String = member_b_doc.try_into().unwrap();
788 assert_eq!(b_value, "value_b");
789
790 let member_c_doc = doc_map.get("c").expect("Should have member c").clone();
792 let c_value: String = member_c_doc.try_into().unwrap();
793 assert_eq!(c_value, "value_c");
794
795 let list_doc = doc_map.get("list").expect("Should have list").clone();
797 let list_value: Vec<String> = list_doc.try_into().unwrap();
798 assert_eq!(list_value, original_list);
799
800 let map_doc = doc_map.get("map").expect("Should have map").clone();
802 let map_value: IndexMap<String, String> = map_doc.try_into().unwrap();
803 assert_eq!(map_value, original_map);
804 }
805
806 #[test]
807 fn roundtrip_option() {
808 let original_some: Option<String> = Some("test".to_string());
810 let doc: Box<dyn Document> = original_some.clone().into();
811 let result: Option<String> = doc.try_into().unwrap();
812 assert_eq!(original_some, result);
813
814 let original_none: Option<String> = None;
816 let doc: Box<dyn Document> = original_none.clone().into();
817 let result: Option<String> = doc.try_into().unwrap();
818 assert_eq!(original_none, result);
819 }
820}