From 5d4a64b7a2ab5572fbc8f844549ea7dab1be8bf8 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 4 May 2022 19:51:14 +0100 Subject: [PATCH] size handling: Sort out outline trait error handling Instead of the `ought` function which can be applied anywhere, use the OutlineCalculable proof token to map the error at the trait call site. Signed-off-by: Ian Jackson --- src/shapelib.rs | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/shapelib.rs b/src/shapelib.rs index ed2085d7..088b05a1 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -31,7 +31,7 @@ pub struct GroupData { #[typetag::deserialize(tag="outline")] pub trait OutlineDefn: Debug + Sync + Send + 'static { fn check(&self, lgi: &GroupData) -> Result; - fn load(&self, lgi: &GroupData, svg_sz: PosC) -> Result; + fn load(&self, lgi: &GroupData, svg_sz: PosC) -> Result; } #[derive(Debug,Clone,Copy)] pub struct OutlineCalculable { } @@ -121,12 +121,6 @@ pub enum LibraryLoadError { #[error("bad item name (invalid characters) in {0:?}")] BadItemName(String), } -impl LibraryLoadError { - fn ought(&self) -> InternalError { - internal_error_bydebug(self) - } -} - #[derive(Error,Copy,Clone,Debug)] pub enum SubstErrorKind { #[error("missing or unrecognised token {0}")] MissingToken (&'static str), @@ -572,10 +566,11 @@ impl Contents { ig: &Instance, depth:SpecDepth) -> ItemSpecLoaded { let (svg_data, svg_sz) = self.load_svg(name, lib_name, &**name)?; - let outline = idata.group.d.outline.load(&idata.group, svg_sz)?; + let outline = idata.group.d.outline.load(&idata.group, svg_sz) + .map_err(idata.outline_calculable.map_err())?; let xform = FaceTransform::from_group(&idata.group.d) - .map_err(|e| SpE::InternalError(format!("reckoning transform: {}",&e)))?; + .map_err(idata.outline_calculable.map_err())?; let mut svgs = IndexVec::with_capacity(1); let svg = svgs.push(svg_data); @@ -619,7 +614,8 @@ impl Contents { occ.item_name.unnest::().unnest(), /* original: */ lib_name, name.as_str() )?; - let outline = idata.group.d.outline.load(&idata.group, occ_svg_sz)?; + let outline = idata.group.d.outline.load(&idata.group, occ_svg_sz) + .map_err(idata.outline_calculable.map_err())?; let xform = FaceTransform::from_group(&idata.group.d) .map_err(idata.outline_calculable.map_err())?; let loaded = OccInertLoaded { @@ -1042,10 +1038,11 @@ impl OutlineDefn for CircleDefn { fn check(&self, lgd: &GroupData) -> OutlineCalculable { Self::get_size(lgd).map(|_| OutlineCalculable{})? } - fn load(&self, lgd: &GroupData, _svg_sz: PosC) -> Result { - Ok(CircleShape { - diam: Self::get_size(lgd).map_err(|e| e.ought())?, - }.into()) + #[throws(LibraryLoadError)] + fn load(&self, lgd: &GroupData, _svg_sz: PosC) -> Outline { + CircleShape { + diam: Self::get_size(lgd)?, + }.into() } } impl CircleDefn { @@ -1116,10 +1113,9 @@ impl OutlineDefn for RectDefn { fn check(&self, lgd: &GroupData) -> OutlineCalculable { Self::get(lgd).map(|_| OutlineCalculable{})? } - fn load(&self, lgd: &GroupData, _svg_sz: PosC) -> Result { - Ok( - Self::get(lgd).map_err(|e| e.ought())?.into() - ) + #[throws(LibraryLoadError)] + fn load(&self, lgd: &GroupData, _svg_sz: PosC) -> Outline { + Self::get(lgd)?.into() } } impl RectDefn { -- 2.30.2