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 type_name: Option<super::ArtifactType>,
50    pub(crate) plugin: Option<std::sync::Arc<super::plugins::Plugin>>,
51    pub basis: Option<bool>,
52    pub fingerprint: Option<Fingerprint>,
53}
54
55impl serde::Serialize for Artifact {
56    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
57        use serde::ser::SerializeMap;
58        let mut map = serializer.serialize_map(None)?;
59        map.serialize_entry("definition", &crate::platform::path_text(&self.definition))?;
60        map.serialize_entry("path", &self.path)?;
61        map.serialize_entry("kind", &self.kind())?;
62        map.serialize_entry("nested", &self.nested)?;
63        if let Some(part) = &self.covering_part {
64            map.serialize_entry("coveringPart", part)?;
65        }
66        if !self.select().is_empty() {
67            map.serialize_entry("select", self.select())?;
68        }
69        if !self.parts.is_empty() {
70            map.serialize_entry("parts", &self.parts)?;
71        }
72        map.serialize_entry("name", &self.name)?;
73        map.serialize_entry("tags", &self.tags)?;
74        map.serialize_entry("views", &self.views)?;
75        if let Some(name) = &self.type_name {
76            map.serialize_entry("type", name)?;
77        }
78        map.serialize_entry("basis", &self.basis)?;
79        map.serialize_entry("fingerprint", &self.fingerprint)?;
80        map.end()
81    }
82}
83
84impl Artifact {
85    pub fn kind(&self) -> ArtifactKind {
86        self.path.kind()
87    }
88    pub fn select(&self) -> &[LogicalPath] {
89        self.path.select()
90    }
91    pub(crate) fn tool_scope(&self) -> artifactize_tools::scope::Artifact {
92        artifactize_tools::scope::Artifact {
93            path: self.path.clone(),
94            name: self.name.to_string(),
95            nested: tool_nested(&self.nested),
96            covering_part: self.covering_part.as_ref().map(crate::scope::tool_id),
97        }
98    }
99
100    /// Runtime commands and relative declarations use the containing folder.
101    pub fn folder(&self) -> &Path {
102        folder_of(self.kind(), &self.path)
103    }
104
105    /// What `{name}` stands for: the folder or file, or an aspect's folder, since an
106    /// aspect's path is only its declaration's label.
107    pub fn referenced_path(&self) -> Option<&Path> {
108        if self.kind() == ArtifactKind::Aspect {
109            Some(self.folder())
110        } else {
111            self.path.local()
112        }
113    }
114
115    pub fn file_name(&self) -> Option<&str> {
116        (self.kind() == ArtifactKind::File)
117            .then(|| {
118                self.path
119                    .local()
120                    .and_then(Path::file_name)
121                    .and_then(|name| name.to_str())
122            })
123            .flatten()
124    }
125
126    pub fn declaration_path(&self) -> PathBuf {
127        if self.definition.as_os_str().is_empty() {
128            declaration_of(self.kind(), &self.path)
129        } else {
130            self.definition.clone()
131        }
132    }
133
134    /// Where it is declared.
135    pub fn site(&self) -> Site {
136        Site {
137            path: self.path.clone(),
138            definition: self.declaration_path(),
139        }
140    }
141}
142
143#[derive(Debug, Serialize)]
144pub struct Eval {
145    /// Workspace-qualified id; declaration.id remains the owner's local id.
146    pub id: EvalId,
147    pub target: ArtifactName,
148    pub references: BTreeMap<crate::config::ReferenceName, ArtifactName>,
149    pub deps: Vec<ArtifactName>,
150    pub declaration: EvalDeclaration,
151    #[serde(skip)]
152    pub(crate) instruction_references: Vec<crate::scope::InstructionReference>,
153    /// The `profileVariants` entry selected for this execution, if any: an execution option,
154    /// recorded with results but never part of the saved declaration.
155    #[serde(skip)]
156    pub variant: Option<ProfileVariantName>,
157}
158
159#[derive(Debug, Clone, Copy, PartialEq, Eq)]
160pub enum ReviewRequirement {
161    Basis,
162    Unreviewed,
163    Evals,
164    /// No evals of its own: its parts' verdicts are its verdict.
165    Parts,
166}
167
168#[derive(Debug)]
169pub struct RepoConfig {
170    pub root: PathBuf,
171    pub artifacts: BTreeMap<ArtifactName, Artifact>,
172    pub evals: Vec<Eval>,
173    pub relations: Vec<crate::scope::Relation>,
174}
175
176impl RepoConfig {
177    /// Every Artifact `name` is made of, directly or through its parts, each once.
178    pub fn descendants<'a>(&'a self, name: &'a ArtifactName) -> Vec<&'a ArtifactName> {
179        let mut seen = BTreeSet::new();
180        let mut pending = vec![name];
181        while let Some(next) = pending.pop() {
182            for part in self.artifacts.get(next).into_iter().flat_map(|a| &a.parts) {
183                if seen.insert(part) {
184                    pending.push(part);
185                }
186            }
187        }
188        seen.into_iter().collect()
189    }
190
191    /// Local obligations only; a basis does not satisfy its dependency scope.
192    pub fn review_requirement(&self, artifact_id: &str) -> Option<ReviewRequirement> {
193        let artifact = self.artifacts.get(artifact_id)?;
194        Some(if artifact.basis == Some(true) {
195            ReviewRequirement::Basis
196        } else if self.evals.iter().any(|eval| eval.target == artifact_id) {
197            ReviewRequirement::Evals
198        } else if !artifact.parts.is_empty() {
199            ReviewRequirement::Parts
200        } else {
201            ReviewRequirement::Unreviewed
202        })
203    }
204}