rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! `rho/hooks/config/*`: the user's or a project's hooks file read and
//! edited one `[[hook]]` at a time, for a settings screen that adds, edits,
//! turns off, or removes one hook. The text edits are
//! `crate::hooks::apply_hook_changes`; the revision, lock, check, and write
//! are the store's (`hooks_store.rs`), as for `rho/hooks/file/*`.
//!
//! A project's file is found as `rho/hooks/list` finds it, and a write is
//! checked by the rules a session that trusts the project loads it with.
//! Writing needs no trust and grants none: a session still loads the file
//! only when its client's environment trusts the project.

use std::path::Path;

use rho_protocol::{
    ErrorCode, HooksConfigGetParams, HooksConfigGetResponse, HooksConfigScope,
    HooksConfigSetParams, HooksConfigSetResponse, RpcError,
};

use super::{
    hooks_store::{HooksStore, HooksTarget, Replacement},
    sessions::Workspace,
};
use crate::hooks::HookEditError;

/// Answers `rho/hooks/config/get`.
pub(super) fn get(
    store: &HooksStore,
    params: HooksConfigGetParams,
) -> Result<HooksConfigGetResponse, RpcError> {
    let snapshot = store.snapshot(&target_of(store, &params.scope)?)?;
    let hooks = match snapshot.text()? {
        None => Vec::new(),
        Some(text) => {
            crate::hooks::hook_entries(text).map_err(|error| refused(&snapshot.path, error))?
        }
    };
    Ok(HooksConfigGetResponse::new(
        snapshot.path,
        snapshot.revision,
        hooks,
    ))
}

/// Answers `rho/hooks/config/set`: applies the changes to the file as the
/// params' revision has it and writes the result once it loads as a session
/// would load it. A missing file, and a project's `.rho` directory, is
/// created.
pub(super) async fn set(
    store: &HooksStore,
    params: HooksConfigSetParams,
) -> Result<HooksConfigSetResponse, RpcError> {
    let target = target_of(store, &params.scope)?;
    let revision = store
        .edit(&target, &params.revision, |current| {
            crate::hooks::apply_hook_changes(current.text()?, &params.changes)
                .map(Replacement::Text)
                .map_err(|error| refused(&current.path, error))
        })
        .await?;
    Ok(HooksConfigSetResponse::new(revision))
}

/// The file `scope` names. A project's workspace is parsed as
/// `rho/session/new` parses its `cwd`.
fn target_of(store: &HooksStore, scope: &HooksConfigScope) -> Result<HooksTarget, RpcError> {
    match scope {
        HooksConfigScope::User => Ok(store.user()),
        HooksConfigScope::Project { workspace } => {
            store.project(Workspace::parse("workspace", workspace)?.path())
        }
    }
}

/// Why the file at `path` could not be read or edited one hook at a time:
/// a file that cannot be split into hooks is the file's fault, any other
/// refusal the changes'.
fn refused(path: &Path, error: HookEditError) -> RpcError {
    let code = match &error {
        HookEditError::FileInvalid(_) => ErrorCode::ConfigInvalid,
        HookEditError::Missing { .. }
        | HookEditError::NotTable { .. }
        | HookEditError::HoldsId { .. }
        | HookEditError::Field { .. } => ErrorCode::InvalidParams,
    };
    RpcError::new(code, format!("{}: {error}", path.display()))
}