rho-coding-agent 3.13.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! `rho/hooks/file/*`: the user's hooks file as text, read and replaced
//! whole, so a client can copy it from one daemon to another. The revision,
//! lock, check, and write are the store's (`hooks_store.rs`).

use rho_protocol::{HooksFileGetResponse, HooksFileSetParams, HooksFileSetResponse, RpcError};

use super::hooks_store::{HooksStore, Replacement};

/// Answers `rho/hooks/file/get`.
pub(super) fn get(store: &HooksStore) -> Result<HooksFileGetResponse, RpcError> {
    let snapshot = store.snapshot(&store.user())?;
    let text = snapshot.text()?.map(str::to_owned);
    Ok(HooksFileGetResponse::new(
        snapshot.path,
        snapshot.revision,
        text,
    ))
}

/// Answers `rho/hooks/file/set`: writes the text, or deletes the file when
/// there is none, while the file is still at the params' revision.
pub(super) async fn set(
    store: &HooksStore,
    params: HooksFileSetParams,
) -> Result<HooksFileSetResponse, RpcError> {
    let replacement = params.text.map_or(Replacement::Delete, Replacement::Text);
    let revision = store
        .edit(&store.user(), &params.revision, |_| Ok(replacement))
        .await?;
    Ok(HooksFileSetResponse::new(revision))
}