Skip to main content

antecedent_core/
node.rs

1//! Stable node identity before dense graph indexing.
2//!
3//! Shared by sample planning (`antecedent-data`) and graph types (`antecedent-graph`).
4//!
5//! SPDX-License-Identifier: MIT OR Apache-2.0
6
7use crate::ids::{EnvironmentId, Lag, VariableId};
8
9/// Stable node identity before dense indexing.
10#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
11pub enum NodeRef {
12    /// Static graph node.
13    Static(VariableId),
14    /// Lagged temporal node (`variable` at `t - lag`).
15    Lagged {
16        /// Variable.
17        variable: VariableId,
18        /// Non-negative lag (`0` = contemporaneous).
19        lag: Lag,
20    },
21    /// Context-aware node.
22    Context {
23        /// Variable.
24        variable: VariableId,
25        /// Optional environment.
26        environment: Option<EnvironmentId>,
27    },
28}
29
30impl NodeRef {
31    /// Variable id carried by this node reference.
32    #[must_use]
33    pub const fn variable(self) -> VariableId {
34        match self {
35            Self::Static(v)
36            | Self::Lagged { variable: v, .. }
37            | Self::Context { variable: v, .. } => v,
38        }
39    }
40
41    /// Lag if this is a lagged node; `None` for static/context.
42    #[must_use]
43    pub const fn lag(self) -> Option<Lag> {
44        match self {
45            Self::Lagged { lag, .. } => Some(lag),
46            Self::Static(_) | Self::Context { .. } => None,
47        }
48    }
49}