Skip to main content

vecgraph_core/
edge.rs

1use crate::NodeId;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
5pub struct EdgeId(pub String);
6
7impl EdgeId {
8    pub fn new(id: impl Into<String>) -> Self {
9        Self(id.into())
10    }
11
12    pub fn from_source_target_kind(source_id: &NodeId, target_id: &NodeId, kind: &str) -> Self {
13        Self(format!(
14            "{}:{}:{}",
15            source_id.as_str(),
16            kind,
17            target_id.as_str()
18        ))
19    }
20
21    pub fn as_str(&self) -> &str {
22        &self.0
23    }
24}
25
26impl std::fmt::Display for EdgeId {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        f.write_str(&self.0)
29    }
30}
31
32impl<T: Into<String>> From<T> for EdgeId {
33    fn from(val: T) -> Self {
34        Self(val.into())
35    }
36}
37
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct Edge {
40    pub id: EdgeId,
41    pub source_node_id: NodeId,
42    pub target_node_id: NodeId,
43    pub kind: String,
44    pub content: String,
45    pub metadata: Option<serde_json::Value>,
46}
47
48impl Edge {
49    pub fn new(
50        source_node_id: impl Into<NodeId>,
51        target_node_id: impl Into<NodeId>,
52        kind: impl Into<String>,
53        content: impl Into<String>,
54    ) -> Self {
55        let source_node_id: NodeId = source_node_id.into();
56        let target_node_id: NodeId = target_node_id.into();
57        let kind: String = kind.into();
58        let id = EdgeId::from_source_target_kind(&source_node_id, &target_node_id, &kind);
59
60        Self {
61            id,
62            source_node_id,
63            target_node_id,
64            kind: kind,
65            content: content.into(),
66            metadata: None,
67        }
68    }
69
70    pub fn with_suffix(
71        source_node_id: impl Into<NodeId>,
72        target_node_id: impl Into<NodeId>,
73        kind: impl Into<String>,
74        suffix: impl std::fmt::Display,
75        content: impl Into<String>,
76    ) -> Self {
77        let source_node_id: NodeId = source_node_id.into();
78        let target_node_id: NodeId = target_node_id.into();
79        let kind: String = kind.into();
80        let id = EdgeId::new(format!(
81            "{}:{}:{}:{}",
82            source_node_id.as_str(),
83            kind,
84            target_node_id.as_str(),
85            suffix
86        ));
87
88        Self {
89            id,
90            source_node_id,
91            target_node_id,
92            kind: kind,
93            content: content.into(),
94            metadata: None,
95        }
96    }
97
98    pub fn with_metadata(mut self, metadata: serde_json::Value) -> Self {
99        self.metadata = Some(metadata);
100        self
101    }
102}
103
104#[derive(Debug, Clone, Serialize, Deserialize)]
105pub struct EdgeWithVector {
106    pub edge: Edge,
107    pub vector: Vec<f32>,
108}
109
110impl EdgeWithVector {
111    pub fn new(edge: Edge, vector: Vec<f32>) -> Self {
112        Self { edge, vector }
113    }
114}