Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(mrml-core): spacing should only handle pixels #422

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 63 additions & 25 deletions packages/mrml-core/src/helper/spacing.rs
Original file line number Diff line number Diff line change
@@ -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<Size>, Option<Size>, Option<Size>);
pub struct Spacing {
top: Pixel,
right: Option<Pixel>,
bottom: Option<Pixel>,
left: Option<Pixel>,
}

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 {
Expand All @@ -32,22 +65,27 @@ impl TryFrom<&str> for Spacing {
fn try_from(input: &str) -> Result<Self, Self::Error> {
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,
})
}
}

Expand All @@ -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());
Expand All @@ -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]
Expand Down
24 changes: 9 additions & 15 deletions packages/mrml-core/src/prelude/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,43 +141,37 @@ pub trait Render<'root> {
fn get_inner_border_left(&self) -> Option<Pixel> {
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<Pixel> {
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<Pixel> {
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<Pixel> {
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<Pixel> {
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<Pixel> {
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 {
Expand Down
Loading