chiark / gitweb /
size handling: Sort out outline trait error handling
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 4 May 2022 18:51:14 +0000 (19:51 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 4 May 2022 18:51:14 +0000 (19:51 +0100)
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 <ijackson@chiark.greenend.org.uk>
src/shapelib.rs

index ed2085d75126688abd8f27749daf5f6afa7ac32a..088b05a156591a309b388d310ecaafba1edbedf2 100644 (file)
@@ -31,7 +31,7 @@ pub struct GroupData {
 #[typetag::deserialize(tag="outline")]
 pub trait OutlineDefn: Debug + Sync + Send + 'static {
   fn check(&self, lgi: &GroupData) -> Result<OutlineCalculable,LLE>;
-  fn load(&self, lgi: &GroupData, svg_sz: PosC<f64>) -> Result<Outline,IE>;
+  fn load(&self, lgi: &GroupData, svg_sz: PosC<f64>) -> Result<Outline,LLE>;
 }
 #[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::<GoodItemName>().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<f64>) -> Result<Outline,IE> {
-    Ok(CircleShape {
-      diam: Self::get_size(lgd).map_err(|e| e.ought())?,
-    }.into())
+  #[throws(LibraryLoadError)]
+  fn load(&self, lgd: &GroupData, _svg_sz: PosC<f64>) -> 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<f64>) -> Result<Outline,IE> {
-    Ok(
-      Self::get(lgd).map_err(|e| e.ought())?.into()
-    )
+  #[throws(LibraryLoadError)]
+  fn load(&self, lgd: &GroupData, _svg_sz: PosC<f64>) -> Outline {
+    Self::get(lgd)?.into()
   }
 }
 impl RectDefn {