Skip to main content

gossan_graph/
edge.rs

1//! Typed graph edge.
2
3use serde::{Deserialize, Serialize};
4
5use crate::schema::EdgeType;
6
7/// An edge (relationship) in the attack-surface graph.
8#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
9pub struct Edge {
10    /// Source node id.
11    pub source_id: String,
12    /// Target node id.
13    pub target_id: String,
14    /// Semantic relationship type.
15    pub kind: EdgeType,
16    /// Optional JSON payload with relationship-specific metadata.
17    #[serde(default, skip_serializing_if = "Option::is_none")]
18    pub payload: Option<serde_json::Value>,
19    /// Unix timestamp (ms) when the edge was first observed.
20    #[serde(default)]
21    pub first_seen_ms: u64,
22    /// Unix timestamp (ms) when the edge was last observed.
23    #[serde(default)]
24    pub last_seen_ms: u64,
25}
26
27impl Edge {
28    /// Create a new edge with the current time as `first_seen`.
29    #[must_use]
30    pub fn new(source_id: impl Into<String>, target_id: impl Into<String>, kind: EdgeType) -> 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            source_id: source_id.into(),
37            target_id: target_id.into(),
38            kind,
39            payload: None,
40            first_seen_ms: now,
41            last_seen_ms: now,
42        }
43    }
44
45    /// Attach a JSON payload.
46    #[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}