1use serde::{Deserialize, Serialize};
4
5use crate::schema::EdgeType;
6
7#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
9pub struct Edge {
10 pub source_id: String,
12 pub target_id: String,
14 pub kind: EdgeType,
16 #[serde(default, skip_serializing_if = "Option::is_none")]
18 pub payload: Option<serde_json::Value>,
19 #[serde(default)]
21 pub first_seen_ms: u64,
22 #[serde(default)]
24 pub last_seen_ms: u64,
25}
26
27impl Edge {
28 #[must_use]
30 pub fn new(
31 source_id: impl Into<String>,
32 target_id: impl Into<String>,
33 kind: EdgeType,
34 ) -> Self {
35 let now = std::time::SystemTime::now()
36 .duration_since(std::time::UNIX_EPOCH)
37 .unwrap_or_default()
38 .as_millis() as u64;
39 Self {
40 source_id: source_id.into(),
41 target_id: target_id.into(),
42 kind,
43 payload: None,
44 first_seen_ms: now,
45 last_seen_ms: now,
46 }
47 }
48
49 #[must_use]
51 pub fn with_payload(mut self, payload: impl Serialize) -> Self {
52 self.payload = serde_json::to_value(payload).ok();
53 self
54 }
55}