Skip to main content

meshdb_core/
edge.rs

1use crate::{EdgeId, NodeId, Property};
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6pub struct Edge {
7    pub id: EdgeId,
8    pub edge_type: String,
9    pub source: NodeId,
10    pub target: NodeId,
11    pub properties: HashMap<String, Property>,
12}
13
14impl Edge {
15    pub fn new(edge_type: impl Into<String>, source: NodeId, target: NodeId) -> Self {
16        Self {
17            id: EdgeId::new(),
18            edge_type: edge_type.into(),
19            source,
20            target,
21            properties: HashMap::new(),
22        }
23    }
24
25    pub fn with_property(mut self, key: impl Into<String>, value: impl Into<Property>) -> Self {
26        self.properties.insert(key.into(), value.into());
27        self
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn edge_connects_source_to_target() {
37        let a = NodeId::new();
38        let b = NodeId::new();
39        let e = Edge::new("KNOWS", a, b).with_property("since", 2020_i64);
40
41        assert_eq!(e.edge_type, "KNOWS");
42        assert_eq!(e.source, a);
43        assert_eq!(e.target, b);
44        assert_eq!(e.properties.get("since"), Some(&Property::Int64(2020)));
45    }
46}