From: Ian Jackson Date: Sat, 4 Jul 2020 02:11:20 +0000 (+0100) Subject: pieces.byid X-Git-Tag: otter-0.2.0~1476 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=af9583160455826cbd4c42cdd8df80766868cb30;p=otter.git pieces.byid --- diff --git a/src/bin/server.rs b/src/bin/server.rs index 4f1b8ed6..744623f6 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -117,14 +117,6 @@ fn session(form : Json) -> Result { Ok(Template::render("test",&c)) } -#[derive(Error,Debug)] -#[error("operation error {:?}",self)] -enum OpError { - Conflict, - PieceGone, - PieceHeld, -} - #[derive(Debug,Serialize,Deserialize)] struct ApiGrab { t : String, @@ -145,7 +137,7 @@ fn api_grab(form : Json) -> impl response::Responder<'static> { let r : Result<(),OpError> = (||{ let piece = decode_visible_pieceid(form.p); let gs = &mut g.gs; - let p = gs.pieces.get_mut(piece).ok_or(OpError::PieceGone)?; + let p = gs.pieces.byid_mut(piece)?; let q_gen = form.g; let u_gen = if client == p.lastclient { p.gen_lastclient } diff --git a/src/error.rs b/src/error.rs index 54a0aa9a..b8c77e4b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,6 +3,14 @@ use crate::imports::*; use std::sync::PoisonError; +#[derive(Error,Debug)] +#[error("operation error {:?}",self)] +pub enum OpError { + Conflict, + PieceGone, + PieceHeld, +} + #[derive(Error,Debug)] pub enum OnlineError { #[error("Game corrupted by previous crash - consult administrator")] @@ -64,3 +72,8 @@ impl IdForById for T where T : AccessId { type Error = OE; const ERROR : OE = ::ERROR; } + +impl IdForById for PieceId { + type Error = OpError; + const ERROR : OpError = OpError::PieceGone; +}