artifactize_core/config/
workspace.rs1use 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
14fn 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 pub(crate) authored_path: Option<String>,
36 pub target_error: Option<String>,
38 pub path: Target,
39 pub nested: BTreeMap<ChildPrefix, ArtifactName>,
42 pub covering_part: Option<ArtifactName>,
44 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 pub fn folder(&self) -> &Path {
97 folder_of(self.kind(), &self.path)
98 }
99
100 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 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 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 #[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 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 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 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}