Skip to main content

artifactize_core/config/
site.rs

1//! Where an Artifact is declared: its target and the file that binds it. Moving a binding to
2//! another file, or its target elsewhere, is a move; anything else about the file is not.
3use super::*;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct Site {
7    pub path: Target,
8    pub definition: PathBuf,
9}
10impl Site {
11    /// Base of relative commands and inputs, not the definition's containing folder.
12    pub fn folder(&self) -> &Path {
13        folder_of(self.path.kind(), &self.path)
14    }
15    pub fn declaration_path(&self) -> PathBuf {
16        self.definition.clone()
17    }
18}
19pub(super) fn folder_of(kind: ArtifactKind, path: &Target) -> &Path {
20    match kind {
21        ArtifactKind::Folder | ArtifactKind::Aspect | ArtifactKind::Graph => {
22            path.local().expect("filesystem kind")
23        }
24        ArtifactKind::File => path
25            .local()
26            .expect("filesystem kind")
27            .parent()
28            .unwrap_or(Path::new("")),
29        ArtifactKind::Live => Path::new(""),
30    }
31}
32/// Old locations are used only while migrating and when reading pre-migration saved Runs.
33pub(super) fn declaration_of(kind: ArtifactKind, target: &Target) -> PathBuf {
34    let path = target.local().unwrap_or(Path::new(""));
35    match kind {
36        ArtifactKind::Live => PathBuf::new(),
37        ArtifactKind::Graph => PathBuf::from(".artf"),
38        ArtifactKind::Folder => logical_join(path, std::ffi::OsStr::new(CONFIG_FILE)),
39        ArtifactKind::File | ArtifactKind::Aspect => {
40            let mut path = path.as_os_str().to_owned();
41            path.push(".artf");
42            PathBuf::from(path)
43        }
44    }
45}
46
47/// Where every Artifact of the project is declared now.
48pub fn read_sites(repo: &Path) -> Result<BTreeMap<ArtifactName, Site>, ConfigError> {
49    let config = discovery::read_workspace_declarations(repo)?;
50    Ok(config
51        .artifacts
52        .into_iter()
53        .map(|(name, artifact)| {
54            let site = artifact.site();
55            (name, site)
56        })
57        .collect())
58}
59
60#[derive(Debug, Error)]
61pub enum ScopeReadError {
62    #[error("{name} is no longer declared at {}.", crate::platform::path_text(.path))]
63    Moved { name: ArtifactName, path: PathBuf },
64    #[error(transparent)]
65    Config(#[from] ConfigError),
66}
67
68/// The project as declared now, when `name` is still declared at `site`.
69fn read_declared(
70    repo: &Path,
71    name: &ArtifactName,
72    site: &Site,
73) -> Result<RepoConfig, ScopeReadError> {
74    let config = discovery::read_workspace_declarations(repo)?;
75    if !config
76        .artifacts
77        .get(name)
78        .is_some_and(|artifact| artifact.site() == *site)
79    {
80        return Err(ScopeReadError::Moved {
81            name: name.clone(),
82            path: site.declaration_path(),
83        });
84    }
85    Ok(config)
86}
87
88pub(crate) fn read_artifact_scope(
89    repo: &Path,
90    name: &ArtifactName,
91    site: &Site,
92) -> Result<RepoConfig, ScopeReadError> {
93    let mut config = read_declared(repo, name, site)?;
94    config.evals.clear();
95    Ok(config)
96}
97
98/// The project as declared now with only eval `id`, when every Artifact it reads is still
99/// declared where `sites` says; none when the eval is no longer declared.
100pub(crate) fn read_eval_scope(
101    repo: &Path,
102    id: &EvalId,
103    sites: &BTreeMap<ArtifactName, Site>,
104) -> Result<Option<RepoConfig>, ScopeReadError> {
105    let mut config = discovery::read_workspace_declarations(repo)?;
106    for (name, site) in sites {
107        if !config
108            .artifacts
109            .get(name)
110            .is_some_and(|artifact| artifact.site() == *site)
111        {
112            return Err(ScopeReadError::Moved {
113                name: name.clone(),
114                path: site.declaration_path(),
115            });
116        }
117    }
118    config.evals.retain(|eval| &eval.id == id);
119    Ok((!config.evals.is_empty()).then_some(config))
120}