Skip to main content

artifactize_core/store/
snapshots.rs

1//! Saved review and validation snapshots, parsed once at the store boundary.
2//! Missing and null saved fields stay distinct; owner extension fields remain JSON.
3
4use crate::{
5    config::Field,
6    types::{ArtifactName, EvalId, Fingerprint, RequestStatus},
7};
8use serde::{Deserialize, Serialize};
9use serde_json::{Map, Value};
10use std::{collections::BTreeMap, path::PathBuf};
11
12#[derive(Debug, Clone, Serialize, Deserialize)]
13#[serde(rename_all = "camelCase")]
14pub struct ChildIdentity {
15    pub pid: crate::process::ProcessId,
16    pub start_time: u64,
17}
18impl From<crate::process::ChildIdentity> for ChildIdentity {
19    fn from(child: crate::process::ChildIdentity) -> Self {
20        Self {
21            pid: child.pid,
22            start_time: child.start_time,
23        }
24    }
25}
26
27/// A dependency blocker is either an Artifact or a workspace-qualified Eval.
28/// Its saved representation remains the original untagged string.
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
30#[serde(try_from = "String", into = "String")]
31pub enum Blocker {
32    Artifact(ArtifactName),
33    Eval(EvalId),
34}
35impl TryFrom<String> for Blocker {
36    type Error = String;
37    fn try_from(value: String) -> Result<Self, String> {
38        if value.contains('/') {
39            value.parse().map(Self::Eval)
40        } else {
41            value.parse().map(Self::Artifact)
42        }
43    }
44}
45impl From<Blocker> for String {
46    fn from(value: Blocker) -> String {
47        value.to_string()
48    }
49}
50impl std::fmt::Display for Blocker {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        match self {
53            Self::Artifact(id) => id.fmt(f),
54            Self::Eval(id) => id.fmt(f),
55        }
56    }
57}
58
59#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
60pub struct HumanDefinition {
61    #[serde(
62        default,
63        skip_serializing_if = "Field::missing",
64        with = "super::definitions::path_field"
65    )]
66    pub repo: Field<PathBuf>,
67    pub eval: HumanEval,
68    #[serde(
69        default,
70        skip_serializing_if = "Field::missing",
71        deserialize_with = "super::definitions::artifact_targets"
72    )]
73    pub artifacts: Field<BTreeMap<ArtifactName, super::definitions::Artifact>>,
74    /// Lookup hint. Read it here for older receipts, but write it beside the definition:
75    /// 0.27.2 reads unknown fields inside this definition as part of its identity.
76    #[serde(default, skip_serializing)]
77    pub declaration_scope: Option<crate::config::DeclarationScope>,
78    #[serde(flatten)]
79    pub extra: Map<String, Value>,
80}
81impl HumanDefinition {
82    pub(crate) fn authored(
83        repo: &std::path::Path,
84        eval: &crate::config::Eval,
85        scope: &crate::scope::Scope<'_>,
86    ) -> Self {
87        Self {
88            repo: Field::Value(repo.to_path_buf()),
89            eval: HumanEval::from(HumanEvalWire {
90                id: Field::Value(eval.id.clone()),
91                target: Field::Value(eval.target.clone()),
92                references: Field::Value(eval.references.clone()),
93                deps: Field::Value(eval.deps.clone()),
94                declaration: Field::Value((&eval.declaration).into()),
95                extra: Map::new(),
96            }),
97            artifacts: Field::Value(
98                scope
99                    .artifacts
100                    .iter()
101                    .map(|(name, artifact)| (name.clone(), (*artifact).into()))
102                    .collect(),
103            ),
104            declaration_scope: None,
105            extra: Map::new(),
106        }
107    }
108
109    pub(crate) fn same_declaration(&self, other: &Self) -> bool {
110        self.repo == other.repo
111            && self.eval == other.eval
112            && self.artifacts == other.artifacts
113            && self.extra == other.extra
114    }
115}
116
117#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
118#[serde(from = "HumanEvalWire", into = "HumanEvalWire")]
119pub struct HumanEval {
120    pub id: Field<EvalId>,
121    pub target: Field<ArtifactName>,
122    pub references: Field<BTreeMap<crate::config::ReferenceName, ArtifactName>>,
123    pub deps: Field<Vec<ArtifactName>>,
124    declaration: Field<super::definitions::Declaration>,
125    pub extra: Map<String, Value>,
126}
127
128impl HumanEval {
129    pub(crate) fn declaration(&self) -> Option<&super::definitions::Declaration> {
130        self.declaration.value()
131    }
132}
133
134impl From<HumanEvalWire> for HumanEval {
135    fn from(wire: HumanEvalWire) -> Self {
136        Self {
137            id: wire.id,
138            target: wire.target,
139            references: wire.references,
140            deps: wire.deps,
141            declaration: wire.declaration,
142            extra: wire.extra,
143        }
144    }
145}
146
147impl From<HumanEval> for HumanEvalWire {
148    fn from(eval: HumanEval) -> Self {
149        Self {
150            id: eval.id,
151            target: eval.target,
152            references: eval.references,
153            deps: eval.deps,
154            declaration: eval.declaration,
155            extra: eval.extra,
156        }
157    }
158}
159
160#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
161struct HumanEvalWire {
162    #[serde(default, skip_serializing_if = "Field::missing")]
163    pub id: Field<EvalId>,
164    #[serde(default, skip_serializing_if = "Field::missing")]
165    pub target: Field<ArtifactName>,
166    #[serde(default, skip_serializing_if = "Field::missing")]
167    pub references: Field<BTreeMap<crate::config::ReferenceName, ArtifactName>>,
168    #[serde(default, skip_serializing_if = "Field::missing")]
169    pub deps: Field<Vec<ArtifactName>>,
170    #[serde(default, skip_serializing_if = "Field::missing")]
171    pub declaration: Field<super::definitions::Declaration>,
172    #[serde(flatten)]
173    pub extra: Map<String, Value>,
174}
175#[derive(Debug, Clone, Default, Serialize, Deserialize)]
176#[serde(transparent)]
177pub struct Validation(pub Option<ValidationSnapshot>);
178impl Validation {
179    pub fn snapshot(&self) -> Option<&ValidationSnapshot> {
180        self.0.as_ref()
181    }
182}
183
184#[derive(Debug, Clone, Default, Serialize, Deserialize)]
185#[serde(rename_all = "camelCase")]
186pub struct ValidationSnapshot {
187    #[serde(default, skip_serializing_if = "Field::missing")]
188    pub selection: Field<super::definitions::SavedSelection>,
189    #[serde(default, skip_serializing_if = "Field::missing")]
190    pub recursive: Field<bool>,
191    #[serde(default, skip_serializing_if = "Field::missing")]
192    pub force: Field<bool>,
193    #[serde(default, skip_serializing_if = "Field::missing")]
194    pub selected_eval_ids: Field<Vec<EvalId>>,
195    #[serde(default, skip_serializing_if = "Field::missing")]
196    pub included_eval_ids: Field<Vec<EvalId>>,
197    #[serde(default, skip_serializing_if = "Field::missing")]
198    pub satisfied: Field<bool>,
199    #[serde(default, skip_serializing_if = "Field::missing")]
200    pub obligations: Field<Vec<ArtifactName>>,
201    #[serde(default, skip_serializing_if = "Field::missing")]
202    pub artifacts: Field<Vec<ArtifactValidation>>,
203    #[serde(default, skip_serializing_if = "Field::missing")]
204    pub evals: Field<Vec<EvalValidation>>,
205    #[serde(flatten)]
206    pub extra: Map<String, Value>,
207}
208
209#[derive(Debug, Clone, Serialize, Deserialize)]
210pub struct ArtifactValidation {
211    pub id: ArtifactName,
212    #[serde(default, skip_serializing_if = "Field::missing")]
213    pub status: Field<crate::types::ArtifactValidationStatus>,
214    #[serde(default, skip_serializing_if = "Field::missing")]
215    pub passed: Field<u64>,
216    #[serde(default, skip_serializing_if = "Field::missing")]
217    pub total: Field<u64>,
218    #[serde(default, skip_serializing_if = "Field::missing")]
219    pub satisfied: Field<bool>,
220    #[serde(
221        default,
222        rename = "fingerprintKind",
223        skip_serializing_if = "Field::missing"
224    )]
225    pub fingerprint_kind: Field<crate::types::FingerprintKind>,
226    #[serde(default, skip_serializing_if = "Field::missing")]
227    pub fingerprint: Field<Fingerprint>,
228    #[serde(flatten)]
229    pub extra: Map<String, Value>,
230}
231#[derive(Debug, Clone, Serialize, Deserialize)]
232#[serde(rename_all = "camelCase")]
233pub struct EvalValidation {
234    pub id: EvalId,
235    #[serde(default, skip_serializing_if = "Field::missing")]
236    pub status: Field<RequestStatus>,
237    #[serde(default, skip_serializing_if = "Field::missing")]
238    pub blocked_by: Field<Vec<Blocker>>,
239    #[serde(flatten)]
240    pub extra: Map<String, Value>,
241}
242
243#[cfg(test)]
244mod tests;