forked from 1b-agent/opencadstudio-upstream
refactor(entities): collapse trait wrapper boilerplate via macro
Every entity file ended with the same three-trait wrapper block:
impl Grippable for T {
fn grips(&self) -> Vec<GripDef> { grips(self) }
fn apply_grip(&mut self, ...) { apply_grip(self, ...) }
}
impl PropertyEditable for T { ... }
impl Transformable for T { ... }
— 23 lines of pure delegation around five same-named free functions.
Replace with `crate::impl_entity_basics!(T);` (a macro in
entities/traits.rs that expands to the three trait impls).
Applied where the file already follows the free-fn delegation
convention (Arc, Circle, Ellipse, Insert, Leader, Line, LwPolyline,
MultiLeader, Ole2Frame, Point, Spline, Viewport). Files that inlined
the trait method bodies directly (Ray, XLine, Shape, Solid, Tolerance,
MLine, Mesh, Solid3D/Region/Body, Table, RasterImage, Wipeout,
Underlay, Hatch, Polyline, Polyline2D, Polyline3D, Dimension) left
alone — they already skip the indirection. Text/MText/Attribute use
the with-text-style flavour and will move once that variant is needed
beyond the existing two manual impls.
A second macro `impl_entity_basics_with_text_styles!` is included for
the text-like flavour but no entity uses it yet.
Net: -204 lines.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
1 parent
5e36e85e07
commit
9b5073752d
13 files changed
+101
-305
No files matched your search
@@ -81,6 +81,83 @@ macro_rules! dispatch {
|
||||
};
|
||||
}
|
||||
|
||||
/// Generates `Grippable`, `PropertyEditable`, `Transformable` trait impls
|
||||
/// that delegate to identically-named free functions in the entity's own
|
||||
/// module (`grips`, `apply_grip`, `properties`, `apply_geom_prop`,
|
||||
/// `apply_transform`). `properties()` is called with `self` only — for
|
||||
/// text-like entities that need the document's text-style list use
|
||||
/// [`impl_entity_basics_with_text_styles!`] instead.
|
||||
#[macro_export]
|
||||
macro_rules! impl_entity_basics {
|
||||
($T:ty) => {
|
||||
impl $crate::entities::traits::Grippable for $T {
|
||||
fn grips(&self) -> Vec<$crate::scene::object::GripDef> {
|
||||
grips(self)
|
||||
}
|
||||
fn apply_grip(
|
||||
&mut self,
|
||||
grip_id: usize,
|
||||
apply: $crate::scene::object::GripApply,
|
||||
) {
|
||||
apply_grip(self, grip_id, apply);
|
||||
}
|
||||
}
|
||||
impl $crate::entities::traits::PropertyEditable for $T {
|
||||
fn geometry_properties(
|
||||
&self,
|
||||
_text_style_names: &[String],
|
||||
) -> $crate::scene::object::PropSection {
|
||||
properties(self)
|
||||
}
|
||||
fn apply_geom_prop(&mut self, field: &str, value: &str) {
|
||||
apply_geom_prop(self, field, value);
|
||||
}
|
||||
}
|
||||
impl $crate::entities::traits::Transformable for $T {
|
||||
fn apply_transform(&mut self, t: &$crate::command::EntityTransform) {
|
||||
apply_transform(self, t);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Same as [`impl_entity_basics!`] but the entity's `properties(...)` free
|
||||
/// function takes the document's text-style name list as a second
|
||||
/// argument (Text, MText, …).
|
||||
#[macro_export]
|
||||
macro_rules! impl_entity_basics_with_text_styles {
|
||||
($T:ty) => {
|
||||
impl $crate::entities::traits::Grippable for $T {
|
||||
fn grips(&self) -> Vec<$crate::scene::object::GripDef> {
|
||||
grips(self)
|
||||
}
|
||||
fn apply_grip(
|
||||
&mut self,
|
||||
grip_id: usize,
|
||||
apply: $crate::scene::object::GripApply,
|
||||
) {
|
||||
apply_grip(self, grip_id, apply);
|
||||
}
|
||||
}
|
||||
impl $crate::entities::traits::PropertyEditable for $T {
|
||||
fn geometry_properties(
|
||||
&self,
|
||||
text_style_names: &[String],
|
||||
) -> $crate::scene::object::PropSection {
|
||||
properties(self, text_style_names)
|
||||
}
|
||||
fn apply_geom_prop(&mut self, field: &str, value: &str) {
|
||||
apply_geom_prop(self, field, value);
|
||||
}
|
||||
}
|
||||
impl $crate::entities::traits::Transformable for $T {
|
||||
fn apply_transform(&mut self, t: &$crate::command::EntityTransform) {
|
||||
apply_transform(self, t);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl EntityTypeOps for EntityType {
|
||||
fn to_truck_entity(&self, document: &CadDocument) -> Option<TruckEntity> {
|
||||
dispatch!(self,
|
||||
|
||||
Reference in new issue
Block a user