subc_daemon/config_gate/
snapshot.rs1use std::collections::{BTreeMap, BTreeSet};
2
3use serde::{Deserialize, Serialize};
4use subc_config::{InputState, InputVector, RecordedValue};
5
6use super::{GateError, Result};
7use crate::config_store::Snapshot;
8
9#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
13pub enum InputKey {
14 Daemon,
15 Entry(String),
16 Layer { module: String, path: String },
17 Declaration(String),
18 Program { module: String, path: String },
19}
20
21impl InputKey {
22 pub fn module(&self) -> Option<&str> {
23 match self {
24 Self::Daemon => None,
25 Self::Entry(module) | Self::Declaration(module) => Some(module),
26 Self::Layer { module, .. } | Self::Program { module, .. } => Some(module),
27 }
28 }
29 pub(super) fn is_program(&self) -> bool {
30 matches!(self, Self::Program { .. })
31 }
32}
33
34#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
40pub struct CapturedInput {
41 pub state: InputState,
42 pub value: RecordedValue,
43}
44
45#[derive(Clone, Debug, Default, PartialEq)]
46pub struct GateSnapshot {
47 pub inputs: BTreeMap<InputKey, CapturedInput>,
48}
49
50impl GateSnapshot {
51 pub fn to_store(&self) -> Result<Snapshot> {
52 self.inputs
53 .iter()
54 .map(|(key, input)| {
55 Ok((
56 serde_json::to_string(key).map_err(GateError::invalid)?,
57 RecordedValue::new(serde_json::to_value(input).map_err(GateError::invalid)?),
58 ))
59 })
60 .collect()
61 }
62
63 pub fn from_store(snapshot: &Snapshot) -> Result<Self> {
64 let inputs = snapshot
65 .iter()
66 .map(|(key, value)| {
67 let RecordedValue::Value { value } = value else {
68 return Err(GateError::invalid("invalid gate snapshot envelope"));
69 };
70 Ok((
71 serde_json::from_str(key).map_err(GateError::invalid)?,
72 serde_json::from_value(value.clone()).map_err(GateError::invalid)?,
73 ))
74 })
75 .collect::<Result<_>>()?;
76 Ok(Self { inputs })
77 }
78
79 pub(super) fn modules(&self) -> BTreeSet<String> {
80 self.inputs
81 .keys()
82 .filter_map(|key| key.module().map(str::to_owned))
83 .collect()
84 }
85
86 pub(super) fn changed_keys<'a>(&'a self, other: &'a Self) -> BTreeSet<&'a InputKey> {
87 self.inputs
88 .keys()
89 .chain(other.inputs.keys())
90 .filter(|key| self.inputs.get(key) != other.inputs.get(key))
91 .collect()
92 }
93
94 pub fn read_set(&self, modules: &BTreeSet<String>) -> Result<InputVector> {
98 let daemon = self
99 .inputs
100 .get(&InputKey::Daemon)
101 .ok_or_else(|| GateError::invalid("snapshot has no subc.jsonc input"))?;
102 let mut inputs = BTreeMap::from([("subc.jsonc".into(), daemon.state.clone())]);
103 for module in modules {
104 let inventory: BTreeMap<_, _> = self
105 .inputs
106 .iter()
107 .filter(|(key, _)| key.module() == Some(module))
108 .map(|(key, value)| (serde_json::to_string(key).expect("input key"), value))
109 .collect();
110 inputs.insert(
111 format!(
112 "module:{}",
113 serde_json::to_string(module).expect("module id")
114 ),
115 InputState::Present {
116 digest: subc_config::raw_digest(
117 &serde_json::to_vec(&inventory).map_err(GateError::invalid)?,
118 ),
119 },
120 );
121 }
122 Ok(InputVector::new(inputs))
123 }
124
125 pub(super) fn restore_programs_from(&mut self, current: &Self) -> Result<()> {
126 for (key, identity) in self.inputs.iter_mut().filter(|(key, _)| key.is_program()) {
127 *identity = current.inputs.get(key).cloned().ok_or_else(|| {
128 GateError::new(
129 "program_missing",
130 "restored program path has no current captured identity",
131 )
132 })?;
133 }
134 Ok(())
135 }
136}