refactor: autogenerate getters with `derive-getters` crate

This commit is contained in:
Mikolaj Wielgus 2024-10-11 18:43:15 +02:00
parent bdc021cb6e
commit 6104e761d2
10 changed files with 24 additions and 70 deletions

View File

@ -1,3 +1,4 @@
use derive_getters::Getters;
use petgraph::graph::EdgeIndex; use petgraph::graph::EdgeIndex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use spade::InsertionError; use spade::InsertionError;
@ -43,6 +44,7 @@ pub enum AutorouterError {
NeedExactlyTwoRatlines, NeedExactlyTwoRatlines,
} }
#[derive(Getters)]
pub struct Autorouter<M: AccessMesadata> { pub struct Autorouter<M: AccessMesadata> {
pub(super) board: Board<M>, pub(super) board: Board<M>,
pub(super) ratsnest: Ratsnest, pub(super) ratsnest: Ratsnest,
@ -201,12 +203,4 @@ impl<M: AccessMesadata> Autorouter<M> {
}) })
.collect() .collect()
} }
pub fn board(&self) -> &Board<M> {
&self.board
}
pub fn ratsnest(&self) -> &Ratsnest {
&self.ratsnest
}
} }

View File

@ -2,7 +2,7 @@
//! Handles error scenarios related to command history, maintaining lists of executed //! Handles error scenarios related to command history, maintaining lists of executed
//! and undone commands for easy navigation. //! and undone commands for easy navigation.
use derive_getters::Dissolve; use derive_getters::{Dissolve, Getters};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use thiserror::Error; use thiserror::Error;
@ -16,7 +16,7 @@ pub enum HistoryError {
NoNextCommand, NoNextCommand,
} }
#[derive(Debug, Default, Clone, Dissolve, Serialize, Deserialize)] #[derive(Debug, Default, Clone, Getters, Dissolve, Serialize, Deserialize)]
pub struct History { pub struct History {
done: Vec<Command>, done: Vec<Command>,
undone: Vec<Command>, undone: Vec<Command>,
@ -60,12 +60,4 @@ impl History {
pub fn last_undone(&self) -> Result<&Command, HistoryError> { pub fn last_undone(&self) -> Result<&Command, HistoryError> {
self.undone.last().ok_or(HistoryError::NoNextCommand) self.undone.last().ok_or(HistoryError::NoNextCommand)
} }
pub fn done(&self) -> &[Command] {
&self.done
}
pub fn undone(&self) -> &[Command] {
&self.undone
}
} }

View File

@ -3,7 +3,7 @@
use std::cmp::Ordering; use std::cmp::Ordering;
use contracts_try::debug_requires; use contracts_try::debug_requires;
use derive_getters::Dissolve; use derive_getters::{Dissolve, Getters};
use enum_dispatch::enum_dispatch; use enum_dispatch::enum_dispatch;
use thiserror::Error; use thiserror::Error;
@ -70,7 +70,7 @@ impl TryInto<()> for InvokerStatus {
} }
} }
#[derive(Dissolve)] #[derive(Getters, Dissolve)]
pub struct Invoker<M: AccessMesadata> { pub struct Invoker<M: AccessMesadata> {
pub(super) autorouter: Autorouter<M>, pub(super) autorouter: Autorouter<M>,
pub(super) history: History, pub(super) history: History,
@ -199,12 +199,4 @@ impl<M: AccessMesadata> Invoker<M> {
self.history.set_undone(undone.into_iter()); self.history.set_undone(undone.into_iter());
} }
pub fn autorouter(&self) -> &Autorouter<M> {
&self.autorouter
}
pub fn history(&self) -> &History {
&self.history
}
} }

View File

@ -1,6 +1,7 @@
use std::{cmp::Ordering, collections::HashMap}; use std::{cmp::Ordering, collections::HashMap};
use bimap::BiHashMap; use bimap::BiHashMap;
use derive_getters::Getters;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{ use crate::{
@ -41,10 +42,13 @@ impl BandName {
/// ///
/// The struct manages the relationships between board's layout, /// The struct manages the relationships between board's layout,
/// and its compounds, as well as provides methods to manipulate them. /// and its compounds, as well as provides methods to manipulate them.
#[derive(Debug)] #[derive(Debug, Getters)]
pub struct Board<M: AccessMesadata> { pub struct Board<M: AccessMesadata> {
layout: Layout<M>, layout: Layout<M>,
// TODO: Simplify access logic to these members so that `#[getter(skip)]`s can be removed.
#[getter(skip)]
node_to_pinname: HashMap<NodeIndex, String>, node_to_pinname: HashMap<NodeIndex, String>,
#[getter(skip)]
band_bandname: BiHashMap<BandUid, BandName>, band_bandname: BiHashMap<BandUid, BandName>,
} }
@ -231,11 +235,6 @@ impl<M: AccessMesadata> Board<M> {
self.layout.drawing().rules() self.layout.drawing().rules()
} }
/// Returns the layout managed by this board.
pub fn layout(&self) -> &Layout<M> {
&self.layout
}
/// Returns a mutable reference to the layout, allowing modifications. /// Returns a mutable reference to the layout, allowing modifications.
pub fn layout_mut(&mut self) -> &mut Layout<M> { pub fn layout_mut(&mut self) -> &mut Layout<M> {
&mut self.layout &mut self.layout

View File

@ -1,4 +1,5 @@
use contracts_try::{debug_ensures, debug_invariant}; use contracts_try::{debug_ensures, debug_invariant};
use derive_getters::Getters;
use enum_dispatch::enum_dispatch; use enum_dispatch::enum_dispatch;
use geo::Point; use geo::Point;
@ -64,7 +65,7 @@ pub struct Collision(pub PrimitiveShape, pub PrimitiveIndex);
#[error("{1:?} is already connected to net {0}")] #[error("{1:?} is already connected to net {0}")]
pub struct AlreadyConnected(pub usize, pub PrimitiveIndex); pub struct AlreadyConnected(pub usize, pub PrimitiveIndex);
#[derive(Debug)] #[derive(Debug, Getters)]
pub struct Drawing<CW: Copy, R: AccessRules> { pub struct Drawing<CW: Copy, R: AccessRules> {
geometry_with_rtree: GeometryWithRtree< geometry_with_rtree: GeometryWithRtree<
PrimitiveWeight, PrimitiveWeight,
@ -890,10 +891,6 @@ impl<CW: Copy, R: AccessRules> Drawing<CW, R> {
self.geometry_with_rtree.rtree() self.geometry_with_rtree.rtree()
} }
pub fn rules(&self) -> &R {
&self.rules
}
#[debug_ensures(self.geometry_with_rtree.graph().node_count() == old(self.geometry_with_rtree.graph().node_count()))] #[debug_ensures(self.geometry_with_rtree.graph().node_count() == old(self.geometry_with_rtree.graph().node_count()))]
#[debug_ensures(self.geometry_with_rtree.graph().edge_count() == old(self.geometry_with_rtree.graph().edge_count()))] #[debug_ensures(self.geometry_with_rtree.graph().edge_count() == old(self.geometry_with_rtree.graph().edge_count()))]
pub fn rules_mut(&mut self) -> &mut R { pub fn rules_mut(&mut self) -> &mut R {
@ -917,7 +914,7 @@ impl<CW: Copy, R: AccessRules> Drawing<CW, R> {
} }
pub fn layer_count(&self) -> usize { pub fn layer_count(&self) -> usize {
self.geometry_with_rtree.layer_count() *self.geometry_with_rtree.layer_count()
} }
pub fn node_count(&self) -> usize { pub fn node_count(&self) -> usize {

View File

@ -1,5 +1,6 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use derive_getters::Getters;
use enum_dispatch::enum_dispatch; use enum_dispatch::enum_dispatch;
use geo::Point; use geo::Point;
use petgraph::{ use petgraph::{
@ -68,7 +69,7 @@ pub trait AccessDotWeight<CW>: GetPos + SetPos + GetWidth + Into<CW> + Copy {}
pub trait AccessSegWeight<CW>: GetWidth + Into<CW> + Copy {} pub trait AccessSegWeight<CW>: GetWidth + Into<CW> + Copy {}
pub trait AccessBendWeight<CW>: GetOffset + SetOffset + GetWidth + Into<CW> + Copy {} pub trait AccessBendWeight<CW>: GetOffset + SetOffset + GetWidth + Into<CW> + Copy {}
#[derive(Debug)] #[derive(Debug, Getters)]
pub struct Geometry< pub struct Geometry<
PW: GetWidth + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<PI> + Copy, PW: GetWidth + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<PI> + Copy,
DW: AccessDotWeight<PW>, DW: AccessDotWeight<PW>,
@ -478,10 +479,6 @@ impl<
}) })
.map(|ni| self.primitive_weight(ni).retag(ni)) .map(|ni| self.primitive_weight(ni).retag(ni))
} }
pub fn graph(&self) -> &StableDiGraph<GenericNode<PW, CW>, GeometryLabel, usize> {
&self.graph
}
} }
impl< impl<

View File

@ -1,6 +1,7 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use contracts_try::debug_invariant; use contracts_try::debug_invariant;
use derive_getters::Getters;
use geo::Point; use geo::Point;
use petgraph::stable_graph::StableDiGraph; use petgraph::stable_graph::StableDiGraph;
use rstar::{primitives::GeomWithData, Envelope, RTree, RTreeObject, AABB}; use rstar::{primitives::GeomWithData, Envelope, RTree, RTreeObject, AABB};
@ -36,7 +37,7 @@ impl RTreeObject for Bbox {
pub type BboxedIndex<I> = GeomWithData<Bbox, I>; pub type BboxedIndex<I> = GeomWithData<Bbox, I>;
#[derive(Debug)] #[derive(Debug, Getters)]
pub struct GeometryWithRtree< pub struct GeometryWithRtree<
PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<PI> + Copy, PW: GetWidth + GetLayer + TryInto<DW> + TryInto<SW> + TryInto<BW> + Retag<PI> + Copy,
DW: AccessDotWeight<PW> + GetLayer, DW: AccessDotWeight<PW> + GetLayer,
@ -347,19 +348,6 @@ impl<
} }
} }
pub fn layer_count(&self) -> usize {
self.layer_count
}
pub fn geometry(&self) -> &Geometry<PW, DW, SW, BW, CW, PI, DI, SI, BI> {
&self.geometry
}
// XXX: The type appears wrong? I don't think it should contain CW?
pub fn rtree(&self) -> &RTree<BboxedIndex<GenericNode<PI, GenericIndex<CW>>>> {
&self.rtree
}
pub fn graph(&self) -> &StableDiGraph<GenericNode<PW, CW>, GeometryLabel, usize> { pub fn graph(&self) -> &StableDiGraph<GenericNode<PW, CW>, GeometryLabel, usize> {
self.geometry.graph() self.geometry.graph()
} }

View File

@ -1,4 +1,5 @@
use contracts_try::debug_ensures; use contracts_try::debug_ensures;
use derive_getters::Getters;
use enum_dispatch::enum_dispatch; use enum_dispatch::enum_dispatch;
use geo::Point; use geo::Point;
use rstar::AABB; use rstar::AABB;
@ -35,7 +36,7 @@ pub enum CompoundWeight {
pub type NodeIndex = GenericNode<PrimitiveIndex, GenericIndex<CompoundWeight>>; pub type NodeIndex = GenericNode<PrimitiveIndex, GenericIndex<CompoundWeight>>;
#[derive(Debug)] #[derive(Debug, Getters)]
pub struct Layout<R: AccessRules> { pub struct Layout<R: AccessRules> {
drawing: Drawing<CompoundWeight, R>, drawing: Drawing<CompoundWeight, R>,
} }
@ -256,10 +257,6 @@ impl<R: AccessRules> Layout<R> {
.compound_members(GenericIndex::new(poly.petgraph_index())) .compound_members(GenericIndex::new(poly.petgraph_index()))
} }
pub fn drawing(&self) -> &Drawing<CompoundWeight, R> {
&self.drawing
}
pub fn rules(&self) -> &R { pub fn rules(&self) -> &R {
self.drawing.rules() self.drawing.rules()
} }

View File

@ -27,7 +27,7 @@ impl RouteStepper {
to: FixedDotIndex, to: FixedDotIndex,
width: f64, width: f64,
) -> Result<Self, NavmeshError> { ) -> Result<Self, NavmeshError> {
let navmesh = Navmesh::new(router.layout(), from, to, router.options())?; let navmesh = Navmesh::new(router.layout(), from, to, router.options().clone())?;
Ok(Self::new_from_navmesh(router, navmesh, width)) Ok(Self::new_from_navmesh(router, navmesh, width))
} }

View File

@ -1,3 +1,4 @@
use derive_getters::Getters;
use geo::EuclideanDistance; use geo::EuclideanDistance;
use petgraph::{data::DataMap, visit::EdgeRef}; use petgraph::{data::DataMap, visit::EdgeRef};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -182,8 +183,9 @@ impl<'a, R: AccessRules> AstarStrategy<Navmesh, f64, BandTermsegIndex>
} }
} }
#[derive(Debug)] #[derive(Debug, Getters)]
pub struct Router<'a, R: AccessRules> { pub struct Router<'a, R: AccessRules> {
#[getter(skip)]
layout: &'a mut Layout<R>, layout: &'a mut Layout<R>,
options: RouterOptions, options: RouterOptions,
} }
@ -209,8 +211,4 @@ impl<'a, R: AccessRules> Router<'a, R> {
pub fn layout(&self) -> &Layout<R> { pub fn layout(&self) -> &Layout<R> {
self.layout self.layout
} }
pub fn options(&self) -> RouterOptions {
self.options
}
} }