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
use std::collections::HashMap;
use crate::*;
use crate::std_structs::StdStruct;

#[derive(Debug, Clone, PartialEq, PackableStruct, Pack, Unpack)]
#[tag = 0x52]
pub struct Relationship {
    pub id: i64,
    pub start_node_id: i64,
    pub end_node_id: i64,
    pub _type: String,
    pub properties: HashMap<String, Value<StdStruct>>
}

impl Relationship {
    pub fn new(id: i64, _type: &str, from: i64, to: i64) -> Self {
        Relationship {
            id,
            start_node_id: from,
            end_node_id: to,
            _type: String::from(_type),
            properties: HashMap::new(),
        }
    }

    pub fn add_property<V: Into<Value<StdStruct>>>(&mut self, key: &str, value: V) -> Option<Value<StdStruct>> {
        self.properties.insert(String::from(key), value.into())
    }
}

#[cfg(test)]
pub mod test {
    use crate::packable::test::pack_unpack_test;
    use crate::Value;
    use crate::std_structs::relationship::Relationship;

    #[test]
    fn pack_unpack() {
        pack_unpack_test::<Relationship>(&[
            Relationship {
                id: 42,
                start_node_id: 1,
                end_node_id: 2,
                _type: String::from("KNOWS"),
                properties: vec![(String::from("foo"), Value::from(1))].into_iter().collect(),
            }
        ]);
    }
}