Cotton code
42 lines · 1.7 KB · RustRaw
//! This Device's two acts, Add sample records and Remove sample records, run//! as the device page's definition commands (`samples.add`,//! `samples.remove`): this device's own write, through//! `cotton_tools::mcp::handlers::tables::samples`, which says what each does.use serde_json::Value;use crate::handle::CottonHandle;use crate::methods::MethodError;pub const ADD: &str = "samples.add";pub const REMOVE: &str = "samples.remove";/// The zone a sample's dates are set in when neither the device nor the/// database names one.const NO_ZONE: &str = "Etc/UTC";/// The command methods' door.pub(crate) fn call(handle: &CottonHandle, method: &str, _request: &Value) -> Result<Value, MethodError> {    use cotton_tools::mcp::handlers::tables::samples;    let client = handle.client();    let device_zone = client.time_zone().map(str::to_string);    let answer = client        .write_rows(method, &mut |ctx| {            let done = match method {                ADD => {                    let zone = match &device_zone {                        Some(zone) => zone.clone(),                        None => cotton_tools::mcp::handlers::common::workspace_timezone_or_null(ctx)                            .map_err(|failure| failure.to_string())?                            .unwrap_or_else(|| NO_ZONE.to_string()),                    };                    samples::add(ctx, &zone)                }                REMOVE => samples::remove(ctx),                _ => return Err(format!("`{method}` is not a sample act")),            };            done.map_err(|failure| failure.to_string())        })        .map_err(|error| MethodError::new("write_refused", error))?;    Ok(crate::authoring::own_answer(answer))}