From af9583160455826cbd4c42cdd8df80766868cb30 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 4 Jul 2020 03:11:20 +0100 Subject: [PATCH] pieces.byid --- src/bin/server.rs | 10 +--------- src/error.rs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 9 deletions(-) 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; +} -- 2.30.2