Skip to content

Commit

Permalink
refactor(mrml-core): update spacing implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jdrouet committed May 1, 2024
1 parent d6efc8f commit aee1c43
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 83 deletions.
68 changes: 34 additions & 34 deletions packages/mrml-core/src/helper/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,96 +80,96 @@ impl ToString for Size {
}

#[derive(Clone, Debug, PartialEq)]
pub struct Pixel(f32);
pub struct Percent(f32);

impl Pixel {
impl Percent {
pub fn new(value: f32) -> Self {
Self(value)
}

pub fn value(&self) -> f32 {
self.0
}

pub fn from_border(input: &str) -> Option<Self> {
input
.split_whitespace()
.next()
.and_then(|value| Self::try_from(value).ok())
}

pub fn lower(&self) -> Self {
if self.0 <= 1.0 {
Self(0.0)
} else {
Self(self.0 - 1.0)
}
}
}

impl TryFrom<&str> for Pixel {
impl TryFrom<&str> for Percent {
type Error = String;

fn try_from(value: &str) -> Result<Self, Self::Error> {
if let Some(value) = value.strip_suffix("px") {
if let Some(value) = value.strip_suffix('%') {
value
.parse::<f32>()
.map(Pixel::new)
.map(Percent::new)
.map_err(|err| err.to_string())
} else {
Err(String::from("pixel value should end with px"))
Err(String::from("percent value should end with %"))
}
}
}

impl Default for Pixel {
impl Default for Percent {
fn default() -> Self {
Self(0.0)
}
}

impl ToString for Pixel {
impl ToString for Percent {
fn to_string(&self) -> String {
format!("{}px", self.0)
format!("{}%", self.0)
}
}

#[derive(Clone, Debug, PartialEq)]
pub struct Percent(f32);
pub struct Pixel(f32);

impl Percent {
impl Pixel {
pub fn new(value: f32) -> Self {
Self(value)
}

pub fn value(&self) -> f32 {
self.0
}

pub fn from_border(input: &str) -> Option<Self> {
input
.split_whitespace()
.next()
.and_then(|value| Self::try_from(value).ok())
}

pub fn lower(&self) -> Self {
if self.0 <= 1.0 {
Self(0.0)
} else {
Self(self.0 - 1.0)
}
}
}

impl TryFrom<&str> for Percent {
impl TryFrom<&str> for Pixel {
type Error = String;

fn try_from(value: &str) -> Result<Self, Self::Error> {
if let Some(value) = value.strip_suffix('%') {
if let Some(value) = value.strip_suffix("px") {
value
.parse::<f32>()
.map(Percent::new)
.map(Pixel::new)
.map_err(|err| err.to_string())
} else {
Err(String::from("percent value should end with %"))
Err(String::from("pixel value should end with px"))
}
}
}

impl Default for Percent {
impl Default for Pixel {
fn default() -> Self {
Self(0.0)
}
}

impl ToString for Percent {
fn to_string(&self) -> String {
format!("{}%", self.0)
impl std::fmt::Display for Pixel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}px", self.0)
}
}
133 changes: 86 additions & 47 deletions packages/mrml-core/src/helper/spacing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,96 @@ use std::convert::TryFrom;
use crate::helper::size::Pixel;

/// representation of spacing
pub struct Spacing {
top: Pixel,
right: Option<Pixel>,
bottom: Option<Pixel>,
left: Option<Pixel>,
pub enum Spacing {
Single(Pixel),
Two(Pixel, Pixel),
Three(Pixel, Pixel, Pixel),
Four(Pixel, Pixel, Pixel, Pixel),
}

impl Spacing {
pub fn top(&self) -> &Pixel {
&self.top
match self {
Self::Single(top) => top,
Self::Two(vertical, _horizontal) => vertical,
Self::Three(top, _horizontal, _bottom) => top,
Self::Four(top, _right, _bottom, _left) => top,
}
}

pub fn into_top(self) -> Pixel {
self.top
match self {
Self::Single(top) => top,
Self::Two(vertical, _horizontal) => vertical,
Self::Three(top, _horizontal, _bottom) => top,
Self::Four(top, _right, _bottom, _left) => top,
}
}

pub fn right(&self) -> &Pixel {
self.right.as_ref().unwrap_or_else(|| self.top())
match self {
Self::Single(top) => top,
Self::Two(_vertical, horizontal) => horizontal,
Self::Three(_top, horizontal, _bottom) => horizontal,
Self::Four(_top, right, _bottom, _left) => right,
}
}

pub fn into_right(self) -> Pixel {
if let Some(v) = self.right {
v
} else {
self.into_top()
match self {
Self::Single(top) => top,
Self::Two(_vertical, horizontal) => horizontal,
Self::Three(_top, horizontal, _bottom) => horizontal,
Self::Four(_top, right, _bottom, _left) => right,
}
}

pub fn bottom(&self) -> &Pixel {
self.bottom.as_ref().unwrap_or_else(|| self.top())
match self {
Self::Single(top) => top,
Self::Two(vertical, _horizontal) => vertical,
Self::Three(_top, _horizontal, bottom) => bottom,
Self::Four(_top, _right, bottom, _left) => bottom,
}
}

pub fn into_bottom(self) -> Pixel {
if let Some(v) = self.bottom {
v
} else {
self.into_top()
match self {
Self::Single(top) => top,
Self::Two(vertical, _horizontal) => vertical,
Self::Three(_top, _horizontal, bottom) => bottom,
Self::Four(_top, _right, bottom, _left) => bottom,
}
}

pub fn left(&self) -> &Pixel {
self.left
.as_ref()
.or_else(|| Some(self.right()))
.unwrap_or_else(|| self.top())
match self {
Self::Single(top) => top,
Self::Two(_vertical, horizontal) => horizontal,
Self::Three(_top, horizontal, _bottom) => horizontal,
Self::Four(_top, _right, _bottom, left) => left,
}
}

pub fn into_left(self) -> Pixel {
if let Some(v) = self.left {
v
} else {
self.into_right()
match self {
Self::Single(top) => top,
Self::Two(_vertical, horizontal) => horizontal,
Self::Three(_top, horizontal, _bottom) => horizontal,
Self::Four(_top, _right, _bottom, left) => left,
}
}
}

impl std::fmt::Display for Spacing {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Single(first) => write!(f, "{first}"),
Self::Two(first, second) => write!(f, "{first} {second}"),
Self::Three(first, second, third) => write!(f, "{first} {second} {third}"),
Self::Four(first, second, third, fourth) => {
write!(f, "{first} {second} {third} {fourth}")
}
}
}
}
Expand All @@ -64,28 +102,29 @@ 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) => Pixel::try_from(value)?,
None => return Err(String::from("no value provided")),
};
let right = match sections.next() {
Some(value) => Some(Pixel::try_from(value)?),
None => None,
};
let bottom = match sections.next() {
Some(value) => Some(Pixel::try_from(value)?),
None => None,
};
let left = match sections.next() {
Some(value) => Some(Pixel::try_from(value)?),
None => None,
};
Ok(Spacing {
top,
right,
bottom,
left,
})
match (
sections.next(),
sections.next(),
sections.next(),
sections.next(),
) {
(Some(first), None, None, None) => Ok(Self::Single(Pixel::try_from(first)?)),
(Some(first), Some(second), None, None) => {
Ok(Self::Two(Pixel::try_from(first)?, Pixel::try_from(second)?))
}
(Some(first), Some(second), Some(third), None) => Ok(Self::Three(
Pixel::try_from(first)?,
Pixel::try_from(second)?,
Pixel::try_from(third)?,
)),
(Some(first), Some(second), Some(third), Some(four)) => Ok(Self::Four(
Pixel::try_from(first)?,
Pixel::try_from(second)?,
Pixel::try_from(third)?,
Pixel::try_from(four)?,
)),
_ => Err(String::from("no value provided")),
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/mrml-core/src/mj_image/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl<'root> Renderer<'root, MjImage, ()> {
td.mj-full-width-mobile {{ width: auto !important; }}
}}
"#,
self.context.header.breakpoint().lower().to_string(),
self.context.header.breakpoint().lower(),
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/mrml-core/src/mj_navbar/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl<'root> Renderer<'root, MjNavbar, MjNavbarExtra> {
.mj-menu-checkbox[type="checkbox"]:checked ~ .mj-menu-trigger .mj-menu-icon-open {{ display:none!important; }}
}}
"#,
self.context.header.breakpoint().lower().to_string()
self.context.header.breakpoint().lower()
)
}
}
Expand Down

0 comments on commit aee1c43

Please sign in to comment.