From e38a059373f90ee8aff3263b559199cd99b23aee Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 19 Jul 2020 13:39:11 +0100 Subject: [PATCH] wip load --- src/gamestate.rs | 5 ++++ src/imports.rs | 1 + src/pieces.rs | 67 +++++++++++++++++++++++++++++++++--------------- 3 files changed, 53 insertions(+), 20 deletions(-) diff --git a/src/gamestate.rs b/src/gamestate.rs index 795f072e..d81a03b9 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -94,6 +94,11 @@ pub struct PieceRenderInstructions { pub face : FaceId, } +#[typetag::deserialize] +pub trait PieceSpec { + fn load(self) -> Result,SE>; +} + // ========== implementations ========== // ---------- simple data types ---------- diff --git a/src/imports.rs b/src/imports.rs index a249c9c4..2fcdea66 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -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}; diff --git a/src/pieces.rs b/src/pieces.rs index 78dc4d41..87d5ba8e 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -125,44 +125,71 @@ impl TryFrom 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,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,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 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 { 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::>()?; + SimpleShape::new_from_path("circle".to_owned(), path, self.diam, + colours)? } } pub fn xxx_make_pieces() -> Result)>,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() ], - )?)), + )?), ]) } -- 2.30.2