🧑‍💻 Replace "impl ToString" with "impl Display"

This commit is contained in:
Manuel Cillero 2024-07-27 20:12:07 +02:00
parent c1e641723b
commit 2c6e9238ab
6 changed files with 230 additions and 225 deletions

View file

@ -2,6 +2,8 @@ use crate::core::component::{AssetsOp, Context};
use crate::html::{JavaScript, StyleSheet}; use crate::html::{JavaScript, StyleSheet};
use crate::{AutoDefault, Weight}; use crate::{AutoDefault, 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";
@ -74,18 +76,18 @@ 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 {
String::from(match self { match self {
BreakPoint::None => "bp__none", BreakPoint::None => write!(f, "bp__none"),
BreakPoint::SM => "bp__sm", BreakPoint::SM => write!(f, "bp__sm"),
BreakPoint::MD => "bp__md", BreakPoint::MD => write!(f, "bp__md"),
BreakPoint::LG => "bp__lg", BreakPoint::LG => write!(f, "bp__lg"),
BreakPoint::XL => "bp__xl", BreakPoint::XL => write!(f, "bp__xl"),
BreakPoint::X2L => "bp__x2l", BreakPoint::X2L => write!(f, "bp__x2l"),
BreakPoint::X3L => "bp__x3l", BreakPoint::X3L => write!(f, "bp__x3l"),
BreakPoint::X2K => "bp__x2k", BreakPoint::X2K => write!(f, "bp__x2k"),
}) }
} }
} }
@ -105,18 +107,18 @@ pub enum StyleBase {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for StyleBase { impl fmt::Display for StyleBase {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
String::from(match self { match self {
StyleBase::Default => "style__default", StyleBase::Default => write!(f, "style__default"),
StyleBase::Info => "style__info", StyleBase::Info => write!(f, "style__info"),
StyleBase::Success => "style__success", StyleBase::Success => write!(f, "style__success"),
StyleBase::Warning => "style__warning", StyleBase::Warning => write!(f, "style__warning"),
StyleBase::Danger => "style__danger", StyleBase::Danger => write!(f, "style__danger"),
StyleBase::Light => "style__light", StyleBase::Light => write!(f, "style__light"),
StyleBase::Dark => "style__dark", StyleBase::Dark => write!(f, "style__dark"),
StyleBase::Link => "style__link", StyleBase::Link => write!(f, "style__link"),
}) }
} }
} }
@ -138,20 +140,20 @@ 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 {
String::from(match self { match self {
FontSize::ExtraLarge => "fs__x3l", FontSize::ExtraLarge => write!(f, "fs__x3l"),
FontSize::XxLarge => "fs__x2l", FontSize::XxLarge => write!(f, "fs__x2l"),
FontSize::XLarge => "fs__xl", FontSize::XLarge => write!(f, "fs__xl"),
FontSize::Large => "fs__l", FontSize::Large => write!(f, "fs__l"),
FontSize::Medium => "fs__m", FontSize::Medium => write!(f, "fs__m"),
FontSize::Normal => "", FontSize::Normal => write!(f, ""),
FontSize::Small => "fs__s", FontSize::Small => write!(f, "fs__s"),
FontSize::XSmall => "fs__xs", FontSize::XSmall => write!(f, "fs__xs"),
FontSize::XxSmall => "fs__x2s", FontSize::XxSmall => write!(f, "fs__x2s"),
FontSize::ExtraSmall => "fs__x3s", FontSize::ExtraSmall => write!(f, "fs__x3s"),
}) }
} }
} }

View file

@ -6,6 +6,8 @@ pub use item::Item;
use crate::prelude::*; use crate::prelude::*;
use std::fmt;
// ************************************************************************************************* // *************************************************************************************************
#[derive(AutoDefault)] #[derive(AutoDefault)]
@ -19,24 +21,14 @@ 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 => concat_string!( Direction::Default => write!(f, "flex__row {}", BreakPoint::default()),
"flex__row ", BreakPoint::default().to_string() Direction::Row(bp) => write!(f, "flex__row {bp}"),
), Direction::RowReverse(bp) => write!(f, "flex__row flex__reverse {bp}"),
Direction::Row(breakpoint) => concat_string!( Direction::Column(bp) => write!(f, "flex__col {bp}"),
"flex__row ", breakpoint.to_string() Direction::ColumnReverse(bp) => write!(f, "flex__col flex__reverse {bp}"),
),
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()
),
} }
} }
} }
@ -53,13 +45,13 @@ pub enum Wrap {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for Wrap { impl fmt::Display for Wrap {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Wrap::Default => "".to_owned(), Wrap::Default => write!(f, ""),
Wrap::NoWrap => "flex__nowrap".to_owned(), Wrap::NoWrap => write!(f, "flex__nowrap"),
Wrap::Wrap(a) => concat_string!("flex__wrap ", a.to_string()), Wrap::Wrap(a) => write!(f, "flex__wrap {a}"),
Wrap::WrapReverse(a) => concat_string!("flex__wrap-reverse ", a.to_string()), Wrap::WrapReverse(a) => write!(f, "flex__wrap-reverse {a}"),
} }
} }
} }
@ -79,17 +71,17 @@ 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 {
String::from(match self { match self {
ContentAlign::Default => "", ContentAlign::Default => write!(f, ""),
ContentAlign::Start => "flex__align-start", ContentAlign::Start => write!(f, "flex__align-start"),
ContentAlign::End => "flex__align-end", ContentAlign::End => write!(f, "flex__align-end"),
ContentAlign::Center => "flex__align-center", ContentAlign::Center => write!(f, "flex__align-center"),
ContentAlign::Stretch => "flex__align-stretch", ContentAlign::Stretch => write!(f, "flex__align-stretch"),
ContentAlign::SpaceBetween => "flex__align-space-between", ContentAlign::SpaceBetween => write!(f, "flex__align-space-between"),
ContentAlign::SpaceAround => "flex__align-space-around", ContentAlign::SpaceAround => write!(f, "flex__align-space-around"),
}) }
} }
} }
@ -108,17 +100,17 @@ pub enum Justify {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for Justify { impl fmt::Display for Justify {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
String::from(match self { match self {
Justify::Default => "", Justify::Default => write!(f, ""),
Justify::Start => "flex__justify-start", Justify::Start => write!(f, "flex__justify-start"),
Justify::End => "flex__justify-end", Justify::End => write!(f, "flex__justify-end"),
Justify::Center => "flex__justify-center", Justify::Center => write!(f, "flex__justify-center"),
Justify::SpaceBetween => "flex__justify-space-between", Justify::SpaceBetween => write!(f, "flex__justify-space-between"),
Justify::SpaceAround => "flex__justify-space-around", Justify::SpaceAround => write!(f, "flex__justify-space-around"),
Justify::SpaceEvenly => "flex__justify-space-evenly", Justify::SpaceEvenly => write!(f, "flex__justify-space-evenly"),
}) }
} }
} }
@ -136,16 +128,16 @@ pub enum Align {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for Align { impl fmt::Display for Align {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
String::from(match self { match self {
Align::Default => "", Align::Default => write!(f, ""),
Align::Start => "flex__start", Align::Start => write!(f, "flex__start"),
Align::End => "flex__end", Align::End => write!(f, "flex__end"),
Align::Center => "flex__center", Align::Center => write!(f, "flex__center"),
Align::Stretch => "flex__stretch", Align::Stretch => write!(f, "flex__stretch"),
Align::Baseline => "flex__baseline", Align::Baseline => write!(f, "flex__baseline"),
}) }
} }
} }
@ -162,14 +154,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_owned(), 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};"),
} }
} }
} }
@ -191,20 +183,20 @@ pub enum Grow {
Is9, Is9,
} }
impl ToString for Grow { impl fmt::Display for Grow {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
String::from(match self { match self {
Grow::Default => "", Grow::Default => write!(f, ""),
Grow::Is1 => "flex__grow-1", Grow::Is1 => write!(f, "flex__grow-1"),
Grow::Is2 => "flex__grow-2", Grow::Is2 => write!(f, "flex__grow-2"),
Grow::Is3 => "flex__grow-3", Grow::Is3 => write!(f, "flex__grow-3"),
Grow::Is4 => "flex__grow-4", Grow::Is4 => write!(f, "flex__grow-4"),
Grow::Is5 => "flex__grow-5", Grow::Is5 => write!(f, "flex__grow-5"),
Grow::Is6 => "flex__grow-6", Grow::Is6 => write!(f, "flex__grow-6"),
Grow::Is7 => "flex__grow-7", Grow::Is7 => write!(f, "flex__grow-7"),
Grow::Is8 => "flex__grow-8", Grow::Is8 => write!(f, "flex__grow-8"),
Grow::Is9 => "flex__grow-9", Grow::Is9 => write!(f, "flex__grow-9"),
}) }
} }
} }
@ -225,20 +217,20 @@ pub enum Shrink {
Is9, Is9,
} }
impl ToString for Shrink { impl fmt::Display for Shrink {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
String::from(match self { match self {
Shrink::Default => "", Shrink::Default => write!(f, ""),
Shrink::Is1 => "flex__shrink-1", Shrink::Is1 => write!(f, "flex__shrink-1"),
Shrink::Is2 => "flex__shrink-2", Shrink::Is2 => write!(f, "flex__shrink-2"),
Shrink::Is3 => "flex__shrink-3", Shrink::Is3 => write!(f, "flex__shrink-3"),
Shrink::Is4 => "flex__shrink-4", Shrink::Is4 => write!(f, "flex__shrink-4"),
Shrink::Is5 => "flex__shrink-5", Shrink::Is5 => write!(f, "flex__shrink-5"),
Shrink::Is6 => "flex__shrink-6", Shrink::Is6 => write!(f, "flex__shrink-6"),
Shrink::Is7 => "flex__shrink-7", Shrink::Is7 => write!(f, "flex__shrink-7"),
Shrink::Is8 => "flex__shrink-8", Shrink::Is8 => write!(f, "flex__shrink-8"),
Shrink::Is9 => "flex__shrink-9", Shrink::Is9 => write!(f, "flex__shrink-9"),
}) }
} }
} }
@ -261,22 +253,22 @@ pub enum Size {
Percent90, Percent90,
} }
impl ToString for Size { impl fmt::Display for Size {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
String::from(match self { match self {
Size::Default => "", Size::Default => write!(f, ""),
Size::Percent10 => "flex__size-10", Size::Percent10 => write!(f, "flex__size-10"),
Size::Percent20 => "flex__size-20", Size::Percent20 => write!(f, "flex__size-20"),
Size::Percent25 => "flex__size-25", Size::Percent25 => write!(f, "flex__size-25"),
Size::Percent33 => "flex__size-33", Size::Percent33 => write!(f, "flex__size-33"),
Size::Percent40 => "flex__size-40", Size::Percent40 => write!(f, "flex__size-40"),
Size::Percent50 => "flex__size-50", Size::Percent50 => write!(f, "flex__size-50"),
Size::Percent60 => "flex__size-60", Size::Percent60 => write!(f, "flex__size-60"),
Size::Percent66 => "flex__size-66", Size::Percent66 => write!(f, "flex__size-66"),
Size::Percent75 => "flex__size-75", Size::Percent75 => write!(f, "flex__size-75"),
Size::Percent80 => "flex__size-80", Size::Percent80 => write!(f, "flex__size-80"),
Size::Percent90 => "flex__size-90", Size::Percent90 => write!(f, "flex__size-90"),
}) }
} }
} }
@ -299,21 +291,21 @@ pub enum Offset {
Offset90, Offset90,
} }
impl ToString for Offset { impl fmt::Display for Offset {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
String::from(match self { match self {
Offset::Default => "", Offset::Default => write!(f, ""),
Offset::Offset10 => "flex__offset-10", Offset::Offset10 => write!(f, "flex__offset-10"),
Offset::Offset20 => "flex__offset-20", Offset::Offset20 => write!(f, "flex__offset-20"),
Offset::Offset25 => "flex__offset-25", Offset::Offset25 => write!(f, "flex__offset-25"),
Offset::Offset33 => "flex__offset-33", Offset::Offset33 => write!(f, "flex__offset-33"),
Offset::Offset40 => "flex__offset-40", Offset::Offset40 => write!(f, "flex__offset-40"),
Offset::Offset50 => "flex__offset-50", Offset::Offset50 => write!(f, "flex__offset-50"),
Offset::Offset60 => "flex__offset-60", Offset::Offset60 => write!(f, "flex__offset-60"),
Offset::Offset66 => "flex__offset-66", Offset::Offset66 => write!(f, "flex__offset-66"),
Offset::Offset75 => "flex__offset-75", Offset::Offset75 => write!(f, "flex__offset-75"),
Offset::Offset80 => "flex__offset-80", Offset::Offset80 => write!(f, "flex__offset-80"),
Offset::Offset90 => "flex__offset-90", Offset::Offset90 => write!(f, "flex__offset-90"),
}) }
} }
} }

View file

@ -1,5 +1,7 @@
use crate::prelude::*; use crate::prelude::*;
use std::fmt;
#[derive(AutoDefault)] #[derive(AutoDefault)]
pub enum ActionButtonType { pub enum ActionButtonType {
#[default] #[default]
@ -8,12 +10,12 @@ pub enum ActionButtonType {
} }
#[rustfmt::skip] #[rustfmt::skip]
impl ToString for ActionButtonType { impl fmt::Display for ActionButtonType {
fn to_string(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
String::from(match self { match self {
ActionButtonType::Submit => "submit", ActionButtonType::Submit => write!(f, "submit"),
ActionButtonType::Reset => "reset", ActionButtonType::Reset => write!(f, "reset"),
}) }
} }
} }

View file

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

View file

@ -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 // 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 +33,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

@ -246,40 +246,45 @@ impl L10n {
} }
} }
impl ToString for L10n { impl fmt::Display for L10n {
fn to_string(&self) -> String { 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) => {
Some(locales) => { if let Some(locales) = self.locales {
if self.args.is_empty() { write!(
locales.lookup( f,
match key.as_str() { "{}",
LANGUAGE_SET_FAILURE => &LANGID_FALLBACK, if self.args.is_empty() {
_ => &LANGID_DEFAULT, locales.lookup(
}, match key.as_str() {
key, LANGUAGE_SET_FAILURE => &LANGID_FALLBACK,
) _ => &LANGID_DEFAULT,
} else { },
locales.lookup_with_args( key,
match key.as_str() { )
LANGUAGE_SET_FAILURE => &LANGID_FALLBACK, } else {
_ => &LANGID_DEFAULT, locales.lookup_with_args(
}, match key.as_str() {
key, LANGUAGE_SET_FAILURE => &LANGID_FALLBACK,
&self _ => &LANGID_DEFAULT,
.args },
.iter() key,
.fold(HashMap::new(), |mut args, (key, value)| { &self
args.insert(key.to_string(), value.to_owned().into()); .args
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), }
},
} }
} }
} }