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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use crate::frame::FrameRef;
use crate::function::Function;
use crate::map::{Map, Result, ValueIter};
use crate::node::Node;

/// An enumeration of all possible value types.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum ValueType {
    Int,
    Float,
    Data,
    Node,
    Frame,
    Function,
}

/// A trait for values which can be stored in a map.
pub trait Value<'map, 'elem: 'map>: Sized {
    /// Retrieves the value from the map.
    fn get_from_map(map: &'map Map<'elem>, key: &str) -> Result<Self>;

    /// Retrieves an iterator over the values from the map.
    fn get_iter_from_map(map: &'map Map<'elem>, key: &str) -> Result<ValueIter<'map, 'elem, Self>>;

    /// Sets the property value in the map.
    fn store_in_map(map: &'map mut Map<'elem>, key: &str, x: &Self) -> Result<()>;

    /// Appends the value to the map.
    fn append_to_map(map: &'map mut Map<'elem>, key: &str, x: &Self) -> Result<()>;
}

impl<'map, 'elem: 'map> Value<'map, 'elem> for i64 {
    #[inline]
    fn get_from_map(map: &Map, key: &str) -> Result<Self> {
        map.get_int(key)
    }

    #[inline]
    fn get_iter_from_map(map: &'map Map<'elem>, key: &str) -> Result<ValueIter<'map, 'elem, Self>> {
        map.get_int_iter(key)
    }

    #[inline]
    fn store_in_map(map: &mut Map, key: &str, x: &Self) -> Result<()> {
        map.set_int(key, *x)
    }

    #[inline]
    fn append_to_map(map: &mut Map, key: &str, x: &Self) -> Result<()> {
        map.append_int(key, *x)
    }
}

impl<'map, 'elem: 'map> Value<'map, 'elem> for f64 {
    fn get_from_map(map: &Map, key: &str) -> Result<Self> {
        map.get_float(key)
    }

    #[inline]
    fn get_iter_from_map(map: &'map Map<'elem>, key: &str) -> Result<ValueIter<'map, 'elem, Self>> {
        map.get_float_iter(key)
    }

    #[inline]
    fn store_in_map(map: &mut Map, key: &str, x: &Self) -> Result<()> {
        map.set_float(key, *x)
    }

    #[inline]
    fn append_to_map(map: &mut Map, key: &str, x: &Self) -> Result<()> {
        map.append_float(key, *x)
    }
}

impl<'map, 'elem: 'map> Value<'map, 'elem> for &'map [u8] {
    #[inline]
    fn get_from_map(map: &'map Map, key: &str) -> Result<Self> {
        map.get_data(key)
    }

    #[inline]
    fn get_iter_from_map(map: &'map Map<'elem>, key: &str) -> Result<ValueIter<'map, 'elem, Self>> {
        map.get_data_iter(key)
    }

    #[inline]
    fn store_in_map(map: &'map mut Map, key: &str, x: &Self) -> Result<()> {
        map.set_data(key, x)
    }

    #[inline]
    fn append_to_map(map: &'map mut Map, key: &str, x: &Self) -> Result<()> {
        map.append_data(key, x)
    }
}

impl<'map, 'elem: 'map> Value<'map, 'elem> for Node<'elem> {
    #[inline]
    fn get_from_map(map: &Map<'elem>, key: &str) -> Result<Self> {
        map.get_node(key)
    }

    #[inline]
    fn get_iter_from_map(map: &'map Map<'elem>, key: &str) -> Result<ValueIter<'map, 'elem, Self>> {
        map.get_node_iter(key)
    }

    #[inline]
    fn store_in_map(map: &mut Map<'elem>, key: &str, x: &Self) -> Result<()> {
        map.set_node(key, x)
    }

    #[inline]
    fn append_to_map(map: &mut Map<'elem>, key: &str, x: &Self) -> Result<()> {
        map.append_node(key, x)
    }
}

impl<'map, 'elem: 'map> Value<'map, 'elem> for FrameRef<'elem> {
    #[inline]
    fn get_from_map(map: &Map<'elem>, key: &str) -> Result<Self> {
        map.get_frame(key)
    }

    #[inline]
    fn get_iter_from_map(map: &'map Map<'elem>, key: &str) -> Result<ValueIter<'map, 'elem, Self>> {
        map.get_frame_iter(key)
    }

    #[inline]
    fn store_in_map(map: &mut Map<'elem>, key: &str, x: &Self) -> Result<()> {
        map.set_frame(key, x)
    }

    #[inline]
    fn append_to_map(map: &mut Map<'elem>, key: &str, x: &Self) -> Result<()> {
        map.append_frame(key, x)
    }
}

impl<'map, 'elem: 'map> Value<'map, 'elem> for Function<'elem> {
    #[inline]
    fn get_from_map(map: &Map<'elem>, key: &str) -> Result<Self> {
        map.get_function(key)
    }

    #[inline]
    fn get_iter_from_map(map: &'map Map<'elem>, key: &str) -> Result<ValueIter<'map, 'elem, Self>> {
        map.get_function_iter(key)
    }

    #[inline]
    fn store_in_map(map: &mut Map<'elem>, key: &str, x: &Self) -> Result<()> {
        map.set_function(key, x)
    }

    #[inline]
    fn append_to_map(map: &mut Map<'elem>, key: &str, x: &Self) -> Result<()> {
        map.append_function(key, x)
    }
}