chiark / gitweb /
wip load
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 19 Jul 2020 12:39:11 +0000 (13:39 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 19 Jul 2020 12:39:11 +0000 (13:39 +0100)
src/gamestate.rs
src/imports.rs
src/pieces.rs

index 795f072e9fbf43a98947dfebe5a421de53a44932..d81a03b96e0ad16baed11018761b05bea5c0bc2f 100644 (file)
@@ -94,6 +94,11 @@ pub struct PieceRenderInstructions {
   pub face : FaceId,
 }
 
+#[typetag::deserialize]
+pub trait PieceSpec {
+  fn load(self) -> Result<Box<dyn Piece>,SE>;
+}
+
 // ========== implementations ==========
 
 // ---------- simple data types ----------
index a249c9c4e7d9fbbffeae3fcce82aab3435ad8835..2fcdea6688a6e6c84badbb1aa2a6c360709e4a18 100644 (file)
@@ -21,6 +21,7 @@ pub use std::error::Error;
 pub use std::marker::PhantomData;
 pub use std::ops::{Deref,DerefMut};
 pub use std::fs;
+pub use std::mem;
   
 pub use thiserror::Error;
 pub use anyhow::{Context,anyhow};
index 78dc4d41127e843d58c0a8433de4aaf6469ed29e..87d5ba8e4c11243a63bddf8cbfd0c19953af0721 100644 (file)
@@ -125,44 +125,71 @@ impl TryFrom<SimpleShapeLoad> for SimpleShape {
 }
 
 impl SimpleShape {
-  #[throws(SE)]
   fn new_from_path(desc: String, path: String, approx_dia: Coord,
-                   colours: ColourMap) -> Self {
-    SimpleShapeLoad(SimpleShape {
+                   colours: ColourMap) -> Result<Box<dyn Piece>,SE> {
+    Ok(Box::new(SimpleShape {
       scaled_path : Default::default(),
       desc, approx_dia, path, colours,
-    }).try_into()?
+    }))
   }
-  #[throws(SE)]
-  fn new_circle(dia: Coord, colours: ColourMap) -> Self {
+  fn new_square(edgelen: Coord, colours: ColourMap) -> Result<Box<dyn Piece>,SE> {
     let unit_path =
-      "M 0 1  a 1 1 0 1 0 0 -2 \
-              a 1 1 0 1 0 0  2  z";
-    let scale = (dia as f64) * 0.5;
+      "M -1 -1 h 2 v 2 h -2 z";
+    let scale = (edgelen as f64) * 0.5;
     let path = svg_rescale_path(&unit_path, scale)?;
-    Self::new_from_path("circle".to_owned(), path, dia, colours)?
+    Ok(Self::new_from_path("square".to_owned(), path, edgelen, colours)?)
   }
+}
+
+#[derive(Deserialize)]
+#[derive(Debug,Default)]
+#[repr(transparent)]
+struct ColourSpec(String);
+
+impl TryFrom<ColourSpec> for Colour {
+  type Error = SE;
   #[throws(SE)]
-  fn new_square(edgelen: Coord, colours: ColourMap) -> Self {
+  fn try_from(spec: ColourSpec) -> Colour {
+    // xxx check syntax
+    spec.0
+  }
+}
+
+#[derive(Debug,Deserialize)]
+struct Disc {
+  diam : Coord,
+  faces : [ColourSpec; 2],
+}
+
+#[typetag::deserialize]
+impl PieceSpec for Disc {
+  #[throws(SE)]
+  fn load(mut self) -> Box<dyn Piece> {
     let unit_path =
-      "M -1 -1 h 2 v 2 h -2 z";
-    let scale = (edgelen as f64) * 0.5;
+      "M 0 1  a 1 1 0 1 0 0 -2 \
+              a 1 1 0 1 0 0  2  z";
+    let scale = (self.diam as f64) * 0.5;
     let path = svg_rescale_path(&unit_path, scale)?;
-    Self::new_from_path("square".to_owned(), path, edgelen, colours)?
+    let colours = self.faces
+      .iter_mut()
+      .map(|s| mem::take(s).try_into())
+      .collect::<Result<_,SE>>()?;
+    SimpleShape::new_from_path("circle".to_owned(), path, self.diam,
+                               colours)?
   }
 }
 
 pub fn xxx_make_pieces() -> Result<Vec<(Pos, Box<dyn Piece>)>,SE> {
   Ok(vec![
     ([ 90, 80 ],
-     Box::new(SimpleShape::new_circle(
-       20,
-       index_vec![ "red".to_string(), "grey".to_string() ],
-     )?)),
+     Disc {
+       diam : 20,
+       faces : [ ColourSpec("red".to_string()), ColourSpec("grey".to_string()) ]
+     }.load()?),
     ([ 90, 60 ],
-     Box::new(SimpleShape::new_square(
+     SimpleShape::new_square(
        20,
        index_vec![ "blue".to_string(), "grey".to_string() ],
-     )?)),
+     )?),
   ])
 }