From dc676d606e855c5b8f745d309a8bf8cc3edc0fe4 Mon Sep 17 00:00:00 2001 From: drendog Date: Sun, 30 Nov 2025 06:26:14 +0100 Subject: [PATCH] refactor: improve margin and scale factor on builder --- crates/composition/src/builder.rs | 20 ++++----- crates/domain/src/dimensions.rs | 8 ++++ crates/domain/src/value_objects/margins.rs | 47 ++++++++++++++++++++++ 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/crates/composition/src/builder.rs b/crates/composition/src/builder.rs index 30a0ea2..65e87f9 100644 --- a/crates/composition/src/builder.rs +++ b/crates/composition/src/builder.rs @@ -149,13 +149,8 @@ impl LayerShika { } #[must_use] - pub const fn with_margin(mut self, top: i32, right: i32, bottom: i32, left: i32) -> Self { - self.config.margin = Margins { - top, - right, - bottom, - left, - }; + pub fn with_margin(mut self, margin: impl Into) -> Self { + self.config.margin = margin.into(); self } @@ -172,14 +167,15 @@ impl LayerShika { } #[must_use] - pub fn with_namespace(mut self, namespace: String) -> Self { - self.config.namespace = namespace; + pub fn with_namespace(mut self, namespace: impl Into) -> Self { + self.config.namespace = namespace.into(); self } - pub fn with_scale_factor(mut self, scale_factor: f32) -> Result { - self.config.scale_factor = ScaleFactor::new(scale_factor)?; - Ok(self) + #[must_use] + pub fn with_scale_factor(mut self, sf: impl TryInto) -> Self { + self.config.scale_factor = sf.try_into().unwrap_or_default(); + self } #[must_use] diff --git a/crates/domain/src/dimensions.rs b/crates/domain/src/dimensions.rs index 56c8d32..4e2954f 100644 --- a/crates/domain/src/dimensions.rs +++ b/crates/domain/src/dimensions.rs @@ -174,6 +174,14 @@ impl Default for ScaleFactor { } } +impl TryFrom for ScaleFactor { + type Error = DomainError; + + fn try_from(factor: f32) -> Result { + Self::new(factor) + } +} + #[derive(Debug, Clone, Copy, PartialEq)] pub struct LogicalPosition { x: f32, diff --git a/crates/domain/src/value_objects/margins.rs b/crates/domain/src/value_objects/margins.rs index 4097586..01fbdc6 100644 --- a/crates/domain/src/value_objects/margins.rs +++ b/crates/domain/src/value_objects/margins.rs @@ -5,3 +5,50 @@ pub struct Margins { pub bottom: i32, pub left: i32, } + +impl Margins { + pub const fn all(px: i32) -> Self { + Self { + top: px, + right: px, + bottom: px, + left: px, + } + } + + pub const fn symmetric(vertical: i32, horizontal: i32) -> Self { + Self { + top: vertical, + right: horizontal, + bottom: vertical, + left: horizontal, + } + } + + pub const fn new(top: i32, right: i32, bottom: i32, left: i32) -> Self { + Self { + top, + right, + bottom, + left, + } + } +} + +impl From for Margins { + fn from(px: i32) -> Self { + Self::all(px) + } +} + +impl From<(i32, i32)> for Margins { + fn from((vertical, horizontal): (i32, i32)) -> Self { + Self::symmetric(vertical, horizontal) + } +} + +impl From<(i32, i32, i32, i32)> for Margins { + fn from((top, right, bottom, left): (i32, i32, i32, i32)) -> Self { + Self::new(top, right, bottom, left) + } +}