chiark / gitweb /
mformat 2: Have resolve_square_size return Option
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 7 May 2022 11:45:51 +0000 (12:45 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 11 May 2022 23:19:17 +0000 (00:19 +0100)
This moves the optional-ness handling into resolve_square_size, and
pushes the error into the call sites.

The mformat 2 callers are going to want to default this, so want an
Option.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/shapelib.rs

index ca59bbcf389ba09dca1b01eba882989612f76afc..3eae365881f220851ad0f413efbb37630a2e8ae3 100644 (file)
@@ -137,6 +137,7 @@ pub enum LibraryLoadError {
 #[derive(Error,Debug,Clone,Copy,Serialize,Deserialize)]
 pub enum LibraryLoadMFIncompat {
   #[error("bad scale definition")] Scale,
+  #[error("size not specified")] SizeRequired,
 }
 pub use LibraryLoadMFIncompat as LLMI;
 
@@ -595,6 +596,7 @@ impl FaceTransform {
     };
     let centre = d.centre.map(Ok).unwrap_or_else(|| Ok::<_,LLE>({
       resolve_square_size(&d.size)?
+        .ok_or_else(|| group.mformat.incompat(LLMI::SizeRequired))?
         .coords.iter().cloned().zip(&scale).map(|(size,scale)| {
           size * 0.5 / scale
         })
@@ -615,13 +617,14 @@ impl FaceTransform {
 }
 
 #[throws(LLE)]
-fn resolve_square_size<T:Copy>(size: &[T]) -> PosC<T> {
-  PosC{ coords: match size {
+fn resolve_square_size<T:Copy>(size: &[T]) -> Option<PosC<T>> {
+  Some(PosC{ coords: match size {
+    [] => return None,
     &[s] => [s,s],
     &[w,h] => [w,h],
     _ => throw!(LLE::WrongNumberOfSizeDimensions
                 { got: size.len(), expected: [1,2]}),
-  } }
+  } })
 }
 
 //---------- Outlines ----------
@@ -720,13 +723,16 @@ struct RectDefn { }
 impl OutlineDefn for RectDefn {
   #[throws(LibraryLoadError)]
   fn load_mf1(&self, lgd: &GroupData, _svg_sz: PosC<f64>) -> Outline {
-    Self::get(lgd)?.into()
+    Self::get_mf1(lgd)?.into()
   }
 }
 impl RectDefn {
   #[throws(LibraryLoadError)]
-  fn get(group: &GroupData) -> RectShape {
-    RectShape { xy: resolve_square_size(&group.d.size)? }
+  fn get_mf1(group: &GroupData) -> RectShape {
+    RectShape {
+      xy: resolve_square_size(&group.d.size)?
+        .ok_or_else(|| group.mformat.incompat(LLMI::SizeRequired))?
+    }
   }
 }