use rho_protocol::{
ConfigTable, ConfigTableGetParams, ConfigTableGetResponse, ConfigTableSetParams,
ConfigTableSetResponse, ErrorCode, RpcError,
};
use super::{change_error, file, ConfigStore};
use crate::config::{apply_table_changes, read_table, table_revision, EditableTable};
impl ConfigStore {
pub(in crate::app::daemon_host) async fn get_table(
&self,
params: ConfigTableGetParams,
) -> Result<ConfigTableGetResponse, RpcError> {
let table = editable(params.table)?;
let (revision, entries) = read_table(&file::read_document(&self.agent_path)?, table);
Ok(ConfigTableGetResponse::new(revision, entries))
}
pub(in crate::app::daemon_host) async fn set_table(
&self,
params: ConfigTableSetParams,
) -> Result<ConfigTableSetResponse, RpcError> {
let table = editable(params.table)?;
let mut file = self.agent.lock().await;
let mut edit = file::Edit::open(&self.agent_path)?;
if table_revision(&edit.doc, table) != params.revision {
return Err(RpcError::new(
ErrorCode::ConfigConflict,
"the table changed since that revision; read it again",
));
}
let loaded =
apply_table_changes(&mut edit.doc, table, ¶ms.changes).map_err(change_error)?;
let (doc, _) = self.save_agent(&mut file, edit, loaded)?;
Ok(ConfigTableSetResponse::new(table_revision(&doc, table)))
}
}
fn editable(table: ConfigTable) -> Result<EditableTable, RpcError> {
EditableTable::from_wire(table)
.ok_or_else(|| RpcError::new(ErrorCode::InvalidParams, "unknown config table"))
}