Skip to main content

artifactize_core/config/
workspace.rs

1//! Resolved Artifacts, their Evals, and the workspace model shared by callers.
2
3use super::{
4    ArtifactKind, ArtifactName, ChildPrefix, EvalDeclaration, EvalId, Fingerprint, LogicalPath,
5    ProfileVariantName, Site, Target, Views,
6    site::{declaration_of, folder_of},
7};
8use serde::Serialize;
9use std::{
10    collections::{BTreeMap, BTreeSet},
11    path::{Path, PathBuf},
12};
13
14/// Nested folder paths with their Artifacts as tool-side IDs. Discovery validated both, so a
15/// failure here is a bug.
16fn tool_nested(
17    nested: &BTreeMap<ChildPrefix, ArtifactName>,
18) -> BTreeMap<artifactize_tools::scope::ChildPrefix, artifactize_tools::scope::ArtifactId> {
19    nested
20        .iter()
21        .map(|(path, name)| {
22            (
23                artifactize_tools::scope::ChildPrefix::new(path.as_ref())
24                    .expect("validated child prefix"),
25                crate::scope::tool_id(name),
26            )
27        })
28        .collect()
29}
30
31#[derive(Debug, Clone)]
32pub struct Artifact {
33    pub definition: PathBuf,
34    /// Exact PATH from a definition node; legacy migration inputs lack this field.
35    pub(crate) authored_path: Option<String>,
36    /// Current target availability is not part of the saved declaration or its identity.
37    pub target_error: Option<String>,
38    pub path: Target,
39    /// Folder Artifacts declared in subfolders, by their path here. Their files are theirs,
40    /// not this Artifact's; discovery also records them as parts.
41    pub nested: BTreeMap<ChildPrefix, ArtifactName>,
42    /// A folder part at the same target or an ancestor, owning every file here.
43    pub covering_part: Option<ArtifactName>,
44    /// What it is made of: the Artifacts its folder holds and those its `parts` names.
45    pub parts: BTreeSet<ArtifactName>,
46    pub name: ArtifactName,
47    pub tags: Vec<String>,
48    pub views: Views,
49    pub basis: Option<bool>,
50    pub fingerprint: Option<Fingerprint>,
51}
52
53impl serde::Serialize for Artifact {
54    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
55        use serde::ser::SerializeMap;
56        let mut map = serializer.serialize_map(None)?;
57        map.serialize_entry("definition", &crate::platform::path_text(&self.definition))?;
58        map.serialize_entry("path", &self.path)?;
59        map.serialize_entry("kind", &self.kind())?;
60        map.serialize_entry("nested", &self.nested)?;
61        if let Some(part) = &self.covering_part {
62            map.serialize_entry("coveringPart", part)?;
63        }
64        if !self.select().is_empty() {
65            map.serialize_entry("select", self.select())?;
66        }
67        if !self.parts.is_empty() {
68            map.serialize_entry("parts", &self.parts)?;
69        }
70        map.serialize_entry("name", &self.name)?;
71        map.serialize_entry("tags", &self.tags)?;
72        map.serialize_entry("views", &self.views)?;
73        map.serialize_entry("basis", &self.basis)?;
74        map.serialize_entry("fingerprint", &self.fingerprint)?;
75        map.end()
76    }
77}
78
79impl Artifact {
80    pub fn kind(&self) -> ArtifactKind {
81        self.path.kind()
82    }
83    pub fn select(&self) -> &[LogicalPath] {
84        self.path.select()
85    }
86    pub(crate) fn tool_scope(&self) -> artifactize_tools::scope::Artifact {
87        artifactize_tools::scope::Artifact {
88            path: self.path.clone(),
89            name: self.name.to_string(),
90            nested: tool_nested(&self.nested),
91            covering_part: self.covering_part.as_ref().map(crate::scope::tool_id),
92        }
93    }
94
95    /// Runtime commands and relative declarations use the containing folder.
96    pub fn folder(&self) -> &Path {
97        folder_of(self.kind(), &self.path)
98    }
99
100    /// What `{name}` stands for: the folder or file, or an aspect's folder, since an
101    /// aspect's path is only its declaration's label.
102    pub fn referenced_path(&self) -> Option<&Path> {
103        if self.kind() == ArtifactKind::Aspect {
104            Some(self.folder())
105        } else {
106            self.path.local()
107        }
108    }
109
110    pub fn file_name(&self) -> Option<&str> {
111        (self.kind() == ArtifactKind::File)
112            .then(|| {
113                self.path
114                    .local()
115                    .and_then(Path::file_name)
116                    .and_then(|name| name.to_str())
117            })
118            .flatten()
119    }
120
121    pub fn declaration_path(&self) -> PathBuf {
122        if self.definition.as_os_str().is_empty() {
123            declaration_of(self.kind(), &self.path)
124        } else {
125            self.definition.clone()
126        }
127    }
128
129    /// Where it is declared.
130    pub fn site(&self) -> Site {
131        Site {
132            path: self.path.clone(),
133            definition: self.declaration_path(),
134        }
135    }
136}
137
138#[derive(Debug, Serialize)]
139pub struct Eval {
140    /// Workspace-qualified id; declaration.id remains the owner's local id.
141    pub id: EvalId,
142    pub target: ArtifactName,
143    pub references: BTreeMap<crate::config::ReferenceName, ArtifactName>,
144    pub deps: Vec<ArtifactName>,
145    pub declaration: EvalDeclaration,
146    #[serde(skip)]
147    pub(crate) instruction_references: Vec<crate::scope::InstructionReference>,
148    /// The `profileVariants` entry selected for this execution, if any: an execution option,
149    /// recorded with results but never part of the saved declaration.
150    #[serde(skip)]
151    pub variant: Option<ProfileVariantName>,
152}
153
154#[derive(Debug, Clone, Copy, PartialEq, Eq)]
155pub enum ReviewRequirement {
156    Basis,
157    Unreviewed,
158    Evals,
159    /// No evals of its own: its parts' verdicts are its verdict.
160    Parts,
161}
162
163#[derive(Debug)]
164pub struct RepoConfig {
165    pub root: PathBuf,
166    pub artifacts: BTreeMap<ArtifactName, Artifact>,
167    pub evals: Vec<Eval>,
168    pub relations: Vec<crate::scope::Relation>,
169}
170
171impl RepoConfig {
172    /// Every Artifact `name` is made of, directly or through its parts, each once.
173    pub fn descendants<'a>(&'a self, name: &'a ArtifactName) -> Vec<&'a ArtifactName> {
174        let mut seen = BTreeSet::new();
175        let mut pending = vec![name];
176        while let Some(next) = pending.pop() {
177            for part in self.artifacts.get(next).into_iter().flat_map(|a| &a.parts) {
178                if seen.insert(part) {
179                    pending.push(part);
180                }
181            }
182        }
183        seen.into_iter().collect()
184    }
185
186    /// Local obligations only; a basis does not satisfy its dependency scope.
187    pub fn review_requirement(&self, artifact_id: &str) -> Option<ReviewRequirement> {
188        let artifact = self.artifacts.get(artifact_id)?;
189        Some(if artifact.basis == Some(true) {
190            ReviewRequirement::Basis
191        } else if self.evals.iter().any(|eval| eval.target == artifact_id) {
192            ReviewRequirement::Evals
193        } else if !artifact.parts.is_empty() {
194            ReviewRequirement::Parts
195        } else {
196            ReviewRequirement::Unreviewed
197        })
198    }
199}