summaryrefslogtreecommitdiffstats
path: root/lib/src/de.rs
blob: d1673d56d83d37684756f66bc6b99673e1ce5239 (plain)
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
105
106
107
108
109
use serde::de;
use value::{Value, ValueKind};
use error::*;
use std::borrow::Cow;
use std::iter::Peekable;
use std::collections::HashMap;
use std::collections::hash_map::Drain;

impl de::Deserializer for Value {
    type Error = ConfigError;

    #[inline]
    fn deserialize<V: de::Visitor>(self, visitor: V) -> Result<V::Value> {
        // Deserialize based on the underlying type
        match self.kind {
            ValueKind::Integer(i) => visitor.visit_i64(i),
            ValueKind::Boolean(b) => visitor.visit_bool(b),
            ValueKind::Float(f) => visitor.visit_f64(f),
            // ValueKind::String(Cow::Borrowed(s)) => visitor.visit_str(s),
            // ValueKind::String(Cow::Owned(s)) => visitor.visit_string(s),
            ValueKind::Table(map) => visitor.visit_map(MapVisitor::new(map)),
            _ => { unimplemented!(); }
        }
    }

    forward_to_deserialize! {
        bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
        seq_fixed_size bytes byte_buf map struct unit enum newtype_struct
        struct_field ignored_any unit_struct tuple_struct tuple option
    }
}

struct StrDeserializer<'a>(&'a str);

impl<'a> de::Deserializer for StrDeserializer<'a> {
    type Error = ConfigError;

    #[inline]
    fn deserialize<V: de::Visitor>(self, visitor: V) -> Result<V::Value> {
        visitor.visit_str(self.0)
    }

    forward_to_deserialize! {
        bool
        u8
        u16
        u32
        u64
        i8
        i16
        i32
        i64
        f32
        f64
        char
        str
        string
        bytes
        byte_buf
        option
        unit
        unit_struct
        newtype_struct
        seq
        seq_fixed_size
        tuple
        tuple_struct
        map
        struct
        struct_field
        enum
        ignored_any
    }
}

struct MapVisitor {
    elements: Vec<(String, Value)>,
    index: usize,
}

impl MapVisitor {
    fn new(mut table: HashMap<String, Value>) -> MapVisitor {
        MapVisitor { elements: table.drain().collect(), index: 0 }
    }
}

impl de::MapVisitor for MapVisitor {
    type Error = ConfigError;

    fn visit_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
        where K: de::DeserializeSeed,
    {
        if self.index >= self.elements.len() {
            return Ok(None);
        }

        let ref key_s = self.elements[0].0;
        let key_de = StrDeserializer(&key_s);
        let key = de::DeserializeSeed::deserialize(seed, key_de)?;

        Ok(Some(key))
    }

    fn visit_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
        where V: de::DeserializeSeed,
    {
        de::DeserializeSeed::deserialize(seed, self.elements.remove(0).1)
    }
}