From 30af956fab6228913bc9876db8961180e28643c9 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 28 Apr 2021 23:36:50 +0100 Subject: [PATCH] PosOffTableError: Introduce Signed-off-by: Ian Jackson --- daemon/api.rs | 4 ++-- src/gamestate.rs | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/daemon/api.rs b/daemon/api.rs index e4a3c058..6b62f955 100644 --- a/daemon/api.rs +++ b/daemon/api.rs @@ -486,8 +486,8 @@ api_route!{ let logents = vec![]; match self.0.clamped(gs.table_size) { Ok(pos) => gpc.pos = pos, - Err(pos) => { - gpc.pos = pos; + Err(pote) => { + gpc.pos = pote.clamped; throw!(ApiPieceOpError::PartiallyProcessed( PieceOpError::PosOffTable, logents, diff --git a/src/gamestate.rs b/src/gamestate.rs index 08b926b7..31931b0a 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -316,30 +316,34 @@ impl Timestamp { } } -pub trait ClampTable: Sized { - fn clamped(self, range: Self) -> Result; +#[derive(Error,Debug,Copy,Clone)] +pub struct PosOffTableError { pub clamped: T } +display_as_debug!{PosOffTableError, } + +pub trait ClampTable: Sized+Debug { + fn clamped(self, range: Self) -> Result>; } impl ClampTable for Coord { - fn clamped(self, range: Coord) -> Result { - if self < 0 { return Err(0, ) } - if self > range { return Err(range) } + fn clamped(self, range: Coord) -> Result> { + if self < 0 { return Err(PosOffTableError{ clamped: 0, }) } + if self > range { return Err(PosOffTableError{ clamped: range }) } return Ok(self) } } impl ClampTable for Pos { - fn clamped(self, range: Pos) -> Result { + fn clamped(self, range: Pos) -> Result> { let mut output = ArrayVec::new(); let mut ok = true; for (&pos, &rng) in izip!(self.coords.iter(), range.coords.iter()) { output.push(match pos.clamped(rng) { Ok(pos) => pos, - Err(pos) => { ok = false; pos }, + Err(e) => { ok = false; e.clamped }, }) } let output = PosC{ coords: output.into_inner().unwrap() }; - if ok { Ok(output) } else { Err(output) } + if ok { Ok(output) } else { Err(PosOffTableError { clamped: output }) } } } -- 2.30.2