use super::*;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Site {
pub path: Target,
pub definition: PathBuf,
}
impl Site {
pub fn folder(&self) -> &Path {
folder_of(self.path.kind(), &self.path)
}
pub fn declaration_path(&self) -> PathBuf {
self.definition.clone()
}
}
pub(super) fn folder_of(kind: ArtifactKind, path: &Target) -> &Path {
match kind {
ArtifactKind::Folder | ArtifactKind::Aspect | ArtifactKind::Graph => {
path.local().expect("filesystem kind")
}
ArtifactKind::File => path
.local()
.expect("filesystem kind")
.parent()
.unwrap_or(Path::new("")),
ArtifactKind::Live => Path::new(""),
}
}
pub(super) fn declaration_of(kind: ArtifactKind, target: &Target) -> PathBuf {
let path = target.local().unwrap_or(Path::new(""));
match kind {
ArtifactKind::Live => PathBuf::new(),
ArtifactKind::Graph => PathBuf::from(".artf"),
ArtifactKind::Folder => logical_join(path, std::ffi::OsStr::new(CONFIG_FILE)),
ArtifactKind::File | ArtifactKind::Aspect => {
let mut path = path.as_os_str().to_owned();
path.push(".artf");
PathBuf::from(path)
}
}
}
pub fn read_sites(repo: &Path) -> Result<BTreeMap<ArtifactName, Site>, ConfigError> {
let config = discovery::read_workspace_declarations(repo)?;
Ok(config
.artifacts
.into_iter()
.map(|(name, artifact)| {
let site = artifact.site();
(name, site)
})
.collect())
}
#[derive(Debug, Error)]
pub enum ScopeReadError {
#[error("{name} is no longer declared at {}.", crate::platform::path_text(.path))]
Moved { name: ArtifactName, path: PathBuf },
#[error(transparent)]
Config(#[from] ConfigError),
}
fn read_declared(
repo: &Path,
name: &ArtifactName,
site: &Site,
) -> Result<RepoConfig, ScopeReadError> {
let config = discovery::read_workspace_declarations(repo)?;
if !config
.artifacts
.get(name)
.is_some_and(|artifact| artifact.site() == *site)
{
return Err(ScopeReadError::Moved {
name: name.clone(),
path: site.declaration_path(),
});
}
Ok(config)
}
pub(crate) fn read_artifact_scope(
repo: &Path,
name: &ArtifactName,
site: &Site,
) -> Result<RepoConfig, ScopeReadError> {
let mut config = read_declared(repo, name, site)?;
config.evals.clear();
Ok(config)
}
pub(crate) fn read_eval_scope(
repo: &Path,
id: &EvalId,
sites: &BTreeMap<ArtifactName, Site>,
) -> Result<Option<RepoConfig>, ScopeReadError> {
let mut config = discovery::read_workspace_declarations(repo)?;
for (name, site) in sites {
if !config
.artifacts
.get(name)
.is_some_and(|artifact| artifact.site() == *site)
{
return Err(ScopeReadError::Moved {
name: name.clone(),
path: site.declaration_path(),
});
}
}
config.evals.retain(|eval| &eval.id == id);
Ok((!config.evals.is_empty()).then_some(config))
}