chiark / gitweb /
PosOffTableError: Introduce
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 28 Apr 2021 22:36:50 +0000 (23:36 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 28 Apr 2021 22:39:06 +0000 (23:39 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
daemon/api.rs
src/gamestate.rs

index e4a3c058712154ae4d18a3317e863e9e992803af..6b62f955e29d2b98c26164a4e516b4f37beb2227 100644 (file)
@@ -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,
index 08b926b71c19d7bc1d42881b4ab924e58153cd87..31931b0a5d478c5eecd8356d84554b97e77efb1f 100644 (file)
@@ -316,30 +316,34 @@ impl Timestamp {
   }
 }
 
-pub trait ClampTable: Sized {
-  fn clamped(self, range: Self) -> Result<Self, Self>;
+#[derive(Error,Debug,Copy,Clone)]
+pub struct PosOffTableError<T:Debug> { pub clamped: T }
+display_as_debug!{PosOffTableError<T>, <T:Debug>}
+
+pub trait ClampTable: Sized+Debug {
+  fn clamped(self, range: Self) -> Result<Self, PosOffTableError<Self>>;
 }
 
 impl ClampTable for Coord {
-  fn clamped(self, range: Coord) -> Result<Coord, Coord> {
-    if self < 0     { return Err(0,   ) }
-    if self > range { return Err(range) }
+  fn clamped(self, range: Coord) -> Result<Coord, PosOffTableError<Coord>> {
+    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<Pos, Pos> {
+  fn clamped(self, range: Pos) -> Result<Pos, PosOffTableError<Pos>> {
     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 }) }
   }
 }