From 2c6e9238abc66c4764f7f0d64d148767b323a276 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sat, 27 Jul 2024 20:12:07 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20Replace=20"?= =?UTF-8?q?impl=20ToString"=20with=20"impl=20Display"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/base/component.rs | 78 ++++---- src/base/component/flex.rs | 236 +++++++++++------------ src/base/component/form/action_button.rs | 14 +- src/base/component/heading.rs | 24 +-- src/html/unit.rs | 34 ++-- src/locale.rs | 69 ++++--- 6 files changed, 230 insertions(+), 225 deletions(-) diff --git a/src/base/component.rs b/src/base/component.rs index d0c8b23d..6c3b9a3f 100644 --- a/src/base/component.rs +++ b/src/base/component.rs @@ -2,6 +2,8 @@ use crate::core::component::{AssetsOp, Context}; use crate::html::{JavaScript, StyleSheet}; use crate::{AutoDefault, Weight}; +use std::fmt; + // Context parameters. pub const PARAM_BASE_WEIGHT: &str = "base.weight"; pub const PARAM_BASE_INCLUDE_ICONS: &str = "base.include.icon"; @@ -74,18 +76,18 @@ pub enum BreakPoint { } #[rustfmt::skip] -impl ToString for BreakPoint { - fn to_string(&self) -> String { - String::from(match self { - BreakPoint::None => "bp__none", - BreakPoint::SM => "bp__sm", - BreakPoint::MD => "bp__md", - BreakPoint::LG => "bp__lg", - BreakPoint::XL => "bp__xl", - BreakPoint::X2L => "bp__x2l", - BreakPoint::X3L => "bp__x3l", - BreakPoint::X2K => "bp__x2k", - }) +impl fmt::Display for BreakPoint { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + BreakPoint::None => write!(f, "bp__none"), + BreakPoint::SM => write!(f, "bp__sm"), + BreakPoint::MD => write!(f, "bp__md"), + BreakPoint::LG => write!(f, "bp__lg"), + BreakPoint::XL => write!(f, "bp__xl"), + BreakPoint::X2L => write!(f, "bp__x2l"), + BreakPoint::X3L => write!(f, "bp__x3l"), + BreakPoint::X2K => write!(f, "bp__x2k"), + } } } @@ -105,18 +107,18 @@ pub enum StyleBase { } #[rustfmt::skip] -impl ToString for StyleBase { - fn to_string(&self) -> String { - String::from(match self { - StyleBase::Default => "style__default", - StyleBase::Info => "style__info", - StyleBase::Success => "style__success", - StyleBase::Warning => "style__warning", - StyleBase::Danger => "style__danger", - StyleBase::Light => "style__light", - StyleBase::Dark => "style__dark", - StyleBase::Link => "style__link", - }) +impl fmt::Display for StyleBase { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + StyleBase::Default => write!(f, "style__default"), + StyleBase::Info => write!(f, "style__info"), + StyleBase::Success => write!(f, "style__success"), + StyleBase::Warning => write!(f, "style__warning"), + StyleBase::Danger => write!(f, "style__danger"), + StyleBase::Light => write!(f, "style__light"), + StyleBase::Dark => write!(f, "style__dark"), + StyleBase::Link => write!(f, "style__link"), + } } } @@ -138,20 +140,20 @@ pub enum FontSize { } #[rustfmt::skip] -impl ToString for FontSize { - fn to_string(&self) -> String { - String::from(match self { - FontSize::ExtraLarge => "fs__x3l", - FontSize::XxLarge => "fs__x2l", - FontSize::XLarge => "fs__xl", - FontSize::Large => "fs__l", - FontSize::Medium => "fs__m", - FontSize::Normal => "", - FontSize::Small => "fs__s", - FontSize::XSmall => "fs__xs", - FontSize::XxSmall => "fs__x2s", - FontSize::ExtraSmall => "fs__x3s", - }) +impl fmt::Display for FontSize { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + FontSize::ExtraLarge => write!(f, "fs__x3l"), + FontSize::XxLarge => write!(f, "fs__x2l"), + FontSize::XLarge => write!(f, "fs__xl"), + FontSize::Large => write!(f, "fs__l"), + FontSize::Medium => write!(f, "fs__m"), + FontSize::Normal => write!(f, ""), + FontSize::Small => write!(f, "fs__s"), + FontSize::XSmall => write!(f, "fs__xs"), + FontSize::XxSmall => write!(f, "fs__x2s"), + FontSize::ExtraSmall => write!(f, "fs__x3s"), + } } } diff --git a/src/base/component/flex.rs b/src/base/component/flex.rs index 9753bc14..0b732dcd 100644 --- a/src/base/component/flex.rs +++ b/src/base/component/flex.rs @@ -6,6 +6,8 @@ pub use item::Item; use crate::prelude::*; +use std::fmt; + // ************************************************************************************************* #[derive(AutoDefault)] @@ -19,24 +21,14 @@ pub enum Direction { } #[rustfmt::skip] -impl ToString for Direction { - fn to_string(&self) -> String { +impl fmt::Display for Direction { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Direction::Default => concat_string!( - "flex__row ", BreakPoint::default().to_string() - ), - Direction::Row(breakpoint) => concat_string!( - "flex__row ", breakpoint.to_string() - ), - Direction::RowReverse(breakpoint) => concat_string!( - "flex__row flex__reverse ", breakpoint.to_string() - ), - Direction::Column(breakpoint) => concat_string!( - "flex__col ", breakpoint.to_string() - ), - Direction::ColumnReverse(breakpoint) => concat_string!( - "flex__col flex__reverse ", breakpoint.to_string() - ), + Direction::Default => write!(f, "flex__row {}", BreakPoint::default()), + Direction::Row(bp) => write!(f, "flex__row {bp}"), + Direction::RowReverse(bp) => write!(f, "flex__row flex__reverse {bp}"), + Direction::Column(bp) => write!(f, "flex__col {bp}"), + Direction::ColumnReverse(bp) => write!(f, "flex__col flex__reverse {bp}"), } } } @@ -53,13 +45,13 @@ pub enum Wrap { } #[rustfmt::skip] -impl ToString for Wrap { - fn to_string(&self) -> String { +impl fmt::Display for Wrap { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Wrap::Default => "".to_owned(), - Wrap::NoWrap => "flex__nowrap".to_owned(), - Wrap::Wrap(a) => concat_string!("flex__wrap ", a.to_string()), - Wrap::WrapReverse(a) => concat_string!("flex__wrap-reverse ", a.to_string()), + Wrap::Default => write!(f, ""), + Wrap::NoWrap => write!(f, "flex__nowrap"), + Wrap::Wrap(a) => write!(f, "flex__wrap {a}"), + Wrap::WrapReverse(a) => write!(f, "flex__wrap-reverse {a}"), } } } @@ -79,17 +71,17 @@ pub enum ContentAlign { } #[rustfmt::skip] -impl ToString for ContentAlign { - fn to_string(&self) -> String { - String::from(match self { - ContentAlign::Default => "", - ContentAlign::Start => "flex__align-start", - ContentAlign::End => "flex__align-end", - ContentAlign::Center => "flex__align-center", - ContentAlign::Stretch => "flex__align-stretch", - ContentAlign::SpaceBetween => "flex__align-space-between", - ContentAlign::SpaceAround => "flex__align-space-around", - }) +impl fmt::Display for ContentAlign { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ContentAlign::Default => write!(f, ""), + ContentAlign::Start => write!(f, "flex__align-start"), + ContentAlign::End => write!(f, "flex__align-end"), + ContentAlign::Center => write!(f, "flex__align-center"), + ContentAlign::Stretch => write!(f, "flex__align-stretch"), + ContentAlign::SpaceBetween => write!(f, "flex__align-space-between"), + ContentAlign::SpaceAround => write!(f, "flex__align-space-around"), + } } } @@ -108,17 +100,17 @@ pub enum Justify { } #[rustfmt::skip] -impl ToString for Justify { - fn to_string(&self) -> String { - String::from(match self { - Justify::Default => "", - Justify::Start => "flex__justify-start", - Justify::End => "flex__justify-end", - Justify::Center => "flex__justify-center", - Justify::SpaceBetween => "flex__justify-space-between", - Justify::SpaceAround => "flex__justify-space-around", - Justify::SpaceEvenly => "flex__justify-space-evenly", - }) +impl fmt::Display for Justify { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Justify::Default => write!(f, ""), + Justify::Start => write!(f, "flex__justify-start"), + Justify::End => write!(f, "flex__justify-end"), + Justify::Center => write!(f, "flex__justify-center"), + Justify::SpaceBetween => write!(f, "flex__justify-space-between"), + Justify::SpaceAround => write!(f, "flex__justify-space-around"), + Justify::SpaceEvenly => write!(f, "flex__justify-space-evenly"), + } } } @@ -136,16 +128,16 @@ pub enum Align { } #[rustfmt::skip] -impl ToString for Align { - fn to_string(&self) -> String { - String::from(match self { - Align::Default => "", - Align::Start => "flex__start", - Align::End => "flex__end", - Align::Center => "flex__center", - Align::Stretch => "flex__stretch", - Align::Baseline => "flex__baseline", - }) +impl fmt::Display for Align { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Align::Default => write!(f, ""), + Align::Start => write!(f, "flex__start"), + Align::End => write!(f, "flex__end"), + Align::Center => write!(f, "flex__center"), + Align::Stretch => write!(f, "flex__stretch"), + Align::Baseline => write!(f, "flex__baseline"), + } } } @@ -162,14 +154,14 @@ pub enum Gap { } #[rustfmt::skip] -impl ToString for Gap { - fn to_string(&self) -> String { +impl fmt::Display for Gap { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Gap::Default => "".to_owned(), - Gap::Row(r) => concat_string!("row-gap: ", r.to_string(), ";"), - Gap::Column(c) => concat_string!("column-gap: ", c.to_string(), ";"), - Gap::Distinct(r, c) => concat_string!("gap: ", r.to_string(), " ", c.to_string(), ";"), - Gap::Both(v) => concat_string!("gap: ", v.to_string(), ";"), + Gap::Default => write!(f, ""), + Gap::Row(r) => write!(f, "row-gap: {r};"), + Gap::Column(c) => write!(f, "column-gap: {c};"), + Gap::Distinct(r, c) => write!(f, "gap: {r} {c};"), + Gap::Both(v) => write!(f, "gap: {v};"), } } } @@ -191,20 +183,20 @@ pub enum Grow { Is9, } -impl ToString for Grow { - fn to_string(&self) -> String { - String::from(match self { - Grow::Default => "", - Grow::Is1 => "flex__grow-1", - Grow::Is2 => "flex__grow-2", - Grow::Is3 => "flex__grow-3", - Grow::Is4 => "flex__grow-4", - Grow::Is5 => "flex__grow-5", - Grow::Is6 => "flex__grow-6", - Grow::Is7 => "flex__grow-7", - Grow::Is8 => "flex__grow-8", - Grow::Is9 => "flex__grow-9", - }) +impl fmt::Display for Grow { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Grow::Default => write!(f, ""), + Grow::Is1 => write!(f, "flex__grow-1"), + Grow::Is2 => write!(f, "flex__grow-2"), + Grow::Is3 => write!(f, "flex__grow-3"), + Grow::Is4 => write!(f, "flex__grow-4"), + Grow::Is5 => write!(f, "flex__grow-5"), + Grow::Is6 => write!(f, "flex__grow-6"), + Grow::Is7 => write!(f, "flex__grow-7"), + Grow::Is8 => write!(f, "flex__grow-8"), + Grow::Is9 => write!(f, "flex__grow-9"), + } } } @@ -225,20 +217,20 @@ pub enum Shrink { Is9, } -impl ToString for Shrink { - fn to_string(&self) -> String { - String::from(match self { - Shrink::Default => "", - Shrink::Is1 => "flex__shrink-1", - Shrink::Is2 => "flex__shrink-2", - Shrink::Is3 => "flex__shrink-3", - Shrink::Is4 => "flex__shrink-4", - Shrink::Is5 => "flex__shrink-5", - Shrink::Is6 => "flex__shrink-6", - Shrink::Is7 => "flex__shrink-7", - Shrink::Is8 => "flex__shrink-8", - Shrink::Is9 => "flex__shrink-9", - }) +impl fmt::Display for Shrink { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Shrink::Default => write!(f, ""), + Shrink::Is1 => write!(f, "flex__shrink-1"), + Shrink::Is2 => write!(f, "flex__shrink-2"), + Shrink::Is3 => write!(f, "flex__shrink-3"), + Shrink::Is4 => write!(f, "flex__shrink-4"), + Shrink::Is5 => write!(f, "flex__shrink-5"), + Shrink::Is6 => write!(f, "flex__shrink-6"), + Shrink::Is7 => write!(f, "flex__shrink-7"), + Shrink::Is8 => write!(f, "flex__shrink-8"), + Shrink::Is9 => write!(f, "flex__shrink-9"), + } } } @@ -261,22 +253,22 @@ pub enum Size { Percent90, } -impl ToString for Size { - fn to_string(&self) -> String { - String::from(match self { - Size::Default => "", - Size::Percent10 => "flex__size-10", - Size::Percent20 => "flex__size-20", - Size::Percent25 => "flex__size-25", - Size::Percent33 => "flex__size-33", - Size::Percent40 => "flex__size-40", - Size::Percent50 => "flex__size-50", - Size::Percent60 => "flex__size-60", - Size::Percent66 => "flex__size-66", - Size::Percent75 => "flex__size-75", - Size::Percent80 => "flex__size-80", - Size::Percent90 => "flex__size-90", - }) +impl fmt::Display for Size { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Size::Default => write!(f, ""), + Size::Percent10 => write!(f, "flex__size-10"), + Size::Percent20 => write!(f, "flex__size-20"), + Size::Percent25 => write!(f, "flex__size-25"), + Size::Percent33 => write!(f, "flex__size-33"), + Size::Percent40 => write!(f, "flex__size-40"), + Size::Percent50 => write!(f, "flex__size-50"), + Size::Percent60 => write!(f, "flex__size-60"), + Size::Percent66 => write!(f, "flex__size-66"), + Size::Percent75 => write!(f, "flex__size-75"), + Size::Percent80 => write!(f, "flex__size-80"), + Size::Percent90 => write!(f, "flex__size-90"), + } } } @@ -299,21 +291,21 @@ pub enum Offset { Offset90, } -impl ToString for Offset { - fn to_string(&self) -> String { - String::from(match self { - Offset::Default => "", - Offset::Offset10 => "flex__offset-10", - Offset::Offset20 => "flex__offset-20", - Offset::Offset25 => "flex__offset-25", - Offset::Offset33 => "flex__offset-33", - Offset::Offset40 => "flex__offset-40", - Offset::Offset50 => "flex__offset-50", - Offset::Offset60 => "flex__offset-60", - Offset::Offset66 => "flex__offset-66", - Offset::Offset75 => "flex__offset-75", - Offset::Offset80 => "flex__offset-80", - Offset::Offset90 => "flex__offset-90", - }) +impl fmt::Display for Offset { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Offset::Default => write!(f, ""), + Offset::Offset10 => write!(f, "flex__offset-10"), + Offset::Offset20 => write!(f, "flex__offset-20"), + Offset::Offset25 => write!(f, "flex__offset-25"), + Offset::Offset33 => write!(f, "flex__offset-33"), + Offset::Offset40 => write!(f, "flex__offset-40"), + Offset::Offset50 => write!(f, "flex__offset-50"), + Offset::Offset60 => write!(f, "flex__offset-60"), + Offset::Offset66 => write!(f, "flex__offset-66"), + Offset::Offset75 => write!(f, "flex__offset-75"), + Offset::Offset80 => write!(f, "flex__offset-80"), + Offset::Offset90 => write!(f, "flex__offset-90"), + } } } diff --git a/src/base/component/form/action_button.rs b/src/base/component/form/action_button.rs index 93a99e65..cadbe391 100644 --- a/src/base/component/form/action_button.rs +++ b/src/base/component/form/action_button.rs @@ -1,5 +1,7 @@ use crate::prelude::*; +use std::fmt; + #[derive(AutoDefault)] pub enum ActionButtonType { #[default] @@ -8,12 +10,12 @@ pub enum ActionButtonType { } #[rustfmt::skip] -impl ToString for ActionButtonType { - fn to_string(&self) -> String { - String::from(match self { - ActionButtonType::Submit => "submit", - ActionButtonType::Reset => "reset", - }) +impl fmt::Display for ActionButtonType { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ActionButtonType::Submit => write!(f, "submit"), + ActionButtonType::Reset => write!(f, "reset"), + } } } diff --git a/src/base/component/heading.rs b/src/base/component/heading.rs index 30d33f45..7cb3b594 100644 --- a/src/base/component/heading.rs +++ b/src/base/component/heading.rs @@ -1,5 +1,7 @@ use crate::prelude::*; +use std::fmt; + #[derive(AutoDefault)] pub enum HeadingType { #[default] @@ -24,17 +26,17 @@ pub enum HeadingSize { } #[rustfmt::skip] -impl ToString for HeadingSize { - fn to_string(&self) -> String { - String::from(match self { - HeadingSize::ExtraLarge => "heading__title-x3l", - HeadingSize::XxLarge => "heading__title-x2l", - HeadingSize::XLarge => "heading__title-xl", - HeadingSize::Large => "heading__title-l", - HeadingSize::Medium => "heading__title-m", - HeadingSize::Normal => "", - HeadingSize::Subtitle => "heading__subtitle", - }) +impl fmt::Display for HeadingSize { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + HeadingSize::ExtraLarge => write!(f, "heading__title-x3l"), + HeadingSize::XxLarge => write!(f, "heading__title-x2l"), + HeadingSize::XLarge => write!(f, "heading__title-xl"), + HeadingSize::Large => write!(f, "heading__title-l"), + HeadingSize::Medium => write!(f, "heading__title-m"), + HeadingSize::Normal => write!(f, ""), + HeadingSize::Subtitle => write!(f, "heading__subtitle"), + } } } diff --git a/src/html/unit.rs b/src/html/unit.rs index 4af81be6..5a153c55 100644 --- a/src/html/unit.rs +++ b/src/html/unit.rs @@ -1,4 +1,6 @@ -use crate::{concat_string, AutoDefault}; +use crate::AutoDefault; + +use std::fmt; // 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 @@ -31,24 +33,24 @@ pub enum Value { } #[rustfmt::skip] -impl ToString for Value { - fn to_string(&self) -> String { +impl fmt::Display for Value { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Value::None => "".to_owned(), - Value::Auto => "auto".to_owned(), + Value::None => write!(f, ""), + Value::Auto => write!(f, "auto"), // Absolute value. - Value::Cm(av) => concat_string!(av.to_string(), "cm"), - Value::In(av) => concat_string!(av.to_string(), "in"), - Value::Mm(av) => concat_string!(av.to_string(), "mm"), - Value::Pc(av) => concat_string!(av.to_string(), "pc"), - Value::Pt(av) => concat_string!(av.to_string(), "pt"), - Value::Px(av) => concat_string!(av.to_string(), "px"), + Value::Cm(av) => write!(f, "{av}cm"), + Value::In(av) => write!(f, "{av}in"), + Value::Mm(av) => write!(f, "{av}mm"), + Value::Pc(av) => write!(f, "{av}pc"), + Value::Pt(av) => write!(f, "{av}pt"), + Value::Px(av) => write!(f, "{av}px"), // Relative value. - Value::RelEm(rv) => concat_string!(rv.to_string(), "em"), - Value::RelPct(rv) => concat_string!(rv.to_string(), "%"), - Value::RelRem(rv) => concat_string!(rv.to_string(), "rem"), - Value::RelVh(rv) => concat_string!(rv.to_string(), "vh"), - Value::RelVw(rv) => concat_string!(rv.to_string(), "vw"), + Value::RelEm(rv) => write!(f, "{rv}em"), + Value::RelPct(rv) => write!(f, "{rv}%"), + Value::RelRem(rv) => write!(f, "{rv}rem"), + Value::RelVh(rv) => write!(f, "{rv}vh"), + Value::RelVw(rv) => write!(f, "{rv}vw"), } } } diff --git a/src/locale.rs b/src/locale.rs index 33f0cbc1..4bed2aa9 100644 --- a/src/locale.rs +++ b/src/locale.rs @@ -246,40 +246,45 @@ impl L10n { } } -impl ToString for L10n { - fn to_string(&self) -> String { +impl fmt::Display for L10n { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match &self.op { - L10nOp::None => "".to_owned(), - L10nOp::Text(text) => text.to_owned(), - L10nOp::Translate(key) => match self.locales { - Some(locales) => { - if self.args.is_empty() { - locales.lookup( - match key.as_str() { - LANGUAGE_SET_FAILURE => &LANGID_FALLBACK, - _ => &LANGID_DEFAULT, - }, - key, - ) - } else { - locales.lookup_with_args( - match key.as_str() { - LANGUAGE_SET_FAILURE => &LANGID_FALLBACK, - _ => &LANGID_DEFAULT, - }, - key, - &self - .args - .iter() - .fold(HashMap::new(), |mut args, (key, value)| { - args.insert(key.to_string(), value.to_owned().into()); - args - }), - ) - } + L10nOp::None => write!(f, ""), + L10nOp::Text(text) => write!(f, "{text}"), + L10nOp::Translate(key) => { + if let Some(locales) = self.locales { + write!( + f, + "{}", + if self.args.is_empty() { + locales.lookup( + match key.as_str() { + LANGUAGE_SET_FAILURE => &LANGID_FALLBACK, + _ => &LANGID_DEFAULT, + }, + key, + ) + } else { + locales.lookup_with_args( + match key.as_str() { + LANGUAGE_SET_FAILURE => &LANGID_FALLBACK, + _ => &LANGID_DEFAULT, + }, + key, + &self + .args + .iter() + .fold(HashMap::new(), |mut args, (key, value)| { + args.insert(key.to_string(), value.to_owned().into()); + args + }), + ) + } + ) + } else { + write!(f, "Unknown localization {key}") } - None => format!("Unknown localization {}", key), - }, + } } } }