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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use crate::{
    errors::{Error, Result},
    types::{serde::DeError, BoltString, BoltType, BoltWireFormat},
    version::Version,
};
use ::serde::Deserialize;
use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::{collections::HashMap, iter::FromIterator, mem};

pub const TINY: u8 = 0xA0;
pub const SMALL: u8 = 0xD8;
pub const MEDIUM: u8 = 0xD9;
pub const LARGE: u8 = 0xDA;

#[derive(Debug, Default, PartialEq, Clone)]
pub struct BoltMap {
    pub value: HashMap<BoltString, BoltType>,
}

impl BoltMap {
    pub fn new() -> Self {
        BoltMap {
            value: HashMap::new(),
        }
    }

    pub fn with_capacity(capacity: usize) -> Self {
        BoltMap {
            value: HashMap::with_capacity(capacity),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.value.is_empty()
    }

    pub fn len(&self) -> usize {
        self.value.len()
    }

    pub fn put(&mut self, key: BoltString, value: BoltType) {
        self.value.insert(key, value);
    }

    pub fn get<'this, T>(&'this self, key: &str) -> Result<T, DeError>
    where
        T: Deserialize<'this>,
        Self: Sized,
    {
        match self.value.get(key) {
            Some(v) => v.to(),
            None => Err(DeError::NoSuchProperty),
        }
    }
}

impl FromIterator<(BoltString, BoltType)> for BoltMap {
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = (BoltString, BoltType)>,
    {
        let mut bolt_map = BoltMap::default();
        for (s, t) in iter.into_iter() {
            bolt_map.put(s, t);
        }
        bolt_map
    }
}

impl BoltWireFormat for BoltMap {
    fn can_parse(_version: Version, input: &[u8]) -> bool {
        let marker = input[0];
        (TINY..=(TINY | 0x0F)).contains(&marker)
            || marker == SMALL
            || marker == MEDIUM
            || marker == LARGE
    }

    fn parse(version: Version, input: &mut Bytes) -> Result<Self> {
        let marker = input.get_u8();
        let size = match marker {
            0xA0..=0xAF => 0x0F & marker as usize,
            SMALL => input.get_u8() as usize,
            MEDIUM => input.get_u16() as usize,
            LARGE => input.get_u32() as usize,
            _ => {
                return Err(Error::InvalidTypeMarker(format!(
                    "invalid map marker {}",
                    marker
                )))
            }
        };

        let mut map = BoltMap::default();
        for _ in 0..size {
            let key = BoltString::parse(version, input)?;
            let value = BoltType::parse(version, input)?;
            map.put(key, value);
        }

        Ok(map)
    }

    fn write_into(&self, version: Version, bytes: &mut BytesMut) -> Result<()> {
        let length = self.value.len();
        match length {
            0..=15 => {
                bytes.reserve(1);
                bytes.put_u8(TINY | length as u8)
            }
            16..=255 => {
                bytes.reserve(2);
                bytes.put_u8(SMALL);
                bytes.put_u8(length as u8);
            }
            256..=65_535 => {
                bytes.reserve(1 + mem::size_of::<u16>());
                bytes.put_u8(MEDIUM);
                bytes.put_u16(length as u16);
            }
            65_536..=4_294_967_295 => {
                bytes.reserve(1 + mem::size_of::<u32>());
                bytes.put_u8(LARGE);
                bytes.put_u32(length as u32);
            }
            _ => return Err(Error::MapTooBig),
        }

        for (key, value) in &self.value {
            key.write_into(version, bytes)?;
            value.write_into(version, bytes)?;
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn should_serialize_empty_map() {
        let map = BoltMap::default();

        let b: Bytes = map.into_bytes(Version::V4_1).unwrap();

        assert_eq!(&b[..], Bytes::from_static(&[TINY]));
    }

    #[test]
    fn should_serialize_map_of_strings() {
        let mut map = BoltMap::default();
        map.put("a".into(), "b".into());

        let b: Bytes = map.into_bytes(Version::V4_1).unwrap();

        assert_eq!(&b[..], Bytes::from_static(&[0xA1, 0x81, 0x61, 0x81, 0x62]));
    }

    #[test]
    fn should_deserialize_map_of_strings() {
        let mut input = Bytes::from_static(&[0xA1, 0x81, 0x61, 0x81, 0x62]);

        let map: BoltMap = BoltMap::parse(Version::V4_1, &mut input).unwrap();

        assert_eq!(map.value.len(), 1);
    }

    #[test]
    fn should_deserialize_small_map() {
        let mut map = BoltMap::default();
        for i in 0..=16 {
            map.put(i.to_string().into(), i.to_string().into());
        }

        let mut bytes = map.clone().into_bytes(Version::V4_1).unwrap();
        assert_eq!(bytes[0], SMALL);
        let deserialized_map: BoltMap = BoltMap::parse(Version::V4_1, &mut bytes).unwrap();
        assert_eq!(map, deserialized_map);
    }

    #[test]
    fn should_deserialize_medium_map() {
        let mut map = BoltMap::default();
        for i in 0..=256 {
            map.put(i.to_string().into(), i.to_string().into());
        }

        let mut bytes = map.clone().into_bytes(Version::V4_1).unwrap();
        assert_eq!(bytes[0], MEDIUM);
        let deserialized_map: BoltMap = BoltMap::parse(Version::V4_1, &mut bytes).unwrap();
        assert_eq!(map, deserialized_map);
    }

    #[test]
    fn should_deserialize_large_map() {
        let mut map = BoltMap::default();
        for i in 0..=65_536 {
            map.put(i.to_string().into(), i.to_string().into());
        }

        let mut bytes = map.clone().into_bytes(Version::V4_1).unwrap();
        assert_eq!(bytes[0], LARGE);
        let deserialized_map: BoltMap = BoltMap::parse(Version::V4_1, &mut bytes).unwrap();
        assert_eq!(map, deserialized_map);
    }
}