chiark / gitweb /
spec: Make table size and colour defaulted in serde
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 31 Mar 2021 18:23:47 +0000 (19:23 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 1 Apr 2021 10:31:37 +0000 (11:31 +0100)
This way `reset` works properly, always resetting the colour to the
default.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bin/otter.rs
src/gamestate.rs
src/spec.rs
wdriver/wdt-simple.rs

index 9d0e750181b269cd5f11399505fa2df22313f0da..5ebb3689d3f39ab21fa60ef062d9a887350db96f 100644 (file)
@@ -700,12 +700,8 @@ mod reset_game {
       insns.push(MGI::DefinePieceAlias{ alias, target });
     }
 
-    if let Some(table_size) = table_size {
-      insns.push(MGI::SetTableSize(table_size));
-    }
-    if let Some(table_colour) = table_colour {
-      insns.push(MGI::SetTableColour(table_colour));
-    }
+    insns.push(MGI::SetTableSize(table_size));
+    insns.push(MGI::SetTableColour(table_colour));
 
     for pspec in pieces.into_iter() {
       insns.push(MGI::AddPieces(pspec));
index a1c73509623fb005afa0f6ada564738389a53dd6..7658d740ca41192e2d1f2a67a0642f277595b575 100644 (file)
@@ -24,6 +24,7 @@ visible_slotmap_key!{ VisiblePieceId(b'.') }
 pub struct Timestamp(pub u64); /* time_t */
 
 pub const DEFAULT_TABLE_SIZE: Pos = PosC::new( 400, 200 );
+pub const DEFAULT_TABLE_COLOUR: &str = "green";
 
 // ---------- general data types ----------
 
index 540800cf97d1e9c98c569c4f35b200fb7f4c5e45..921a1721ab8897fb44ef0a5a04f844dbfab3fb37 100644 (file)
@@ -164,9 +164,9 @@ pub struct UrlOnStdout;
 
 #[derive(Debug,Serialize,Deserialize)]
 pub struct GameSpec {
-  pub table_size: Option<Pos>,
+  #[serde(default="imp::def_table_size")] pub table_size: Pos,
   pub pieces: Vec<PiecesSpec>,
-  pub table_colour: Option<ColourSpec>,
+  #[serde(default="imp::def_table_colour")] pub table_colour: ColourSpec,
   #[serde(default)] pub pcaliases: HashMap<String, Box<dyn PieceSpec>>,
 }
 
@@ -310,6 +310,13 @@ pub mod imp {
   type AS = AccountScope;
   type TPS = TablePlayerSpec;
 
+  pub fn def_table_size() -> Pos {
+    DEFAULT_TABLE_SIZE
+  }
+  pub fn def_table_colour() -> ColourSpec {
+    ColourSpec(DEFAULT_TABLE_COLOUR.into())
+  }
+
   impl Default for piece_specs::PieceLabelPlace {
     fn default() -> Self { Self::BottomLeft }
   }
index 8e9a51d9fdc838fcc4ebaaf3ea1408dbb0fd989e..4fc177691d3618a4448b36113fc1bba0a43f5482 100644 (file)
@@ -96,8 +96,7 @@ impl Ctx {
       Ok::<_,AE>(())
     };
 
-    let table_size = self.spec.table_size
-      .ok_or(anyhow!("table size missing from spec"))?;
+    let table_size = self.spec.table_size;
 
     let exp_end = {
       let mut w = su.w(&self.alice)?;