diff --git a/src/graph.rs b/src/graph.rs index a991e63..703a055 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -34,26 +34,43 @@ pub enum Weight { Bend(BendWeight), } +macro_rules! impl_type { + ($weight_struct_name:ident, $weight_variant_name:ident, $index_struct_name:ident) => { + impl Retag for $weight_struct_name { + fn retag(&self, index: NodeIndex) -> Index { + Index::$weight_variant_name($index_struct_name { + node_index: index, + marker: PhantomData, + }) + } + } + + impl GetNet for $weight_struct_name { + fn net(&self) -> i64 { + self.net + } + } + + pub type $index_struct_name = GenericIndex<$weight_struct_name>; + + impl MakePrimitive for $index_struct_name { + fn primitive<'a>( + &self, + graph: &'a StableDiGraph, + ) -> Primitive<'a> { + Primitive::$weight_variant_name(GenericPrimitive::new(*self, graph)) + } + } + }; +} + #[derive(Debug, Clone, Copy, PartialEq)] pub struct DotWeight { pub net: i64, pub circle: Circle, } -impl Retag for DotWeight { - fn retag(&self, index: NodeIndex) -> Index { - Index::Dot(DotIndex { - node_index: index, - marker: PhantomData, - }) - } -} - -impl GetNet for DotWeight { - fn net(&self) -> i64 { - self.net - } -} +impl_type!(DotWeight, Dot, DotIndex); #[derive(Debug, Clone, Copy, PartialEq)] pub struct SegWeight { @@ -61,20 +78,7 @@ pub struct SegWeight { pub width: f64, } -impl Retag for SegWeight { - fn retag(&self, index: NodeIndex) -> Index { - Index::Seg(SegIndex { - node_index: index, - marker: PhantomData, - }) - } -} - -impl GetNet for SegWeight { - fn net(&self) -> i64 { - self.net - } -} +impl_type!(SegWeight, Seg, SegIndex); #[derive(Debug, Clone, Copy, PartialEq)] pub struct BendWeight { @@ -82,20 +86,7 @@ pub struct BendWeight { pub cw: bool, } -impl Retag for BendWeight { - fn retag(&self, index: NodeIndex) -> Index { - Index::Bend(BendIndex { - node_index: index, - marker: PhantomData, - }) - } -} - -impl GetNet for BendWeight { - fn net(&self) -> i64 { - self.net - } -} +impl_type!(BendWeight, Bend, BendIndex); #[derive(Debug, EnumAsInner, Clone, Copy, PartialEq)] pub enum Label { @@ -142,27 +133,3 @@ impl GetNodeIndex for GenericIndex { self.node_index } } - -pub type DotIndex = GenericIndex; - -impl MakePrimitive for DotIndex { - fn primitive<'a>(&self, graph: &'a StableDiGraph) -> Primitive<'a> { - Primitive::Dot(GenericPrimitive::new(*self, graph)) - } -} - -pub type SegIndex = GenericIndex; - -impl MakePrimitive for SegIndex { - fn primitive<'a>(&self, graph: &'a StableDiGraph) -> Primitive<'a> { - Primitive::Seg(GenericPrimitive::new(*self, graph)) - } -} - -pub type BendIndex = GenericIndex; - -impl MakePrimitive for BendIndex { - fn primitive<'a>(&self, graph: &'a StableDiGraph) -> Primitive<'a> { - Primitive::Bend(GenericPrimitive::new(*self, graph)) - } -} diff --git a/src/primitive.rs b/src/primitive.rs index e768047..e4580db 100644 --- a/src/primitive.rs +++ b/src/primitive.rs @@ -22,15 +22,7 @@ pub trait GetConnectable: GetNet + GetGraph { let this = self.net(); let other = index.primitive(self.graph()).net(); - if this == other { - true - } else if this == -1 || other == -1 { - true - } else if this == -2 || other == -2 { - false - } else { - this == other - } + (this == other) || this == -1 || other == -1 } }