From 2a2c7254ad86529a2c8b39bf260e10eddad799e1 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 7 May 2022 12:22:23 +0100 Subject: [PATCH] shapelib: Break out resolve_square_size We're going to want to reuse this even more. Signed-off-by: Ian Jackson --- src/shapelib.rs | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/shapelib.rs b/src/shapelib.rs index 23781de4..f3382a9b 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -578,16 +578,10 @@ impl FaceTransform { [s,s] }; let centre = d.centre.map(Ok).unwrap_or_else(|| Ok::<_,LLE>({ - match d.size.as_slice() { - [a] => [a,a], - [a,b] => [a,b], - x => throw!(LLE::WrongNumberOfSizeDimensions { - got: x.len(), - expected: [1,2], - }), - }.iter().cloned().zip(&scale).map(|(size,scale)| { - size * 0.5 / scale - }) + resolve_square_size(&d.size)? + .coords.iter().cloned().zip(&scale).map(|(size,scale)| { + size * 0.5 / scale + }) .collect::>() .into_inner() .unwrap() @@ -604,6 +598,16 @@ impl FaceTransform { } } +#[throws(LLE)] +fn resolve_square_size(size: &[T]) -> PosC { + PosC{ coords: match size { + &[s] => [s,s], + &[w,h] => [w,h], + _ => throw!(LLE::WrongNumberOfSizeDimensions + { got: size.len(), expected: [1,2]}), + } } +} + //---------- Outlines ---------- impl ShapeCalculable { @@ -695,14 +699,7 @@ impl OutlineDefn for RectDefn { impl RectDefn { #[throws(LibraryLoadError)] fn get(group: &GroupData) -> RectShape { - RectShape { xy: PosC{ coords: - match group.d.size.as_slice() { - &[s] => [s,s], - s if s.len() == 2 => s.try_into().unwrap(), - size => throw!(LLE::WrongNumberOfSizeDimensions - { got: size.len(), expected: [1,2]}), - } - }} + RectShape { xy: resolve_square_size(&group.d.size)? } } } -- 2.30.2