1use serde::{Deserialize, Serialize};
4
5use crate::schema::NodeType;
6
7#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
9pub struct Node {
10 pub id: String,
12 pub kind: NodeType,
14 pub label: String,
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 Node {
28 #[must_use]
30 pub fn new(id: impl Into<String>, kind: NodeType, label: impl Into<String>) -> Self {
31 let now = std::time::SystemTime::now()
32 .duration_since(std::time::UNIX_EPOCH)
33 .unwrap_or_default()
34 .as_millis() as u64;
35 Self {
36 id: id.into(),
37 kind,
38 label: label.into(),
39 payload: None,
40 first_seen_ms: now,
41 last_seen_ms: now,
42 }
43 }
44
45 #[must_use]
47 pub fn with_payload(mut self, payload: impl Serialize) -> Self {
48 self.payload = serde_json::to_value(payload).ok();
49 self
50 }
51}