artifactize-core 0.27.0

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
Documentation
//! The saved checkout and Artifact targets an active review can show in a Viewer.
use std::path::{Path, PathBuf};

use crate::{
    config::Target,
    inspect::{self, RequestView},
    repository::Identity,
    review,
    types::{ArtifactName, RequestId, ReviewerId, RunId},
};

/// The Artifact's recorded target, within a particular checkout of that worktree.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Address {
    pub worktree: PathBuf,
    pub checkout: Identity,
    pub artifact: ArtifactName,
    pub target: Target,
}

impl Address {
    pub fn same_artifact(&self, other: &Self) -> bool {
        self.worktree == other.worktree
            && self.checkout == other.checkout
            && self.artifact == other.artifact
    }
}

pub struct ReviewScope {
    pub owner: RequestId,
    pub addresses: Vec<Address>,
}

/// The recorded checkout for a review Viewer, falling back to the exact directory's current
/// identity only for older Runs that did not save checkout metadata.
pub async fn review_checkout(state: &Path, run: &RunId) -> Result<(PathBuf, Identity), String> {
    crate::store::viewer_checkout(state, run).await
}

/// Resolve the claimed owner and the exact request's Artifact target when it shares another
/// Run's review. An unclaimed or settled review cannot enable a Viewer to add Points.
pub async fn review_scope(
    state: &Path,
    shown: &RequestId,
    reviewer: &ReviewerId,
) -> Result<Option<ReviewScope>, String> {
    let review::Review { exact, owner } = review::read(state, shown).await?;
    if review::review_refusal(Some(&owner), reviewer).is_some() {
        return Ok(None);
    }
    let mut addresses = Vec::new();
    if let Some(address) = address(state, &owner, None).await {
        addresses.push(address);
    }
    if exact.request.id != owner.request.id {
        let target = inspect::request(state, &exact.request.id)
            .await
            .ok()
            .and_then(|view| {
                view.definition
                    .and_then(|definition| definition.artifact().path.value().cloned())
            });
        if let Some(address) = address(state, &exact, target).await {
            addresses.push(address);
        }
    }
    Ok(Some(ReviewScope {
        owner: owner.request.id.clone(),
        addresses,
    }))
}

async fn address(
    state: &Path,
    request: &RequestView,
    recorded_target: Option<Target>,
) -> Option<Address> {
    let artifact = &request.request.target;
    let target = recorded_target.or_else(|| {
        request
            .request
            .human_definition
            .as_ref()
            .and_then(|definition| definition.artifacts.value())
            .and_then(|artifacts| artifacts.get(artifact))
            .and_then(|saved| saved.path.value())
            .cloned()
    })?;
    let (worktree, checkout) = review_checkout(state, &request.request.run_id).await.ok()?;
    Some(Address {
        worktree,
        checkout,
        artifact: artifact.clone(),
        target,
    })
}