packs/std_structs/
node.rs

1use std::collections::{HashSet};
2use crate::*;
3use crate::std_structs::{StdStructPrimitive};
4
5#[derive(Debug, Clone, PartialEq, Pack, Unpack)]
6#[tag = 0x4E]
7pub struct Node {
8    pub id: i64,
9    pub labels: HashSet<String>,
10    pub properties: Dictionary<StdStructPrimitive>
11}
12
13impl Node {
14    pub fn new(id: i64) -> Self {
15        Node {
16            id,
17            labels: HashSet::new(),
18            properties: Dictionary::new(),
19        }
20    }
21
22    pub fn add_label(&mut self, label: &str) {
23        self.labels.insert(String::from(label));
24    }
25}
26
27#[cfg(test)]
28pub mod test {
29    use crate::packable::test::{pack_unpack_test, pack_to_test};
30    use crate::std_structs::node::Node;
31    use crate::value::Value;
32
33    #[test]
34    fn pack_unpack() {
35        pack_unpack_test::<Node>(&[
36            Node {
37                id: 42,
38                labels: vec!(String::from("Person"), String::from("Author")).into_iter().collect(),
39                properties: vec![
40                    (String::from("name"), Value::from("Hans Fallada")),
41                    (String::from("age"), Value::from(32)),
42                ].into_iter().collect(),
43            }
44        ],)
45    }
46
47    #[test]
48    fn pack_into() {
49        let mut node = Node::new(42);
50        node.add_label("Person");
51        node.properties.add_property("name", "Hans");
52        pack_to_test(
53            node,
54            &[0xB3, 0x4E,
55                0x2A,
56                0x91, 0x86, 0x50, 0x65, 0x72, 0x73, 0x6F, 0x6E,
57                0xA1,
58                    0x84, 0x6E, 0x61, 0x6D, 0x65,
59                    0x84, 0x48, 0x61, 0x6E, 0x73]
60        )
61    }
62}