🧑‍💻 Swap ToString with Display for cleaner code

This commit is contained in:
Manuel Cillero 2023-11-10 00:00:27 +01:00
parent 012fac2608
commit 13c7eb4ade
6 changed files with 213 additions and 196 deletions

View file

@ -2,6 +2,8 @@ use crate::core::component::{Context, ContextOp};
use crate::html::{JavaScript, StyleSheet}; use crate::html::{JavaScript, StyleSheet};
use crate::Weight; use crate::Weight;
use std::fmt;
// Context parameters. // Context parameters.
pub const PARAM_BASE_WEIGHT: &str = "base.weight"; pub const PARAM_BASE_WEIGHT: &str = "base.weight";
pub const PARAM_BASE_INCLUDE_ICONS: &str = "base.include.icon"; pub const PARAM_BASE_INCLUDE_ICONS: &str = "base.include.icon";
@ -66,18 +68,19 @@ pub enum BreakPoint {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for BreakPoint { impl fmt::Display for BreakPoint {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { let breakpoint = match self {
BreakPoint::None => "pt-bp__none".to_string(), BreakPoint::None => "pt-bp__none",
BreakPoint::SM => "pt-bp__sm".to_string(), BreakPoint::SM => "pt-bp__sm",
BreakPoint::MD => "pt-bp__md".to_string(), BreakPoint::MD => "pt-bp__md",
BreakPoint::LG => "pt-bp__lg".to_string(), BreakPoint::LG => "pt-bp__lg",
BreakPoint::XL => "pt-bp__xl".to_string(), BreakPoint::XL => "pt-bp__xl",
BreakPoint::X2L => "pt-bp__x2l".to_string(), BreakPoint::X2L => "pt-bp__x2l",
BreakPoint::X3L => "pt-bp__x3l".to_string(), BreakPoint::X3L => "pt-bp__x3l",
BreakPoint::X2K => "pt-bp__x2k".to_string(), BreakPoint::X2K => "pt-bp__x2k",
} };
write!(f, "{breakpoint}")
} }
} }
@ -99,20 +102,21 @@ pub enum FontSize {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for FontSize { impl fmt::Display for FontSize {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { let font_size = match self {
FontSize::ExtraLarge => "pt-fs__x3l".to_string(), FontSize::ExtraLarge => "pt-fs__x3l",
FontSize::XxLarge => "pt-fs__x2l".to_string(), FontSize::XxLarge => "pt-fs__x2l",
FontSize::XLarge => "pt-fs__xl".to_string(), FontSize::XLarge => "pt-fs__xl",
FontSize::Large => "pt-fs__l".to_string(), FontSize::Large => "pt-fs__l",
FontSize::Medium => "pt-fs__m".to_string(), FontSize::Medium => "pt-fs__m",
FontSize::Normal => "".to_string(), FontSize::Normal => "",
FontSize::Small => "pt-fs__s".to_string(), FontSize::Small => "pt-fs__s",
FontSize::XSmall => "pt-fs__xs".to_string(), FontSize::XSmall => "pt-fs__xs",
FontSize::XxSmall => "pt-fs__x2s".to_string(), FontSize::XxSmall => "pt-fs__x2s",
FontSize::ExtraSmall => "pt-fs__x3s".to_string(), FontSize::ExtraSmall => "pt-fs__x3s",
} };
write!(f, "{font_size}")
} }
} }

View file

@ -5,6 +5,8 @@ pub use item::{Item, COMPONENT_BASE_FLEX_ITEM};
use crate::prelude::*; use crate::prelude::*;
use std::fmt;
// ************************************************************************************************* // *************************************************************************************************
#[derive(Default)] #[derive(Default)]
@ -18,39 +20,24 @@ pub enum Direction {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for Direction { impl fmt::Display for Direction {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Direction::Default => { Direction::Default => write!(
concat_string!( f, "pt-flex__container pt-flex__row {}", BreakPoint::default()
"pt-flex__container pt-flex__row ", ),
BreakPoint::default().to_string() Direction::Row(breakpoint) => write!(
) f, "pt-flex__container pt-flex__row {breakpoint}"
} ),
Direction::Row(breakpoint) => { Direction::RowReverse(breakpoint) => write!(
concat_string!( f, "pt-flex__container pt-flex__row pt-flex__reverse {breakpoint}"
"pt-flex__container pt-flex__row ", ),
breakpoint.to_string() Direction::Column(breakpoint) => write!(
) f, "pt-flex__container pt-flex__col {breakpoint}"
} ),
Direction::RowReverse(breakpoint) => { Direction::ColumnReverse(breakpoint) => write!(
concat_string!( f, "pt-flex__container pt-flex__col pt-flex__reverse {breakpoint}"
"pt-flex__container pt-flex__row pt-flex__reverse ", ),
breakpoint.to_string()
)
}
Direction::Column(breakpoint) => {
concat_string!(
"pt-flex__container pt-flex__col ",
breakpoint.to_string()
)
}
Direction::ColumnReverse(breakpoint) => {
concat_string!(
"pt-flex__container pt-flex__col pt-flex__reverse ",
breakpoint.to_string()
)
}
} }
} }
} }
@ -67,13 +54,13 @@ pub enum WrapAlign {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for WrapAlign { impl fmt::Display for WrapAlign {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
WrapAlign::Default => "".to_string(), WrapAlign::Default => write!(f, ""),
WrapAlign::NoWrap => "flex-nowrap".to_string(), WrapAlign::NoWrap => write!(f, "flex-nowrap"),
WrapAlign::Wrap(a) => concat_string!("pt-flex__wrap ", a.to_string()), WrapAlign::Wrap(a) => write!(f, "pt-flex__wrap {a}"),
WrapAlign::WrapReverse(a) => concat_string!("pt-flex__wrap-reverse ", a.to_string()), WrapAlign::WrapReverse(a) => write!(f, "pt-flex__wrap-reverse {a}"),
} }
} }
} }
@ -93,17 +80,18 @@ pub enum ContentAlign {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for ContentAlign { impl fmt::Display for ContentAlign {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { let content_align = match self {
ContentAlign::Default => "".to_string(), ContentAlign::Default => "",
ContentAlign::Start => "pt-flex__align-start".to_string(), ContentAlign::Start => "pt-flex__align-start",
ContentAlign::End => "pt-flex__align-end".to_string(), ContentAlign::End => "pt-flex__align-end",
ContentAlign::Center => "pt-flex__align-center".to_string(), ContentAlign::Center => "pt-flex__align-center",
ContentAlign::Stretch => "pt-flex__align-stretch".to_string(), ContentAlign::Stretch => "pt-flex__align-stretch",
ContentAlign::SpaceBetween => "pt-flex__align-space-between".to_string(), ContentAlign::SpaceBetween => "pt-flex__align-space-between",
ContentAlign::SpaceAround => "pt-flex__align-space-around".to_string(), ContentAlign::SpaceAround => "pt-flex__align-space-around",
} };
write!(f, "{content_align}")
} }
} }
@ -122,17 +110,18 @@ pub enum ContentJustify {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for ContentJustify { impl fmt::Display for ContentJustify {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { let content_justify = match self {
ContentJustify::Default => "".to_string(), ContentJustify::Default => "",
ContentJustify::Start => "pt-flex__justify-start".to_string(), ContentJustify::Start => "pt-flex__justify-start",
ContentJustify::End => "pt-flex__justify-end".to_string(), ContentJustify::End => "pt-flex__justify-end",
ContentJustify::Center => "pt-flex__justify-center".to_string(), ContentJustify::Center => "pt-flex__justify-center",
ContentJustify::SpaceBetween => "pt-flex__justify-space-between".to_string(), ContentJustify::SpaceBetween => "pt-flex__justify-space-between",
ContentJustify::SpaceAround => "pt-flex__justify-space-around".to_string(), ContentJustify::SpaceAround => "pt-flex__justify-space-around",
ContentJustify::SpaceEvenly => "pt-flex__justify-space-evenly".to_string(), ContentJustify::SpaceEvenly => "pt-flex__justify-space-evenly",
} };
write!(f, "{content_justify}")
} }
} }
@ -150,16 +139,17 @@ pub enum ItemAlign {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for ItemAlign { impl fmt::Display for ItemAlign {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { let item_align = match self {
ItemAlign::Default => "".to_string(), ItemAlign::Default => "",
ItemAlign::Top => "pt-flex__item-top".to_string(), ItemAlign::Top => "pt-flex__item-top",
ItemAlign::Bottom => "pt-flex__item-bottom".to_string(), ItemAlign::Bottom => "pt-flex__item-bottom",
ItemAlign::Middle => "pt-flex__item-middle".to_string(), ItemAlign::Middle => "pt-flex__item-middle",
ItemAlign::Stretch => "pt-flex__item-stretch".to_string(), ItemAlign::Stretch => "pt-flex__item-stretch",
ItemAlign::Baseline => "pt-flex__item-baseline".to_string(), ItemAlign::Baseline => "pt-flex__item-baseline",
} };
write!(f, "{item_align}")
} }
} }
@ -176,14 +166,14 @@ pub enum Gap {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for Gap { impl fmt::Display for Gap {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Gap::Default => "".to_string(), Gap::Default => write!(f, ""),
Gap::Row(r) => concat_string!("row-gap: ", r.to_string(), ";"), Gap::Row(r) => write!(f, "row-gap: {r};"),
Gap::Column(c) => concat_string!("column-gap: ", c.to_string(), ";"), Gap::Column(c) => write!(f, "column-gap: {c};"),
Gap::Distinct(r, c) => concat_string!("gap: ", r.to_string(), " ", c.to_string(), ";"), Gap::Distinct(r, c) => write!(f, "gap: {r} {c};"),
Gap::Both(v) => concat_string!("gap: ", v.to_string(), ";"), Gap::Both(v) => write!(f, "gap: {v};"),
} }
} }
} }
@ -206,20 +196,21 @@ pub enum ItemGrow {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for ItemGrow { impl fmt::Display for ItemGrow {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { let item_grow = match self {
ItemGrow::Default => "".to_string(), ItemGrow::Default => "",
ItemGrow::Is1 => "pt-flex__grow-1".to_string(), ItemGrow::Is1 => "pt-flex__grow-1",
ItemGrow::Is2 => "pt-flex__grow-2".to_string(), ItemGrow::Is2 => "pt-flex__grow-2",
ItemGrow::Is3 => "pt-flex__grow-3".to_string(), ItemGrow::Is3 => "pt-flex__grow-3",
ItemGrow::Is4 => "pt-flex__grow-4".to_string(), ItemGrow::Is4 => "pt-flex__grow-4",
ItemGrow::Is5 => "pt-flex__grow-5".to_string(), ItemGrow::Is5 => "pt-flex__grow-5",
ItemGrow::Is6 => "pt-flex__grow-6".to_string(), ItemGrow::Is6 => "pt-flex__grow-6",
ItemGrow::Is7 => "pt-flex__grow-7".to_string(), ItemGrow::Is7 => "pt-flex__grow-7",
ItemGrow::Is8 => "pt-flex__grow-8".to_string(), ItemGrow::Is8 => "pt-flex__grow-8",
ItemGrow::Is9 => "pt-flex__grow-9".to_string(), ItemGrow::Is9 => "pt-flex__grow-9",
} };
write!(f, "{item_grow}")
} }
} }
@ -241,20 +232,21 @@ pub enum ItemShrink {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for ItemShrink { impl fmt::Display for ItemShrink {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { let item_shrink = match self {
ItemShrink::Default => "".to_string(), ItemShrink::Default => "",
ItemShrink::Is1 => "pt-flex__shrink-1".to_string(), ItemShrink::Is1 => "pt-flex__shrink-1",
ItemShrink::Is2 => "pt-flex__shrink-2".to_string(), ItemShrink::Is2 => "pt-flex__shrink-2",
ItemShrink::Is3 => "pt-flex__shrink-3".to_string(), ItemShrink::Is3 => "pt-flex__shrink-3",
ItemShrink::Is4 => "pt-flex__shrink-4".to_string(), ItemShrink::Is4 => "pt-flex__shrink-4",
ItemShrink::Is5 => "pt-flex__shrink-5".to_string(), ItemShrink::Is5 => "pt-flex__shrink-5",
ItemShrink::Is6 => "pt-flex__shrink-6".to_string(), ItemShrink::Is6 => "pt-flex__shrink-6",
ItemShrink::Is7 => "pt-flex__shrink-7".to_string(), ItemShrink::Is7 => "pt-flex__shrink-7",
ItemShrink::Is8 => "pt-flex__shrink-8".to_string(), ItemShrink::Is8 => "pt-flex__shrink-8",
ItemShrink::Is9 => "pt-flex__shrink-9".to_string(), ItemShrink::Is9 => "pt-flex__shrink-9",
} };
write!(f, "{item_shrink}")
} }
} }
@ -278,22 +270,23 @@ pub enum ItemSize {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for ItemSize { impl fmt::Display for ItemSize {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { let item_size = match self {
ItemSize::Default => "".to_string(), ItemSize::Default => "",
ItemSize::Percent10 => "pt-flex__width-10".to_string(), ItemSize::Percent10 => "pt-flex__width-10",
ItemSize::Percent20 => "pt-flex__width-20".to_string(), ItemSize::Percent20 => "pt-flex__width-20",
ItemSize::Percent25 => "pt-flex__width-25".to_string(), ItemSize::Percent25 => "pt-flex__width-25",
ItemSize::Percent33 => "pt-flex__width-33".to_string(), ItemSize::Percent33 => "pt-flex__width-33",
ItemSize::Percent40 => "pt-flex__width-40".to_string(), ItemSize::Percent40 => "pt-flex__width-40",
ItemSize::Percent50 => "pt-flex__width-50".to_string(), ItemSize::Percent50 => "pt-flex__width-50",
ItemSize::Percent60 => "pt-flex__width-60".to_string(), ItemSize::Percent60 => "pt-flex__width-60",
ItemSize::Percent66 => "pt-flex__width-66".to_string(), ItemSize::Percent66 => "pt-flex__width-66",
ItemSize::Percent75 => "pt-flex__width-75".to_string(), ItemSize::Percent75 => "pt-flex__width-75",
ItemSize::Percent80 => "pt-flex__width-80".to_string(), ItemSize::Percent80 => "pt-flex__width-80",
ItemSize::Percent90 => "pt-flex__width-90".to_string(), ItemSize::Percent90 => "pt-flex__width-90",
} };
write!(f, "{item_size}")
} }
} }
@ -317,21 +310,22 @@ pub enum ItemOffset {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for ItemOffset { impl fmt::Display for ItemOffset {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { let item_offset = match self {
ItemOffset::Default => "".to_string(), ItemOffset::Default => "",
ItemOffset::Offset10 => "pt-flex__offset-10".to_string(), ItemOffset::Offset10 => "pt-flex__offset-10",
ItemOffset::Offset20 => "pt-flex__offset-20".to_string(), ItemOffset::Offset20 => "pt-flex__offset-20",
ItemOffset::Offset25 => "pt-flex__offset-25".to_string(), ItemOffset::Offset25 => "pt-flex__offset-25",
ItemOffset::Offset33 => "pt-flex__offset-33".to_string(), ItemOffset::Offset33 => "pt-flex__offset-33",
ItemOffset::Offset40 => "pt-flex__offset-40".to_string(), ItemOffset::Offset40 => "pt-flex__offset-40",
ItemOffset::Offset50 => "pt-flex__offset-50".to_string(), ItemOffset::Offset50 => "pt-flex__offset-50",
ItemOffset::Offset60 => "pt-flex__offset-60".to_string(), ItemOffset::Offset60 => "pt-flex__offset-60",
ItemOffset::Offset66 => "pt-flex__offset-66".to_string(), ItemOffset::Offset66 => "pt-flex__offset-66",
ItemOffset::Offset75 => "pt-flex__offset-75".to_string(), ItemOffset::Offset75 => "pt-flex__offset-75",
ItemOffset::Offset80 => "pt-flex__offset-80".to_string(), ItemOffset::Offset80 => "pt-flex__offset-80",
ItemOffset::Offset90 => "pt-flex__offset-90".to_string(), ItemOffset::Offset90 => "pt-flex__offset-90",
} };
write!(f, "{item_offset}")
} }
} }

View file

@ -1,5 +1,7 @@
use crate::prelude::*; use crate::prelude::*;
use std::fmt;
#[derive(Default)] #[derive(Default)]
pub enum ButtonType { pub enum ButtonType {
#[default] #[default]
@ -8,6 +10,18 @@ pub enum ButtonType {
Reset, Reset,
} }
#[rustfmt::skip]
impl fmt::Display for ButtonType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let button_type = match self {
ButtonType::Button => "button",
ButtonType::Submit => "submit",
ButtonType::Reset => "reset",
};
write!(f, "{button_type}")
}
}
#[rustfmt::skip] #[rustfmt::skip]
#[derive(Default)] #[derive(Default)]
pub struct Button { pub struct Button {

View file

@ -1,5 +1,7 @@
use crate::prelude::*; use crate::prelude::*;
use std::fmt;
#[derive(Default)] #[derive(Default)]
pub enum HeadingType { pub enum HeadingType {
#[default] #[default]
@ -24,17 +26,18 @@ pub enum HeadingDisplay {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for HeadingDisplay { impl fmt::Display for HeadingDisplay {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { let heading_display = match self {
HeadingDisplay::ExtraLarge => "pt-heading__title-x3l".to_string(), HeadingDisplay::ExtraLarge => "pt-heading__title-x3l",
HeadingDisplay::XxLarge => "pt-heading__title-x2l".to_string(), HeadingDisplay::XxLarge => "pt-heading__title-x2l",
HeadingDisplay::XLarge => "pt-heading__title-xl".to_string(), HeadingDisplay::XLarge => "pt-heading__title-xl",
HeadingDisplay::Large => "pt-heading__title-l".to_string(), HeadingDisplay::Large => "pt-heading__title-l",
HeadingDisplay::Medium => "pt-heading__title-m".to_string(), HeadingDisplay::Medium => "pt-heading__title-m",
HeadingDisplay::Normal => "".to_string(), HeadingDisplay::Normal => "",
HeadingDisplay::Subtitle => "pt-heading__subtitle".to_string(), HeadingDisplay::Subtitle => "pt-heading__subtitle",
} };
write!(f, "{heading_display}")
} }
} }

View file

@ -1,4 +1,4 @@
use crate::concat_string; use std::fmt;
// About pixels: Pixels (px) are relative to the viewing device. For low-dpi devices, 1px is one // About pixels: Pixels (px) are relative to the viewing device. For low-dpi devices, 1px is one
// device pixel (dot) of the display. For printers and high resolution screens 1px implies multiple // device pixel (dot) of the display. For printers and high resolution screens 1px implies multiple
@ -31,24 +31,24 @@ pub enum Value {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for Value { impl fmt::Display for Value {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Value::None => "".to_owned(), Value::None => write!(f, ""),
Value::Auto => "auto".to_owned(), Value::Auto => write!(f, "auto"),
// Absolute value. // Absolute value.
Value::Cm(av) => concat_string!(av.to_string(), "cm"), Value::Cm(av) => write!(f, "{av}cm"),
Value::In(av) => concat_string!(av.to_string(), "in"), Value::In(av) => write!(f, "{av}in"),
Value::Mm(av) => concat_string!(av.to_string(), "mm"), Value::Mm(av) => write!(f, "{av}mm"),
Value::Pc(av) => concat_string!(av.to_string(), "pc"), Value::Pc(av) => write!(f, "{av}pc"),
Value::Pt(av) => concat_string!(av.to_string(), "pt"), Value::Pt(av) => write!(f, "{av}pt"),
Value::Px(av) => concat_string!(av.to_string(), "px"), Value::Px(av) => write!(f, "{av}px"),
// Relative value. // Relative value.
Value::RelEm(rv) => concat_string!(rv.to_string(), "em"), Value::RelEm(rv) => write!(f, "{rv}em"),
Value::RelPct(rv) => concat_string!(rv.to_string(), "%"), Value::RelPct(rv) => write!(f, "{rv}%"),
Value::RelRem(rv) => concat_string!(rv.to_string(), "rem"), Value::RelRem(rv) => write!(f, "{rv}rem"),
Value::RelVh(rv) => concat_string!(rv.to_string(), "vh"), Value::RelVh(rv) => write!(f, "{rv}vh"),
Value::RelVw(rv) => concat_string!(rv.to_string(), "vw"), Value::RelVw(rv) => write!(f, "{rv}vw"),
} }
} }
} }

View file

@ -99,6 +99,7 @@ pub(crate) use fluent_templates::StaticLoader as Locales;
use unic_langid::langid; use unic_langid::langid;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt;
const LANGUAGE_SET_FAILURE: &str = "language_set_failure"; const LANGUAGE_SET_FAILURE: &str = "language_set_failure";
@ -244,12 +245,13 @@ impl L10n {
} }
} }
impl ToString for L10n { #[rustfmt::skip]
fn to_string(&self) -> String { impl fmt::Display for L10n {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.op { match &self.op {
L10nOp::None => "".to_owned(), L10nOp::None => write!(f, ""),
L10nOp::Text(text) => text.to_owned(), L10nOp::Text(text) => write!(f, "{text}"),
L10nOp::Translate(key) => match self.locales { L10nOp::Translate(key) => write!(f, "{}", match self.locales {
Some(locales) => locales Some(locales) => locales
.lookup_with_args( .lookup_with_args(
match key.as_str() { match key.as_str() {
@ -267,7 +269,7 @@ impl ToString for L10n {
) )
.unwrap_or(key.to_owned()), .unwrap_or(key.to_owned()),
None => key.to_owned(), None => key.to_owned(),
}, }),
} }
} }
} }