From: Ian Jackson Date: Fri, 12 Feb 2021 19:13:35 +0000 (+0000) Subject: Refactor to introduce SimplePieceSpec X-Git-Tag: otter-0.4.0~534 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=e4a3bde7de97670001d8e893f7f6a5ee0d9f22ef;p=otter.git Refactor to introduce SimplePieceSpec NFC Signed-off-by: Ian Jackson --- diff --git a/src/pieces.rs b/src/pieces.rs index b5d873b3..10a1c126 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -153,33 +153,74 @@ impl SimpleShape { } } -#[typetag::serde] -impl PieceSpec for piece_specs::Disc { +type FacesSpec = IndexVec; + +trait SimplePieceSpec { + fn outline(&self) -> Result, SpecError>; + fn path(&self) -> Result; + fn faces(&self) -> Result<&FacesSpec, SpecError>; + fn desc(&self) -> Result; + fn itemname(&self) -> Result; + #[throws(SpecError)] - fn load(&self, _: usize) -> Box { - let outline = Box::new(shapelib::Circle { diam: self.diam as f64 }); - let path = svg_circle_path(self.diam as f64)?; - let itemname = self.itemname.clone() - .unwrap_or_else(||"simple-disc".to_string()); - SimpleShape::new_from_path(Html::lit("circle"), path, - &self.faces, outline, itemname)? + fn load(&self) -> Box { + SimpleShape::new_from_path(self.desc()?, + self.path()?, + self.faces()?, + self.outline()?, + self.itemname()?)? + } +} + +impl SimplePieceSpec for piece_specs::Disc { + fn outline(&self) -> Result, SpecError> { Ok(Box::new( + shapelib::Circle { diam: self.diam as f64 } + ))} + #[throws(SpecError)] fn path(&self) -> Html { + svg_circle_path(self.diam as f64)? + } + #[throws(SpecError)] fn faces(&self) -> &FacesSpec { &self.faces } + #[throws(SpecError)] fn desc(&self) -> Html { Html::lit("disc") } + #[throws(SpecError)] fn itemname(&self) -> String { + self.itemname.clone() + .unwrap_or_else(||"simple-disc".to_string()) } } #[typetag::serde] -impl PieceSpec for piece_specs::Square { +impl PieceSpec for piece_specs::Disc { + #[throws(SpecError)] + fn load(&self, _: usize) -> Box { SimplePieceSpec::load(self)? } +} + +impl piece_specs::Square { #[throws(SpecError)] - fn load(&self, _: usize) -> Box { - let xy = match *self.size.as_slice() { + fn xy(&self) -> Pos { + match *self.size.as_slice() { [s,] => PosC([s,s]), [x,y] => PosC([x,y]), _ => throw!(SpecError::ImproperSizeSpec), - }; - let outline = Box::new(shapelib::Square { xy: xy.map(|v| v as f64) }); - let path = svg_rectangle_path(xy.promote())?; - let itemname = self.itemname.clone() - .unwrap_or_else(||"simple-square".to_string()); - SimpleShape::new_from_path(Html::lit("square"), path, - &self.faces, outline, itemname)? + } + } +} + +impl SimplePieceSpec for piece_specs::Square { + fn outline(&self) -> Result, SpecError> { Ok(Box::new( + shapelib::Square { xy: self.xy()?.map(|v| v as f64) } + ))} + #[throws(SpecError)] fn path(&self) -> Html { + svg_rectangle_path(self.xy()?.promote())? + } + #[throws(SpecError)] fn faces(&self) -> &FacesSpec { &self.faces } + #[throws(SpecError)] fn desc(&self) -> Html { Html::lit("square") } + #[throws(SpecError)] fn itemname(&self) -> String { + self.itemname.clone() + .unwrap_or_else(||"simple-square".to_string()) } -} +} + +#[typetag::serde] +impl PieceSpec for piece_specs::Square { + #[throws(SpecError)] + fn load(&self, _: usize) -> Box { SimplePieceSpec::load(self)? } +} diff --git a/src/spec.rs b/src/spec.rs index 665695a0..73caaeaf 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -205,7 +205,10 @@ pub mod piece_specs { pub size: Vec, pub faces: IndexVec, } - +/* + pub struct Hand { + pub shape: Box, + }*/ } //---------- Pos ----------