Cotton code
46 lines · 1.6 KB · RustRaw
//! `camera.shot` — a shot from the Camera tool, saved the moment it is taken.//!//! A shot is `capture.media` (the bytes, the photo row, the upload ledger, in//! that order), held to what a camera makes: a photo or a video.use serde_json::Value;use cotton_client::layout::Layout;use cotton_core::media::keys::MediaKind;use cotton_tools::mcp::dispatch::Ctx;use crate::handle::CottonHandle;use crate::methods::MethodError;use crate::phone::media::{validate_capture, write_media, CaptureMediaArgs};pub fn shot(handle: &CottonHandle, request: &Value) -> Result<Value, MethodError> {    let args = validate_shot(request)?;    let client = handle.client();    let layout = client.layout().clone();    let device_key = client.self_device_id();    let answer = client        .write_rows("camera.shot", &mut |ctx| {            write_shot(ctx, &layout, &args, device_key.as_deref())        })        .map_err(|reason| MethodError::new("capture_refused", reason))?;    Ok(crate::authoring::own_answer(answer))}/// `capture.media`'s arguments, held to what a camera makes.pub fn validate_shot(request: &Value) -> Result<CaptureMediaArgs, MethodError> {    let args = validate_capture(request)?;    if !matches!(args.kind, MediaKind::Photo | MediaKind::Video) {        return Err(MethodError::new("bad_request", "a camera shot is a photo or a video"));    }    Ok(args)}/// The shot, so a test can drive it over a `Ctx`.pub fn write_shot(    ctx: &mut Ctx<'_>,    layout: &Layout,    args: &CaptureMediaArgs,    device_key: Option<&str>,) -> Result<Value, String> {    write_media(ctx, layout, args, device_key)}