From: Ian Jackson Date: Mon, 22 Mar 2021 00:42:28 +0000 (+0000) Subject: shapelib refactoring: Change return type of load1 X-Git-Tag: otter-0.5.0~532 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=e5f6ec2b2e2976b144ae6b74201708d569998ced;p=otter.git shapelib refactoring: Change return type of load1 We don't want this to be type-erased; we need to reuse it as a different trait object. This means making the `Item` struct public, but nothing else about it. Signed-off-by: Ian Jackson --- diff --git a/src/bin/otterlib.rs b/src/bin/otterlib.rs index 38287f45..92b123b8 100644 --- a/src/bin/otterlib.rs +++ b/src/bin/otterlib.rs @@ -91,8 +91,8 @@ fn preview(items: Vec) { let mut pieces: Vec = items.into_iter().map(|it| { let spec = ItemSpec { lib: it.0, item: it.1.itemname }; (||{ - let loaded = spec.clone().load().context("load")?; - let p = loaded.p; // todo show occulted version too + let (p, _occultable) = spec.clone().load().context("load")?; + // todo show occulted version too let mut uos = vec![]; p.add_ui_operations(&mut uos, &GameState::dummy(), &GPiece::dummy()) .context("add uos")?; diff --git a/src/gamestate.rs b/src/gamestate.rs index 643047cc..782f440c 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -189,8 +189,10 @@ pub struct ApiPieceOpArgs<'a> { #[derive(Debug)] pub struct PieceSpecLoaded { pub p: Box, - pub occultable: Option<(OccultIlkName, Box)>, + pub occultable: PieceSpecLoadedOccultable, } +pub type PieceSpecLoadedOccultable = + Option<(OccultIlkName, Box)>; #[typetag::serde(tag="type")] pub trait PieceSpec: Debug + Sync + Send + 'static { diff --git a/src/shapelib.rs b/src/shapelib.rs index 9d51b585..c8014621 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -141,7 +141,7 @@ struct FaceTransform { } #[derive(Debug,Serialize,Deserialize)] -struct Item { +pub struct Item { itemname: String, faces: IndexVec, svgs: IndexVec, @@ -281,9 +281,17 @@ pub fn libs_lookup(libname: &str) ? } +pub type ItemSpecLoaded = (Box, PieceSpecLoadedOccultable); + +impl From for PieceSpecLoaded { + fn from((p, occultable): ItemSpecLoaded) -> PieceSpecLoaded { + PieceSpecLoaded { p, occultable } + } +} + impl ItemSpec { #[throws(SpecError)] - pub fn load(&self) -> PieceSpecLoaded { + pub fn load(&self) -> ItemSpecLoaded { let lib = libs_lookup(&self.lib)?; let idata = lib.items.get(&self.item) .ok_or(SpE::LibraryItemNotFound(self.item.clone()))?; @@ -310,7 +318,7 @@ impl Contents { } #[throws(SpecError)] - fn load1(&self, idata: &ItemData, name: &str) -> PieceSpecLoaded { + fn load1(&self, idata: &ItemData, name: &str) -> ItemSpecLoaded { let svg_data = self.load_svg(name, name)?; let occultable = match &idata.occ { @@ -364,8 +372,7 @@ impl Contents { let it = Item { faces, descs, svgs, outline, itemname: name.to_string() }; - let p = Box::new(it); - PieceSpecLoaded { p, occultable } + (Box::new(it), occultable) } #[throws(MgmtError)] @@ -375,16 +382,16 @@ impl Contents { let mut out = vec![]; for (k,v) in &self.items { if !pat.matches(&k) { continue } - let loaded = match self.load1(v, &k) { + let (loaded, _) = match self.load1(v, &k) { Err(SpecError::LibraryItemNotFound(_)) => continue, e@ Err(_) => e?, Ok(r) => r, }; - let f0bbox = loaded.p.bbox_approx()?; + let f0bbox = loaded.bbox_approx()?; let ier = ItemEnquiryData { itemname: k.clone(), f0bbox, - f0desc: loaded.p.describe_html(&GPiece::dummy())?, + f0desc: loaded.describe_html(&GPiece::dummy())?, }; out.push(ier); } @@ -394,24 +401,27 @@ impl Contents { #[typetag::serde(name="Lib")] impl PieceSpec for ItemSpec { + #[throws(SpecError)] fn load(&self, _: usize, _: &mut GPiece, _ir: &InstanceRef) - -> Result { - self.load() + -> PieceSpecLoaded { + self.load()?.into() } } #[typetag::serde(name="LibList")] impl PieceSpec for MultiSpec { fn count(&self) -> usize { self.items.len() } + + #[throws(SpecError)] fn load(&self, i: usize, _: &mut GPiece, _ir: &InstanceRef) - -> Result + -> PieceSpecLoaded { let item = self.items.get(i).ok_or_else( || SpE::InternalError(format!("item {:?} from {:?}", i, &self)) )?; let item = format!("{}{}{}", &self.prefix, item, &self.suffix); let lib = self.lib.clone(); - ItemSpec { lib, item }.load() + ItemSpec { lib, item }.load()?.into() } } diff --git a/src/spec.rs b/src/spec.rs index bb963c56..3369eb60 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -84,6 +84,7 @@ pub enum SpecError { SpecifiedWidthOfNoEdges, UnsupportedShape, NegativeTimeout, + ComplexPieceWhereSimpleRequired, } display_as_debug!{SpecError}