kotoba_core/graph/
edge.rs

1//! エッジ関連構造体
2
3use std::collections::HashMap;
4use crate::types::*;
5
6/// エッジビルダー
7#[derive(Debug, Clone)]
8pub struct EdgeBuilder {
9    id: Option<EdgeId>,
10    src: Option<VertexId>,
11    dst: Option<VertexId>,
12    label: Option<Label>,
13    props: Properties,
14}
15
16impl EdgeBuilder {
17    pub fn new() -> Self {
18        Self {
19            id: None,
20            src: None,
21            dst: None,
22            label: None,
23            props: HashMap::new(),
24        }
25    }
26
27    pub fn id(mut self, id: EdgeId) -> Self {
28        self.id = Some(id);
29        self
30    }
31
32    pub fn src(mut self, src: VertexId) -> Self {
33        self.src = Some(src);
34        self
35    }
36
37    pub fn dst(mut self, dst: VertexId) -> Self {
38        self.dst = Some(dst);
39        self
40    }
41
42    pub fn label(mut self, label: Label) -> Self {
43        self.label = Some(label);
44        self
45    }
46
47    pub fn prop(mut self, key: PropertyKey, value: Value) -> Self {
48        self.props.insert(key, value);
49        self
50    }
51
52    pub fn props(mut self, props: Properties) -> Self {
53        self.props = props;
54        self
55    }
56
57    pub fn build(self) -> crate::graph::EdgeData {
58        crate::graph::EdgeData {
59            id: self.id.unwrap_or_else(|| uuid::Uuid::new_v4()),
60            src: self.src.expect("src must be set"),
61            dst: self.dst.expect("dst must be set"),
62            label: self.label.expect("label must be set"),
63            props: self.props,
64        }
65    }
66}