1use 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 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 pub fn folder(&self) -> &Path {
102 folder_of(self.kind(), &self.path)
103 }
104
105 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 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 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 #[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 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 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 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}