Skip to main content

meshdb_core/
node.rs

1use crate::{NodeId, Property};
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6pub struct Node {
7    pub id: NodeId,
8    pub labels: Vec<String>,
9    pub properties: HashMap<String, Property>,
10}
11
12impl Node {
13    pub fn new() -> Self {
14        Self {
15            id: NodeId::new(),
16            labels: Vec::new(),
17            properties: HashMap::new(),
18        }
19    }
20
21    pub fn with_label(mut self, label: impl Into<String>) -> Self {
22        self.labels.push(label.into());
23        self
24    }
25
26    pub fn with_property(mut self, key: impl Into<String>, value: impl Into<Property>) -> Self {
27        self.properties.insert(key.into(), value.into());
28        self
29    }
30}
31
32impl Default for Node {
33    fn default() -> Self {
34        Self::new()
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn builder_populates_labels_and_properties() {
44        let n = Node::new()
45            .with_label("Person")
46            .with_property("name", "Ada")
47            .with_property("age", 37_i64);
48
49        assert_eq!(n.labels, vec!["Person"]);
50        assert_eq!(
51            n.properties.get("name"),
52            Some(&Property::String("Ada".into()))
53        );
54        assert_eq!(n.properties.get("age"), Some(&Property::Int64(37)));
55    }
56}