cassandra_proto/frame/
frame_supported.rs

1use std::collections::HashMap;
2use std::io::Cursor;
3
4use crate::frame::FromCursor;
5use crate::error;
6use crate::types::{cursor_next_value, try_from_bytes, CString, CStringList, SHORT_LEN};
7
8#[derive(Debug)]
9pub struct BodyResSupported {
10    pub data: HashMap<String, Vec<String>>,
11}
12
13impl FromCursor for BodyResSupported {
14    fn from_cursor(mut cursor: &mut Cursor<&[u8]>) -> error::Result<BodyResSupported> {
15        let l =
16            try_from_bytes(cursor_next_value(&mut cursor, SHORT_LEN as u64)?.as_slice())? as usize;
17        let mut data: HashMap<String, Vec<String>> = HashMap::with_capacity(l);
18        for _ in 0..l {
19            let name = CString::from_cursor(&mut cursor)?.into_plain();
20            let val = CStringList::from_cursor(&mut cursor)?.into_plain();
21            data.insert(name, val);
22        }
23
24        Ok(BodyResSupported { data: data })
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use std::io::Cursor;
31    use crate::frame::traits::FromCursor;
32    use super::*;
33
34    #[test]
35    fn test_name() {
36        let bytes = [0,
37                     1, // n options
38                     // 1-st option
39                     0,
40                     2,
41                     97,
42                     98, // key [string] "ab"
43                     0,
44                     2,
45                     0,
46                     1,
47                     97,
48                     0,
49                     1,
50                     98 /* value ["a", "b"] */];
51        let mut cursor: Cursor<&[u8]> = Cursor::new(&bytes);
52        let options = BodyResSupported::from_cursor(&mut cursor).unwrap().data;
53        assert_eq!(options.len(), 1);
54        let option_ab = options.get(&"ab".to_string()).unwrap();
55        assert_eq!(option_ab[0], "a".to_string());
56        assert_eq!(option_ab[1], "b".to_string());
57    }
58}