refactor(commands): per-entity MassProps + TextContent traits

Two free helpers in app/commands.rs lived as big match-on-EntityType
blocks: massprop_entity (Circle/Arc/Line/LwPolyline/Ellipse area +
perimeter + centroid, ~140 lines) and entity_text_content /
replace_entity_text (Text/MText/AttributeDefinition/AttributeEntity).

Move each to its own trait in entities/traits.rs and implement
per-entity:

  MassPropsCalc  — Circle, Arc, Line, LwPolyline, Ellipse
  TextContent    — Text, MText, AttributeDefinition, AttributeEntity

EntityTypeOps gets dispatch shims that route to the trait via the
existing macro, so callsites just call entity.mass_props() /
entity.text_content() / entity.replace_text(search, rep).

The remaining free helpers in commands.rs (entity_list_details,
entity_extra_info, entity_type_name, build_data_extraction_csv) are
single-command formatting helpers tightly coupled to LIST / EXTRACT /
text-format output. Distributing them as per-entity methods would
inflate every entity file with command-side formatting concerns
without making the command-side any cleaner — left in place.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Hakan SevenandClaude Opus 4.7 committed 2026-05-23 23:30:34 +03:00
1 parent 40180e9089
commit e78321190a
10 files changed
+266 -193

No files matched your search

+51
View File
@@ -31,6 +31,30 @@ pub trait Transformable {
fn apply_transform(&mut self, t: &EntityTransform);
}
/// Inquiry-time mass / area / perimeter properties for entities whose
/// 2D footprint has a meaningful area or perimeter (Circle, Arc, Line,
/// LwPolyline, Ellipse). Entities outside this set get `None` via the
/// dispatcher.
#[derive(Clone, Copy, Debug)]
pub struct MassProps {
pub area: f64,
pub perimeter: f64,
pub cx: f64,
pub cy: f64,
}
pub trait MassPropsCalc {
fn mass_props(&self) -> MassProps;
}
/// Read / replace the visible string of text-like entities (Text, MText,
/// AttributeDefinition, AttributeEntity). `replace` rewrites every
/// occurrence of `search` with `rep`.
pub trait TextContent {
fn text_content(&self) -> Option<String>;
fn replace_text(&mut self, search: &str, rep: &str);
}
pub trait EntityTypeOps {
fn to_truck_entity(&self, document: &CadDocument) -> Option<TruckEntity>;
fn grips(&self) -> Vec<GripDef>;
@@ -38,6 +62,9 @@ pub trait EntityTypeOps {
fn apply_geom_prop(&mut self, field: &str, value: &str);
fn apply_grip(&mut self, grip_id: usize, apply: GripApply);
fn apply_transform(&mut self, t: &EntityTransform);
fn mass_props(&self) -> Option<MassProps>;
fn text_content(&self) -> Option<String>;
fn replace_text(&mut self, search: &str, rep: &str);
}
/// Per-dispatch-function entity-variant lists. Adding a new entity that
@@ -148,4 +175,28 @@ impl EntityTypeOps for EntityType {
_ => {},
)
}
fn mass_props(&self) -> Option<MassProps> {
dispatch!(self,
|e| Some(MassPropsCalc::mass_props(e)),
[Circle, Arc, Line, LwPolyline, Ellipse],
_ => None,
)
}
fn text_content(&self) -> Option<String> {
dispatch!(self,
|e| TextContent::text_content(e),
[Text, MText, AttributeDefinition, AttributeEntity],
_ => None,
)
}
fn replace_text(&mut self, search: &str, rep: &str) {
dispatch!(self,
|e| TextContent::replace_text(e, search, rep),
[Text, MText, AttributeDefinition, AttributeEntity],
_ => {},
)
}
}