From 27edbfb01089a1b3407dcc4094a66ba10567919a Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Tue, 7 Oct 2025 15:36:12 -0700 Subject: [PATCH] Streamline axis naming This makes it simpler, from the programmer's perspective, to get the name of an axis as a string slice and to format an axis name into a string. To me, the matching method `Axis::name` seems more direct than the explicit lookup table that it replaces, and I'm hoping that it will be about as easy for the compiler to inline, or even easier. Implementing `Display` enables us to hand an `Axis` to a string formatter without any explicit conversion. It adds extra code in the short run, but I'd expect it to simplify our code in the long run by fitting into the conventions set by the Rust standard library. --- app-proto/src/assembly.rs | 14 +++++++++++--- app-proto/src/components/outline.rs | 3 +-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app-proto/src/assembly.rs b/app-proto/src/assembly.rs index 8e4b96f..4da9422 100644 --- a/app-proto/src/assembly.rs +++ b/app-proto/src/assembly.rs @@ -127,7 +127,7 @@ pub trait Element: Serial + ProblemPoser + DisplayItem { } impl Debug for dyn Element { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { self.id().fmt(f) } } @@ -511,10 +511,18 @@ impl ProblemPoser for HalfCurvatureRegulator { } #[derive(Clone, Copy, Sequence)] -pub enum Axis {X = 0, Y = 1, Z = 2} +pub enum Axis { X = 0, Y = 1, Z = 2 } impl Axis { - pub const NAME: [&str; Axis::CARDINALITY] = ["X", "Y", "Z"]; + fn name(&self) -> &'static str { + match self { Axis::X => "X", Axis::Y => "Y", Axis::Z => "Z" } + } +} + +impl fmt::Display for Axis { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.name()) + } } pub struct PointCoordinateRegulator { diff --git a/app-proto/src/components/outline.rs b/app-proto/src/components/outline.rs index d9ab71d..547b73b 100644 --- a/app-proto/src/components/outline.rs +++ b/app-proto/src/components/outline.rs @@ -6,7 +6,6 @@ use web_sys::{KeyboardEvent, MouseEvent, wasm_bindgen::JsCast}; use crate::{ AppState, assembly::{ - Axis, Element, HalfCurvatureRegulator, InversiveDistanceRegulator, @@ -123,7 +122,7 @@ impl OutlineItem for HalfCurvatureRegulator { impl OutlineItem for PointCoordinateRegulator { fn outline_item(self: Rc, _element: &Rc) -> View { - let name = format!("{} coordinate", Axis::NAME[self.axis as usize]); + let name = format!("{} coordinate", self.axis); view! { li(class = "regulator") { div(class = "regulator-label") // for spacing