diff --git a/packages/mrml-core/src/helper/spacing.rs b/packages/mrml-core/src/helper/spacing.rs index 8fc5cb3b..09f85b72 100644 --- a/packages/mrml-core/src/helper/spacing.rs +++ b/packages/mrml-core/src/helper/spacing.rs @@ -1,29 +1,62 @@ use std::convert::TryFrom; -use crate::helper::size::Size; +use crate::helper::size::Pixel; /// representation of spacing -pub struct Spacing(Size, Option, Option, Option); +pub struct Spacing { + top: Pixel, + right: Option, + bottom: Option, + left: Option, +} impl Spacing { - pub fn top(&self) -> &Size { - &self.0 + pub fn top(&self) -> &Pixel { + &self.top + } + + pub fn into_top(self) -> Pixel { + self.top + } + + pub fn right(&self) -> &Pixel { + self.right.as_ref().unwrap_or_else(|| self.top()) } - pub fn right(&self) -> &Size { - self.1.as_ref().unwrap_or_else(|| self.top()) + pub fn into_right(self) -> Pixel { + if let Some(v) = self.right { + v + } else { + self.into_top() + } } - pub fn bottom(&self) -> &Size { - self.2.as_ref().unwrap_or_else(|| self.top()) + pub fn bottom(&self) -> &Pixel { + self.bottom.as_ref().unwrap_or_else(|| self.top()) } - pub fn left(&self) -> &Size { - self.3 + pub fn into_bottom(self) -> Pixel { + if let Some(v) = self.bottom { + v + } else { + self.into_top() + } + } + + pub fn left(&self) -> &Pixel { + self.left .as_ref() .or_else(|| Some(self.right())) .unwrap_or_else(|| self.top()) } + + pub fn into_left(self) -> Pixel { + if let Some(v) = self.left { + v + } else { + self.into_right() + } + } } impl TryFrom<&str> for Spacing { @@ -32,22 +65,27 @@ impl TryFrom<&str> for Spacing { fn try_from(input: &str) -> Result { let mut sections = input.split(' '); let top = match sections.next() { - Some(value) => Size::try_from(value)?, + Some(value) => Pixel::try_from(value)?, None => return Err(String::from("no value provided")), }; let right = match sections.next() { - Some(value) => Some(Size::try_from(value)?), + Some(value) => Some(Pixel::try_from(value)?), None => None, }; let bottom = match sections.next() { - Some(value) => Some(Size::try_from(value)?), + Some(value) => Some(Pixel::try_from(value)?), None => None, }; let left = match sections.next() { - Some(value) => Some(Size::try_from(value)?), + Some(value) => Some(Pixel::try_from(value)?), None => None, }; - Ok(Spacing(top, right, bottom, left)) + Ok(Spacing { + top, + right, + bottom, + left, + }) } } @@ -59,7 +97,7 @@ pub mod tests { #[test] fn single_value() { let res: Spacing = Spacing::try_from("1px").unwrap(); - assert_eq!(res.top(), &Size::Pixel(Pixel::new(1.0))); + assert_eq!(res.top(), &Pixel::new(1.0)); assert_eq!(res.top(), res.bottom()); assert_eq!(res.top(), res.right()); assert_eq!(res.right(), res.left()); @@ -68,28 +106,28 @@ pub mod tests { #[test] fn two_values() { let res: Spacing = Spacing::try_from("2px 4px").unwrap(); - assert_eq!(res.top(), &Size::Pixel(Pixel::new(2.0))); + assert_eq!(res.top(), &Pixel::new(2.0)); assert_eq!(res.top(), res.bottom()); - assert_eq!(res.left(), &Size::Pixel(Pixel::new(4.0))); + assert_eq!(res.left(), &Pixel::new(4.0)); assert_eq!(res.left(), res.right()); } #[test] fn three_values() { let res: Spacing = Spacing::try_from("2px 3px 4px").unwrap(); - assert_eq!(res.top(), &Size::Pixel(Pixel::new(2.0))); - assert_eq!(res.right(), &Size::Pixel(Pixel::new(3.0))); + assert_eq!(res.top(), &Pixel::new(2.0)); + assert_eq!(res.right(), &Pixel::new(3.0)); assert_eq!(res.left(), res.right()); - assert_eq!(res.bottom(), &Size::Pixel(Pixel::new(4.0))); + assert_eq!(res.bottom(), &Pixel::new(4.0)); } #[test] fn four_values() { let res: Spacing = Spacing::try_from("2px 3px 4px 5px").unwrap(); - assert_eq!(res.top(), &Size::Pixel(Pixel::new(2.0))); - assert_eq!(res.right(), &Size::Pixel(Pixel::new(3.0))); - assert_eq!(res.bottom(), &Size::Pixel(Pixel::new(4.0))); - assert_eq!(res.left(), &Size::Pixel(Pixel::new(5.0))); + assert_eq!(res.top(), &Pixel::new(2.0)); + assert_eq!(res.right(), &Pixel::new(3.0)); + assert_eq!(res.bottom(), &Pixel::new(4.0)); + assert_eq!(res.left(), &Pixel::new(5.0)); } #[test] diff --git a/packages/mrml-core/src/prelude/render/mod.rs b/packages/mrml-core/src/prelude/render/mod.rs index 9d451bd6..2df9d5db 100644 --- a/packages/mrml-core/src/prelude/render/mod.rs +++ b/packages/mrml-core/src/prelude/render/mod.rs @@ -141,43 +141,37 @@ pub trait Render<'root> { fn get_inner_border_left(&self) -> Option { self.attribute_as_pixel("inner-border-left").or_else(|| { self.attribute_as_spacing("inner-border") - .and_then(|s| s.left().as_pixel().cloned()) + .map(|s| s.into_left()) }) } fn get_inner_border_right(&self) -> Option { self.attribute_as_pixel("inner-border-right").or_else(|| { self.attribute_as_spacing("inner-border") - .and_then(|s| s.right().as_pixel().cloned()) + .map(|s| s.into_right()) }) } fn get_padding_top(&self) -> Option { - self.attribute_as_pixel("padding-top").or_else(|| { - self.attribute_as_spacing("padding") - .and_then(|s| s.top().as_pixel().cloned()) - }) + self.attribute_as_pixel("padding-top") + .or_else(|| self.attribute_as_spacing("padding").map(|s| s.into_top())) } fn get_padding_bottom(&self) -> Option { self.attribute_as_pixel("padding-bottom").or_else(|| { self.attribute_as_spacing("padding") - .and_then(|s| s.bottom().as_pixel().cloned()) + .map(|s| s.into_bottom()) }) } fn get_padding_left(&self) -> Option { - self.attribute_as_pixel("padding-left").or_else(|| { - self.attribute_as_spacing("padding") - .and_then(|s| s.left().as_pixel().cloned()) - }) + self.attribute_as_pixel("padding-left") + .or_else(|| self.attribute_as_spacing("padding").map(|s| s.into_left())) } fn get_padding_right(&self) -> Option { - self.attribute_as_pixel("padding-right").or_else(|| { - self.attribute_as_spacing("padding") - .and_then(|s| s.right().as_pixel().cloned()) - }) + self.attribute_as_pixel("padding-right") + .or_else(|| self.attribute_as_spacing("padding").map(|s| s.into_right())) } fn get_padding_horizontal(&self) -> Pixel {