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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use super::*;
use serde::{
    de::{Deserialize, Deserializer, Error, SeqAccess, Visitor},
    ser::{Serialize, SerializeSeq, Serializer},
};

impl Serialize for Bins {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let strings: Vec<&'static str> = string_cache_iter().collect();
        let mut seq = serializer.serialize_seq(Some(strings.len()))?;
        for s in strings {
            match seq.serialize_element(s) {
                Ok(_) => (),
                Err(e) => {
                    panic!("Error serializing \"{}\": {}", s, e);
                }
            }
        }
        seq.end()
    }
}

pub struct BinsVisitor {}

impl BinsVisitor {
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        BinsVisitor {}
    }
}

impl<'de> Visitor<'de> for BinsVisitor {
    type Value = DeserializedCache;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a sequence of strings")
    }

    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        while let Some(s) = seq.next_element::<String>()? {
            ustr(&s);
        }

        Ok(DeserializedCache {})
    }
}

pub struct DeserializedCache {}

impl<'de> Deserialize<'de> for DeserializedCache {
    fn deserialize<D>(deserializer: D) -> Result<DeserializedCache, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_seq(BinsVisitor::new())
    }
}

pub struct UstrVisitor {}
impl UstrVisitor {
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        UstrVisitor {}
    }
}

impl<'de> Visitor<'de> for UstrVisitor {
    type Value = Ustr;

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

    fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
    where
        E: Error,
    {
        Ok(Ustr::from(s))
    }
}

impl<'de> Deserialize<'de> for Ustr {
    fn deserialize<D>(deserializer: D) -> Result<Ustr, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_str(UstrVisitor::new())
    }
}

impl Serialize for Ustr {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}