kotoba_graph/graph/
vertex.rs

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