chiark / gitweb /
Unify outlines from shapelib and spec
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 14 May 2022 22:09:04 +0000 (23:09 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 14 May 2022 22:09:04 +0000 (23:09 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/shapelib-toml.rs
src/shapelib.rs
src/spec.rs

index 9d8f119745b8f4053cde97115af2a33335af99dc..5dd16c5d6a130e9b2608c52be12f91883492ea93 100644 (file)
@@ -4,8 +4,6 @@
 
 use crate::prelude::*;
 
-use shapelib::OutlineDefnEnum;
-
 // At the implementation level, each loaded item contains an
 // `Arc<GroupDetails>`, which is simply stored directly.  The
 // `GroupDefn` is processed.
@@ -56,12 +54,12 @@ pub enum ScaleFitDetails { Fit, Cover, Stretch }
 #[serde(untagged)]
 pub enum OutlineDetails {
   Full(FullOutlineDetails), // introduced with mformat=2
-  Shape(OutlineDefnEnum),
+  Shape(Shape),
 }
 
 #[derive(Debug,Deserialize)]
 pub struct FullOutlineDetails {
-  shape: OutlineDefnEnum,
+  shape: Shape,
   #[serde(default)] size: Vec<f64>,
   #[serde(default)] scale: Option<f64>,
 }
@@ -69,7 +67,7 @@ pub struct FullOutlineDetails {
 impl OutlineDetails {
   // enum_access could perhaps do this but controlling the serde
   // would become confusing
-  pub fn shape(&self) -> OutlineDefnEnum { match self {
+  pub fn shape(&self) -> Shape { match self {
     OutlineDetails::Full(full) => full.shape,
     OutlineDetails::Shape(shape) => *shape,
   }}
index 4737e1d92653e98f178eb71ed4daf08eb03964eb..4e3277b7b2c26b6d39c71fe4b17c288eeec9c0dd 100644 (file)
@@ -788,7 +788,7 @@ impl GroupDetails {
 //---------- OutlineDefn etc. ----------
 
 #[ambassador::delegatable_trait]
-pub trait OutlineDefnTrait: Debug + Sync + Send + 'static {
+pub trait ShapeLoadableTrait: Debug + Sync + Send + 'static {
   /// Success or failure must not depend on `svg_sz`
   ///
   /// Called to *check* the group configuration before load, but
@@ -801,33 +801,14 @@ pub trait OutlineDefnTrait: Debug + Sync + Send + 'static {
   fn load_mf1(&self, group: &GroupData) -> Result<Outline,LLE>;
 }
 
-// We used to do this via typetag and Box<dyn OutlineDefn>
+// We used to do shape deser via typetag and Box<dyn OutlineDefn>
 //
-// But I didnt manage to get typetag to deserialise these unit variants.
-// Instead, we have this enum and a cheesy macro to impl OutlineDefn
-// by delegating to a freshly made (static) unit struct value.
-macro_rules! outline_defns {
-  { $( $Shape:ident ),* } => { paste!{
-
-    #[derive(Deserialize,Debug,Copy,Clone,Eq,PartialEq)]
-    pub enum OutlineDefnEnum {
-      $( $Shape, )*
-    }
-
-    impl OutlineDefnEnum {
-      fn defn(self) -> &'static dyn OutlineDefnTrait {
-        match self { $(
-          Self::$Shape => &[< $Shape Defn >] as _,
-        )* }
-      }
-    }
-
-  } }
-}
-
-outline_defns! { Circle, Rect }
+// But I didnt manage to get typetag to deserialise the way I wanted.
+// Instead, we have the Shape enum and a cheesy macro to impl OutlineDefn
+// by delegating to a freshly made (static) unit struct value,
+// - see outline_defn in mod outline in spec.rs.
 impl_via_ambassador!{
-  impl OutlineDefnTrait for OutlineDefnEnum { defn() }
+  impl ShapeLoadableTrait for Shape { shapelib_loadable() }
 }
 
 //---------- RectOutline ----------
@@ -882,9 +863,7 @@ impl OutlineTrait for RectOutline {
   }
 }
 
-#[derive(Deserialize,Debug)]
-struct RectDefn;
-impl OutlineDefnTrait for RectDefn {
+impl ShapeLoadableTrait for RectShapeIndicator {
   fn load(&self, size: PosC<f64>) -> Outline {
     RectOutline { xy: size }.into()
   }
@@ -919,9 +898,7 @@ impl OutlineTrait for CircleOutline {
   }
 }
 
-#[derive(Deserialize,Debug)]
-struct CircleDefn;
-impl OutlineDefnTrait for CircleDefn {
+impl ShapeLoadableTrait for CircleShapeIndicator {
   fn load(&self, size: PosC<f64>) -> Outline {
     let diam = size
       .coords.into_iter()
index a41ff8f7a316c42982f83a5135ba3e38028e5b70..ab0c5c820631d52b3956269ee4cac43bbf40129d 100644 (file)
@@ -263,6 +263,7 @@ mod outline {
     $( $Shape:ident  $serde:literal  ;)*
   } => { paste!{
     $( use crate::shapelib::[< $Shape Outline >]; )*
+
     #[dyn_upcast(OutlineTrait)]
     #[enum_dispatch(OutlineTrait)]
     #[derive(Clone,Debug,Serialize,Deserialize)]
@@ -270,6 +271,27 @@ mod outline {
     pub enum Outline { $(
       #[serde(rename=$serde)] [< $Shape Outline >],
     )* }
+
+    #[derive(Deserialize,Debug,Copy,Clone,Eq,PartialEq)]
+    pub enum Shape { $(
+      #[serde(rename=$serde)] [< $Shape >],
+    )* }
+
+    $(
+    #[derive(Deserialize,Debug)]
+    pub struct [< $Shape ShapeIndicator >];
+    )*
+
+    impl Shape {
+      pub fn shapelib_loadable(self)
+          -> &'static dyn shapelib::ShapeLoadableTrait
+      {
+        match self { $(
+          Self::$Shape => &[< $Shape ShapeIndicator >] as _,
+        )* }
+      }
+    }
+    
   } } }
 
   shape_defns! {