artifactize-core 0.27.2

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
Documentation
//! Workspace paths and external state/output safety checks.

use std::{
    io,
    path::{Path, PathBuf},
};

/// The definition directory is the workspace marker.
pub(crate) const ARTIFACT_MARKERS: [&str; 4] = [
    ".artifactize",
    "index.artf",
    "artifactize.json",
    ".artifactizeignore",
];

pub(crate) const ARTIFACT_SUFFIX: &str = ".artf";

pub(crate) fn has_artifact_marker(path: &Path) -> io::Result<bool> {
    crate::platform::paths::has_marked_entry(path, &ARTIFACT_MARKERS, ARTIFACT_SUFFIX).map_err(
        |error| {
            io::Error::new(
                error.kind(),
                format!(
                    "Cannot inspect workspace markers in {}: {error}",
                    crate::platform::path_text(path)
                ),
            )
        },
    )
}

pub fn canonical_target(path: &Path) -> io::Result<PathBuf> {
    crate::platform::paths::canonical_target(path)
}

pub(crate) fn outside_workspace(workspace: &Path, output: &Path) -> io::Result<()> {
    outside_workspaces(std::iter::once(workspace), output)
}

/// Validate a snapshot's repositories together: output-ancestor marker checks are identical
/// for every saved Run and must not enumerate those directories once per Run.
pub(crate) fn outside_workspaces<'a>(
    workspaces: impl IntoIterator<Item = &'a Path>,
    output: &Path,
) -> io::Result<()> {
    let mut workspaces = workspaces.into_iter().peekable();
    if workspaces.peek().is_none() {
        return Ok(());
    }
    if workspaces.any(|workspace| crate::platform::is_within(output, workspace)) {
        return Err(io::Error::other(
            "State and output directories must be outside the reviewed repository.",
        ));
    }
    outside_artifact_workspaces(output)
}

pub(crate) fn outside_artifact_workspaces(output: &Path) -> io::Result<()> {
    for ancestor in output.ancestors() {
        if has_artifact_marker(ancestor)? {
            return Err(io::Error::other(
                "State and output directories must be outside artifactize workspaces.",
            ));
        }
    }
    Ok(())
}

pub(crate) fn prepare_directory(path: &Path, workspace: &Path) -> io::Result<PathBuf> {
    let path = crate::platform::paths::resolve_target(path)?;
    outside_workspace(workspace, &path)?;
    crate::platform::create_private_dir_all(&path)?;
    let path = crate::platform::canonicalize(&path)?;
    outside_workspace(workspace, &path)?;
    Ok(path)
}

#[cfg(test)]
mod tests;