refactor(drawing): simplify traits

This commit is contained in:
Ellen Emilia Anna Zscheile 2025-03-18 04:07:34 +01:00 committed by mikolaj
parent a836189086
commit 13f2400c45
5 changed files with 38 additions and 57 deletions

View File

@ -10,7 +10,7 @@ use super::{
gear::{GearIndex, GetNextGear},
graph::PrimitiveIndex,
loose::{GetPrevNextLoose, LooseIndex},
primitive::{GetInnerOuter, GetJoints},
primitive::GetJoints,
rules::AccessRules,
Drawing,
};

View File

@ -32,8 +32,7 @@ use crate::{
guide::Guide,
loose::{GetPrevNextLoose, Loose, LooseIndex},
primitive::{
GenericPrimitive, GetCore, GetInnerOuter, GetJoints, GetLimbs, GetOtherJoint,
MakePrimitiveShape,
GenericPrimitive, GetCore, GetJoints, GetLimbs, GetOtherJoint, MakePrimitiveShape,
},
rules::{AccessRules, GetConditions},
seg::{

View File

@ -10,7 +10,7 @@ use crate::{
bend::{BendIndex, FixedBendIndex, LooseBendIndex},
dot::FixedDotIndex,
graph::{MakePrimitive, PrimitiveIndex},
primitive::{FixedBend, FixedDot, GetFirstGear, GetInnerOuter, LooseBend, Primitive},
primitive::{FixedBend, FixedDot, GetFirstGear, LooseBend, Primitive},
rules::AccessRules,
Drawing,
},

View File

@ -17,7 +17,7 @@ use super::{
dot::{DotIndex, FixedDotIndex, LooseDotIndex},
graph::{MakePrimitive, PrimitiveIndex},
head::{BareHead, CaneHead, GetFace, Head},
primitive::{GetCore, GetInnerOuter, GetJoints, GetOtherJoint, GetWeight, MakePrimitiveShape},
primitive::{GetCore, GetJoints, GetOtherJoint, GetWeight, MakePrimitiveShape},
rules::{AccessRules, Conditions, GetConditions},
Drawing,
};

View File

@ -18,9 +18,9 @@ use crate::{
graph::{GenericIndex, GetPetgraphIndex},
};
#[enum_dispatch]
pub trait GetDrawing<'a, R: AccessRules> {
fn drawing(&self) -> &Drawing<impl Copy, R>;
pub trait GetDrawing {
type Rules: AccessRules;
fn drawing(&self) -> &Drawing<impl Copy, Self::Rules>;
}
#[enum_dispatch]
@ -55,8 +55,12 @@ pub trait GetInterior<T> {
fn interior(&self) -> Vec<T>;
}
pub trait GetOtherJoint<F: GetPetgraphIndex, T: GetPetgraphIndex + Into<F>>:
GetJoints<F, T>
pub trait GetOtherJoint<F, T>: GetJoints<F, T> {
fn other_joint(&self, end: F) -> F;
}
impl<F: GetPetgraphIndex, T: GetPetgraphIndex + Into<F>, S: GetJoints<F, T>> GetOtherJoint<F, T>
for S
{
fn other_joint(&self, end: F) -> F {
let joints = self.joints();
@ -72,7 +76,7 @@ pub trait GetJoints<F, T> {
fn joints(&self) -> (F, T);
}
pub trait GetFirstGear<'a, R: AccessRules>: GetDrawing<'a, R> + GetPetgraphIndex {
pub trait GetFirstGear: GetDrawing + GetPetgraphIndex {
fn first_gear(&self) -> Option<LooseBendIndex> {
self.drawing()
.geometry()
@ -85,7 +89,11 @@ pub trait GetBendIndex {
fn bend_index(&self) -> BendIndex;
}
pub trait GetCore<'a, R: AccessRules>: GetDrawing<'a, R> + GetBendIndex {
pub trait GetCore: GetBendIndex {
fn core(&self) -> FixedDotIndex;
}
impl<'a, S: GetDrawing + GetBendIndex> GetCore for S {
fn core(&self) -> FixedDotIndex {
FixedDotIndex::new(
self.drawing()
@ -96,22 +104,6 @@ pub trait GetCore<'a, R: AccessRules>: GetDrawing<'a, R> + GetBendIndex {
}
}
pub trait GetInnerOuter<'a, R: AccessRules>: GetDrawing<'a, R> + GetBendIndex {
fn inner(&self) -> Option<LooseBendIndex> {
self.drawing()
.geometry()
.inner(self.bend_index())
.map(|ni| LooseBendIndex::new(ni.petgraph_index()))
}
fn outer(&self) -> Option<LooseBendIndex> {
self.drawing()
.geometry()
.outer(self.bend_index())
.map(|ni| LooseBendIndex::new(ni.petgraph_index()))
}
}
macro_rules! impl_primitive {
($primitive_struct:ident, $weight_struct:ident) => {
impl<'a, CW: Copy, R: AccessRules> GetWeight<$weight_struct>
@ -218,7 +210,8 @@ impl<'a, W, CW: Copy, R: AccessRules> GetInterior<PrimitiveIndex>
}
}
impl<'a, W, CW: Copy, R: AccessRules> GetDrawing<'a, R> for GenericPrimitive<'a, W, CW, R> {
impl<'a, W, CW: Copy, R: AccessRules> GetDrawing for GenericPrimitive<'a, W, CW, R> {
type Rules = R;
fn drawing(&self) -> &Drawing<impl Copy, R> {
self.drawing
}
@ -277,7 +270,7 @@ impl<'a, CW: Copy, R: AccessRules> GetLimbs for FixedDot<'a, CW, R> {
}
}
impl<'a, CW: Copy, R: AccessRules> GetFirstGear<'a, R> for FixedDot<'a, CW, R> {}
impl<'a, CW: Copy, R: AccessRules> GetFirstGear for FixedDot<'a, CW, R> {}
pub type LooseDot<'a, CW, R> = GenericPrimitive<'a, LooseDotWeight, CW, R>;
impl_loose_primitive!(LooseDot, LooseDotWeight);
@ -342,11 +335,6 @@ impl<'a, CW: Copy, R: AccessRules> GetJoints<FixedDotIndex, FixedDotIndex> for F
}
}
impl<'a, CW: Copy, R: AccessRules> GetOtherJoint<FixedDotIndex, FixedDotIndex>
for FixedSeg<'a, CW, R>
{
}
pub type LoneLooseSeg<'a, CW, R> = GenericPrimitive<'a, LoneLooseSegWeight, CW, R>;
impl_loose_primitive!(LoneLooseSeg, LoneLooseSegWeight);
@ -370,11 +358,6 @@ impl<'a, CW: Copy, R: AccessRules> GetJoints<FixedDotIndex, FixedDotIndex>
}
}
impl<'a, CW: Copy, R: AccessRules> GetOtherJoint<FixedDotIndex, FixedDotIndex>
for LoneLooseSeg<'a, CW, R>
{
}
pub type SeqLooseSeg<'a, CW, R> = GenericPrimitive<'a, SeqLooseSegWeight, CW, R>;
impl_loose_primitive!(SeqLooseSeg, SeqLooseSegWeight);
@ -408,11 +391,6 @@ impl<'a, CW: Copy, R: AccessRules> GetJoints<DotIndex, LooseDotIndex> for SeqLoo
}
}
impl<'a, CW: Copy, R: AccessRules> GetOtherJoint<DotIndex, LooseDotIndex>
for SeqLooseSeg<'a, CW, R>
{
}
pub type FixedBend<'a, CW, R> = GenericPrimitive<'a, FixedBendWeight, CW, R>;
impl_fixed_primitive!(FixedBend, FixedBendWeight);
@ -442,12 +420,7 @@ impl<'a, CW: Copy, R: AccessRules> GetJoints<FixedDotIndex, FixedDotIndex>
}
}
impl<'a, CW: Copy, R: AccessRules> GetOtherJoint<FixedDotIndex, FixedDotIndex>
for FixedBend<'a, CW, R>
{
}
impl<'a, CW: Copy, R: AccessRules> GetFirstGear<'a, R> for FixedBend<'a, CW, R> {}
impl<'a, CW: Copy, R: AccessRules> GetCore<'a, R> for FixedBend<'a, CW, R> {} // TODO: Fixed bends don't have cores actually.
impl<'a, CW: Copy, R: AccessRules> GetFirstGear for FixedBend<'a, CW, R> {}
//impl<'a, R: QueryRules> GetInnerOuter for FixedBend<'a, CW, R> {}
pub type LooseBend<'a, CW, R> = GenericPrimitive<'a, LooseBendWeight, CW, R>;
@ -491,9 +464,18 @@ impl<'a, CW: Copy, R: AccessRules> GetJoints<LooseDotIndex, LooseDotIndex>
}
}
impl<'a, CW: Copy, R: AccessRules> GetOtherJoint<LooseDotIndex, LooseDotIndex>
for LooseBend<'a, CW, R>
{
impl<'a, CW: Copy, R: AccessRules> LooseBend<'a, CW, R> {
pub fn inner(&self) -> Option<LooseBendIndex> {
self.drawing()
.geometry()
.inner(self.bend_index())
.map(|ni| LooseBendIndex::new(ni.petgraph_index()))
}
pub fn outer(&self) -> Option<LooseBendIndex> {
self.drawing()
.geometry()
.outer(self.bend_index())
.map(|ni| LooseBendIndex::new(ni.petgraph_index()))
}
}
impl<'a, CW: Copy, R: AccessRules> GetCore<'a, R> for LooseBend<'a, CW, R> {}
impl<'a, CW: Copy, R: AccessRules> GetInnerOuter<'a, R> for LooseBend<'a, CW, R> {}