From 39351a2a2f311840a6016b0fa033769ca73efc38 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 14 May 2022 23:09:04 +0100 Subject: [PATCH] Unify outlines from shapelib and spec Signed-off-by: Ian Jackson --- src/shapelib-toml.rs | 8 +++----- src/shapelib.rs | 41 +++++++++-------------------------------- src/spec.rs | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 37 deletions(-) diff --git a/src/shapelib-toml.rs b/src/shapelib-toml.rs index 9d8f1197..5dd16c5d 100644 --- a/src/shapelib-toml.rs +++ b/src/shapelib-toml.rs @@ -4,8 +4,6 @@ use crate::prelude::*; -use shapelib::OutlineDefnEnum; - // At the implementation level, each loaded item contains an // `Arc`, 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, #[serde(default)] scale: Option, } @@ -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, }} diff --git a/src/shapelib.rs b/src/shapelib.rs index 4737e1d9..4e3277b7 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -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; } -// We used to do this via typetag and Box +// We used to do shape deser via typetag and Box // -// 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) -> 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) -> Outline { let diam = size .coords.into_iter() diff --git a/src/spec.rs b/src/spec.rs index a41ff8f7..ab0c5c82 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -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! { -- 2.30.2