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
use std::collections::HashMap;
use std::io::Cursor;

use frame::FromCursor;
use error;
use types::{cursor_next_value, try_from_bytes, CString, CStringList, SHORT_LEN};

#[derive(Debug)]
pub struct BodyResSupported {
    pub data: HashMap<String, Vec<String>>,
}

impl FromCursor for BodyResSupported {
    fn from_cursor(mut cursor: &mut Cursor<&[u8]>) -> error::Result<BodyResSupported> {
        let l =
            try_from_bytes(cursor_next_value(&mut cursor, SHORT_LEN as u64)?.as_slice())? as usize;
        let mut data: HashMap<String, Vec<String>> = HashMap::with_capacity(l);
        for _ in 0..l {
            let name = CString::from_cursor(&mut cursor)?.into_plain();
            let val = CStringList::from_cursor(&mut cursor)?.into_plain();
            data.insert(name, val);
        }

        Ok(BodyResSupported { data: data })
    }
}

#[cfg(test)]
mod tests {
    use std::io::Cursor;
    use frame::traits::FromCursor;
    use super::*;

    #[test]
    fn test_name() {
        let bytes = [0,
                     1, // n options
                     // 1-st option
                     0,
                     2,
                     97,
                     98, // key [string] "ab"
                     0,
                     2,
                     0,
                     1,
                     97,
                     0,
                     1,
                     98 /* value ["a", "b"] */];
        let mut cursor: Cursor<&[u8]> = Cursor::new(&bytes);
        let options = BodyResSupported::from_cursor(&mut cursor).unwrap().data;
        assert_eq!(options.len(), 1);
        let option_ab = options.get(&"ab".to_string()).unwrap();
        assert_eq!(option_ab[0], "a".to_string());
        assert_eq!(option_ab[1], "b".to_string());
    }
}