From 9bc95e91e1ab0c90a28a1c84c3120ad103122bc3 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 12 Feb 2021 23:26:37 +0000 Subject: [PATCH] simplify SimplePieceSpec etc. Signed-off-by: Ian Jackson --- src/pieces.rs | 79 ++++++++++++++++++++++++--------------------------- 1 file changed, 37 insertions(+), 42 deletions(-) diff --git a/src/pieces.rs b/src/pieces.rs index 7c615cb9..a5e5aa92 100644 --- a/src/pieces.rs +++ b/src/pieces.rs @@ -138,50 +138,45 @@ impl Piece for SimpleShape { } impl SimpleShape { - fn new_from_path(desc: Html, path: Html, - faces: &IndexVec, - outline: Box, - itemname: String) - -> Result,SpecError> { + #[throws(SpecError)] + fn new(desc: Html, path: Html, + faces: &IndexVec, + outline: Box, + spec_itemname: &Option, + def_itemname: &'_ str) + -> SimpleShape + { + let itemname = spec_itemname.clone() + .unwrap_or_else(|| def_itemname.to_string()); let colours = faces .iter() .map(|s| s.try_into()) .collect::>()?; - Ok(Box::new(SimpleShape { + SimpleShape { desc, path, colours, itemname, outline, - })) + } } } trait SimplePieceSpec { - fn outline(&self) -> Result, SpecError>; - fn path(&self) -> Result; - fn faces(&self) -> Result<&FaceColourSpecs, SpecError>; - fn desc(&self) -> Result; - fn itemname(&self) -> Result; - - #[throws(SpecError)] - fn load(&self) -> Box { - SimpleShape::new_from_path(self.desc()?, - self.path()?, - self.faces()?, - self.outline()?, - self.itemname()?)? + fn load_simple(&self) -> Result; + fn load(&self) -> Result, SpecError> { + Ok(Box::new(self.load_simple()?)) } } 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) -> &FaceColourSpecs { &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()) + #[throws(SpecError)] + fn load_simple(&self) -> SimpleShape { + let outline = shapelib::Circle { diam: self.diam as f64 }; + SimpleShape::new( + Html::lit("disc"), + svg_circle_path(self.diam as f64)?, + &self.faces, + Box::new(outline), + &self.itemname, + "simple-disc", + )? } } @@ -203,17 +198,17 @@ impl piece_specs::Square { } 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) -> &FaceColourSpecs { &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()) + #[throws(SpecError)] + fn load_simple(&self) -> SimpleShape { + let outline = shapelib::Square { xy: self.xy()?.map(|v| v as f64) }; + SimpleShape::new( + Html::lit("square"), + svg_rectangle_path(self.xy()?.promote())?, + &self.faces, + Box::new(outline), + &self.itemname, + "simple-square", + )? } } -- 2.30.2