1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use crate::{CountryEvent, CountryEvents};
use serde::{de, Deserialize, Deserializer};
use std::fmt;

impl<'de> Deserialize<'de> for CountryEvents {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct CountryEventsVisitor;

        impl<'de> de::Visitor<'de> for CountryEventsVisitor {
            type Value = CountryEvents;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("struct CountryEvents")
            }

            fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
            where
                A: de::SeqAccess<'de>,
            {
                // Hmm empty object
                let abc = seq.next_element::<String>()?;
                if abc.is_some() {
                    return Err(de::Error::custom("unexpected sequence!"));
                }

                Ok(CountryEvents(vec![]))
            }

            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
            where
                A: de::MapAccess<'de>,
            {
                let mut values = if let Some(size) = map.size_hint() {
                    Vec::with_capacity(size)
                } else {
                    Vec::new()
                };

                while let Some(key) = map.next_key::<String>()? {
                    let val = match key.as_str() {
                        "monarch" => CountryEvent::Monarch(map.next_value()?),
                        "union" => CountryEvent::Union(map.next_value()?),
                        "capital" => CountryEvent::Capital(map.next_value()?),
                        "leader" => CountryEvent::Leader(map.next_value()?),
                        "remove_accepted_culture" => {
                            CountryEvent::RemoveAcceptedCulture(map.next_value()?)
                        }
                        "changed_country_name_from" => {
                            CountryEvent::ChangedCountryNameFrom(map.next_value()?)
                        }
                        "changed_country_adjective_from" => {
                            CountryEvent::ChangedCountryAdjectiveFrom(map.next_value()?)
                        }
                        "changed_country_mapcolor_from" => {
                            CountryEvent::ChangedCountryMapColorFrom(map.next_value()?)
                        }
                        "changed_tag_from" => CountryEvent::ChangedTagFrom(map.next_value()?),
                        _ => continue, /*panic!("unknown: {}", &key)*/
                    };

                    values.push(val);
                }

                Ok(CountryEvents(values))
            }
        }

        deserializer.deserialize_map(CountryEventsVisitor)
    }
}