1
use std::any::Any;
2

            
3
use serde::{
4
    de::{MapAccess, Visitor},
5
    Deserializer,
6
};
7

            
8
#[test]
9
4
fn manually_deserialize_dyn() {
10
4
    let ron = r#"SerializeDyn(
11
4
        type: "engine_utils::types::registry::tests::Player",
12
4
    )"#;
13
4

            
14
4
    let mut de = ron::Deserializer::from_bytes(ron.as_bytes()).unwrap();
15
4

            
16
4
    let result = de
17
4
        .deserialize_struct("SerializeDyn", &["type"], SerializeDynVisitor)
18
4
        .unwrap();
19
4

            
20
4
    assert_eq!(
21
4
        *result.downcast::<Option<(String, String)>>().unwrap(),
22
4
        Some((
23
4
            String::from("type"),
24
4
            String::from("engine_utils::types::registry::tests::Player")
25
4
        ))
26
4
    );
27
4
}
28

            
29
struct SerializeDynVisitor;
30

            
31
impl<'de> Visitor<'de> for SerializeDynVisitor {
32
    type Value = Box<dyn Any>;
33

            
34
    // GRCOV_EXCL_START
35
    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
36
        write!(formatter, "a serialize dyn struct")
37
    }
38
    // GRCOV_EXCL_STOP
39

            
40
4
    fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
41
4
        let entry = map.next_entry::<&str, String>()?;
42

            
43
4
        Ok(Box::new(entry.map(|(k, v)| (String::from(k), v))))
44
4
    }
45
}