Skip to main content

sim_expr_tree_core/
node.rs

1use crate::{CellId, CodecPolicyPatch, DirId, NamespaceName, Stamp};
2
3/// Durable expression-tree node families.
4#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
5pub enum NodeKind {
6    /// A mutable source cell.
7    Source,
8    /// A control/configuration cell.
9    Control,
10    /// A calculation cell whose value is derived from dependencies.
11    Derived,
12    /// A mounted external table or directory.
13    Mounted,
14}
15
16/// Source provenance attached to a namespace node.
17#[derive(Clone, Debug, PartialEq, Eq)]
18pub struct SourceRecord {
19    origin: String,
20    observed_at: Stamp,
21}
22
23impl SourceRecord {
24    /// Create source provenance from an origin label and stamp.
25    pub fn new(origin: impl Into<String>, observed_at: Stamp) -> Self {
26        Self {
27            origin: origin.into(),
28            observed_at,
29        }
30    }
31
32    /// The source origin label.
33    pub fn origin(&self) -> &str {
34        &self.origin
35    }
36
37    /// The stamp associated with this source observation.
38    pub fn observed_at(&self) -> Stamp {
39        self.observed_at
40    }
41}
42
43/// Immutable durable calculation cell record.
44#[derive(Clone, Debug, PartialEq, Eq)]
45pub struct CellRecord {
46    id: CellId,
47    parent: DirId,
48    name: NamespaceName,
49    kind: NodeKind,
50    source: Option<SourceRecord>,
51    policy_patch: CodecPolicyPatch,
52    created_at: Stamp,
53}
54
55impl CellRecord {
56    pub(crate) fn new(
57        id: CellId,
58        parent: DirId,
59        name: NamespaceName,
60        kind: NodeKind,
61        source: Option<SourceRecord>,
62        policy_patch: CodecPolicyPatch,
63        created_at: Stamp,
64    ) -> Self {
65        Self {
66            id,
67            parent,
68            name,
69            kind,
70            source,
71            policy_patch,
72            created_at,
73        }
74    }
75
76    /// Stable immutable cell id.
77    pub fn id(&self) -> &CellId {
78        &self.id
79    }
80
81    /// Parent directory id.
82    pub fn parent(&self) -> &DirId {
83        &self.parent
84    }
85
86    /// Current child name.
87    pub fn name(&self) -> &NamespaceName {
88        &self.name
89    }
90
91    /// Node family.
92    pub fn kind(&self) -> NodeKind {
93        self.kind
94    }
95
96    /// Optional source provenance.
97    pub fn source(&self) -> Option<&SourceRecord> {
98        self.source.as_ref()
99    }
100
101    /// Local policy patch.
102    pub fn policy_patch(&self) -> &CodecPolicyPatch {
103        &self.policy_patch
104    }
105
106    /// Creation stamp.
107    pub fn created_at(&self) -> Stamp {
108        self.created_at
109    }
110
111    pub(crate) fn rename(&mut self, parent: DirId, name: NamespaceName) {
112        self.parent = parent;
113        self.name = name;
114    }
115
116    pub(crate) fn set_policy_patch(&mut self, policy_patch: CodecPolicyPatch) {
117        self.policy_patch = policy_patch;
118    }
119}
120
121/// Durable directory record with immutable identity and movable parent/name.
122#[derive(Clone, Debug, PartialEq, Eq)]
123pub struct DirRecord {
124    id: DirId,
125    parent: Option<DirId>,
126    name: Option<NamespaceName>,
127    policy_patch: CodecPolicyPatch,
128    created_at: Stamp,
129}
130
131impl DirRecord {
132    pub(crate) fn root(id: DirId, created_at: Stamp) -> Self {
133        Self {
134            id,
135            parent: None,
136            name: None,
137            policy_patch: CodecPolicyPatch::empty(),
138            created_at,
139        }
140    }
141
142    pub(crate) fn child(
143        id: DirId,
144        parent: DirId,
145        name: NamespaceName,
146        policy_patch: CodecPolicyPatch,
147        created_at: Stamp,
148    ) -> Self {
149        Self {
150            id,
151            parent: Some(parent),
152            name: Some(name),
153            policy_patch,
154            created_at,
155        }
156    }
157
158    /// Stable immutable directory id.
159    pub fn id(&self) -> &DirId {
160        &self.id
161    }
162
163    /// Parent directory id, absent for root.
164    pub fn parent(&self) -> Option<&DirId> {
165        self.parent.as_ref()
166    }
167
168    /// Current child name, absent for root.
169    pub fn name(&self) -> Option<&NamespaceName> {
170        self.name.as_ref()
171    }
172
173    /// Local policy patch.
174    pub fn policy_patch(&self) -> &CodecPolicyPatch {
175        &self.policy_patch
176    }
177
178    /// Creation stamp.
179    pub fn created_at(&self) -> Stamp {
180        self.created_at
181    }
182
183    pub(crate) fn rename(&mut self, parent: DirId, name: NamespaceName) {
184        self.parent = Some(parent);
185        self.name = Some(name);
186    }
187
188    pub(crate) fn set_policy_patch(&mut self, policy_patch: CodecPolicyPatch) {
189        self.policy_patch = policy_patch;
190    }
191}