chiark / gitweb /
spec errors: Expand WrongNumberOfFaces error
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 12 Apr 2022 20:33:51 +0000 (21:33 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 12 Apr 2022 20:35:05 +0000 (21:35 +0100)
We're going to want to reuse this in a new context.  And, this already
produces better error messages for the case where it's used now.

It seems better to provide this rather generic error rather than
expect each funky piece kind to invent its own kind in SpecError.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/deck.rs
src/spec.rs

index 55fe7ca3750f95f0240d588a13f4b20dad8ab9d2..e5eae521fbb94f5b6d7cf6fabbe21892bb1d4814 100644 (file)
@@ -55,7 +55,12 @@ impl PieceSpec for piece_specs::Deck {
       "magic-pickupdeck",
       &common)?;
     if shape.count_faces() != 2 {
-      throw!(SpE::WrongNumberOfFaces);
+      throw!(SpE::WrongNumberOfFaces {
+        got: shape.count_faces(),
+        got_why: "shape".into(),
+        exp: 2,
+        exp_why: "required".into(),
+      });
     }
     gpc.moveable = PieceMoveable::IfWresting;
     gpc.rotateable = false;
index 162a72f6931f04514313b862946edcb18ac555c6..4e2e7db6453aa59c03a77e11cc61986d2bfa47fc 100644 (file)
@@ -6,6 +6,7 @@
 
 use crate::imports::*;
 
+use std::borrow::Cow;
 use std::collections::hash_map::HashMap;
 use std::collections::hash_set::HashSet;
 use std::fmt::Debug;
@@ -74,7 +75,6 @@ pub enum SpecError {
   #[error("compass angle invalid")]          CompassAngleInvalid,
   #[error("piece has zero faces")]           ZeroFaces,
   #[error("inconsistent face/edge colours")] InconsistentFacesEdgecoloursCount,
-  #[error("wrong number of faces")]          WrongNumberOfFaces,
   #[error("specified with of edges, but no edges")] SpecifiedWidthOfNoEdges,
   #[error("shape not supported")]            UnsupportedShape,
   #[error("negative timeout")]               NegativeTimeout,
@@ -82,6 +82,13 @@ pub enum SpecError {
   #[error("piece alias not found")]          AliasNotFound,
   #[error("piece alias target is multi spec")] AliasTargetMultiSpec,
   #[error("piece alias loop")]               AliasLoop(String),
+  #[error("wrong number of faces {got_why}={got} != {exp_why}={exp}")]
+  WrongNumberOfFaces {
+    got: RawFaceId,
+    exp: RawFaceId,
+    got_why: Cow<'static, str>,
+    exp_why: Cow<'static, str>,
+  }
 }
 
 //---------- Bundle "otter.toml" file ----------