rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The hooks files a session would load, each read on its own, for
//! `rho/hooks/list`: a settings view of what is configured, with no
//! session, runtime, or activity.
//!
//! Each file is read as [`HookCatalog::discover`](super::HookCatalog::discover)
//! reads it ([`HooksFileRead`]). Where `discover` stops at the first file a
//! session cannot load, since a session then runs no hooks, this reads
//! every file, so one file's error does not hide the other's hooks, and
//! marks the file that would load `disabled`.

use std::path::Path;

use rho_protocol::{HookStatus, HooksFile, HooksFileScope, HooksFileStatus, HooksProjectTrust};

use super::{
    catalog::{spawn_contract, HooksFileOutcome, HooksFileRead},
    config::HookDefinition,
    diagnostics::contract,
    HookConfigError,
};
use crate::{paths::without_verbatim_prefix, workspace::ProjectTrust};

/// The user's hooks file under `rho_home`, then, given `project_root`, the
/// project's, loaded only under `trust` as a session loads it, which it is
/// listed with. Each is listed whether or not it exists, by the path people
/// write: a project root comes from a canonical workspace, so verbatim
/// (`\\?\C:\…`) on Windows.
pub(crate) fn configured_files(
    rho_home: Option<&Path>,
    project_root: Option<&Path>,
    trust: ProjectTrust,
) -> Vec<HooksFile> {
    // The user's file always loads, so only the project's carries a trust.
    let listed_trust = match trust {
        ProjectTrust::Trusted => HooksProjectTrust::Trusted,
        ProjectTrust::Untrusted => HooksProjectTrust::Untrusted,
    };
    let user = rho_home.map(|home| (HooksFileScope::User, None, HooksFileRead::user(home)));
    let project = project_root.map(|root| {
        (
            HooksFileScope::Project,
            Some(listed_trust),
            HooksFileRead::project(root, trust),
        )
    });
    let files = user
        .into_iter()
        .chain(project)
        .map(|(scope, trust, mut file)| {
            file.path = without_verbatim_prefix(file.path);
            (scope, trust, file)
        })
        .collect::<Vec<_>>();
    // The first invalid file is the one a session fails on.
    let invalid = files
        .iter()
        .find(|(_, _, file)| matches!(file.outcome, HooksFileOutcome::Invalid(_)))
        .map(|(_, _, file)| file.path.clone());
    files
        .into_iter()
        .map(|(scope, trust, file)| {
            let listed = listed(scope, file, invalid.as_deref());
            match trust {
                Some(trust) => listed.with_trust(trust),
                None => listed,
            }
        })
        .collect()
}

/// `file` as `rho/hooks/list` lists it, `disabled` by the file at
/// `invalid` when it would load. An error leaves out the path, which the
/// listing gives on its own, as the session hooks report does.
fn listed(scope: HooksFileScope, file: HooksFileRead, invalid: Option<&Path>) -> HooksFile {
    let (status, definitions, listing) = match file.outcome {
        HooksFileOutcome::Absent => (HooksFileStatus::Absent, Vec::new(), Listing::Loads),
        HooksFileOutcome::Loaded(hooks) => match invalid {
            None => (HooksFileStatus::Loaded, hooks, Listing::Loads),
            Some(by) => (
                HooksFileStatus::Disabled {
                    by: by.to_path_buf(),
                },
                hooks,
                Listing::Disabled,
            ),
        },
        HooksFileOutcome::Untrusted { definitions, error } => (
            HooksFileStatus::Untrusted {
                error: error.as_ref().map(HookConfigError::detail),
            },
            definitions,
            Listing::Untrusted,
        ),
        HooksFileOutcome::Invalid(error) => (
            HooksFileStatus::Invalid {
                error: error.detail(),
            },
            Vec::new(),
            Listing::Loads,
        ),
    };
    let hooks = definitions
        .iter()
        .map(|hook| contract(&spawn_contract(hook), listing.status(hook)))
        .collect();
    HooksFile::new(file.path, scope, status, hooks)
}

/// How the hooks of one listed file are listed. The file's status wins over
/// a hook's own `enabled`: an untrusted or disabled file's hooks are listed
/// so whether or not they are on.
#[derive(Clone, Copy)]
enum Listing {
    /// A session loads the file: each hook is active, or off when its file
    /// sets `enabled = false`.
    Loads,
    /// The file would load, but another file stops a session loading any.
    Disabled,
    /// The project's file, which the session does not trust.
    Untrusted,
}

impl Listing {
    /// How `hook`, one of the file's, is listed.
    fn status(self, hook: &HookDefinition) -> HookStatus {
        match self {
            Self::Loads if hook.enabled => HookStatus::Active,
            Self::Loads => HookStatus::Off,
            Self::Disabled => HookStatus::Disabled,
            Self::Untrusted => HookStatus::Untrusted,
        }
    }
}

#[cfg(test)]
#[path = "configured_tests.rs"]
mod tests;