css-style 0.8.0

Typed CSS Style
Documentation
use crate::{css, unit::*, StyleUpdater};
use derive_rich::Rich;
use std::borrow::Cow;

/// ```
/// use css_style::{css, Style, Color, unit::em};
/// use palette::rgb::Rgb;
///
/// Style::default()
///     .and_font(|conf| {
///         // set the font size to xx-large
///         conf.xx_large()
///             // we can set the font size with unit functions too
///             .size(em(1.5))
///             // set font variant to smal-caps
///             .small_caps()
///             // set font to be bold
///             .bold()
///             // we can pick specific weight (e.g. 200)
///             .weight_200()
///     });
/// ```
#[derive(Rich, Clone, Debug, PartialEq, Default)]
pub struct Font {
    #[rich(write, write(option))]
    pub family: Option<Family>,
    #[rich(write(rename = size), write(option, rename = try_size), value_fns = {
        medium = css::Medium,
        xx_small = css::XXSmall,
        x_small = css::XSmall,
        small = css::Small,
        large = css::Large,
        x_large = css::XLarge,
        xx_large = css::XXLarge,
        smaller = css::Smaller,
        larger = css::Larger,
    })]
    pub size: Option<Size>,
    #[rich(write(rename = style), write(option, rename = try_style), value_fns = {
        normal_style = css::Normal,
        italic = css::Italic,
        oblique = css::Oblique,
    })]
    pub style: Option<Style>,
    #[rich(write(rename = variant), write(option, rename = try_variant), value_fns = {
        normal_variant = css::Normal,
        small_caps = css::SmallCaps,
    })]
    pub variant: Option<Variant>,
    #[rich(write(rename = weight), write(option, rename = try_weight), value_fns = {
        normal_weight = css::Normal,
        bold = css::Bold,
        bolder = css::Bolder,
        lighter = css::Lighter,
        weight_100 = Weight::L100,
        weight_200 = Weight::L200,
        weight_300 = Weight::L300,
        weight_400 = Weight::L400,
        weight_500 = Weight::L500,
        weight_600 = Weight::L600,
        weight_700 = Weight::L700,
        weight_800 = Weight::L800,
        weight_900 = Weight::L900,
    })]
    pub weight: Option<Weight>,
}

impl StyleUpdater for Font {
    fn update_style(self, style: crate::Style) -> crate::Style {
        style
            .try_insert("font-family", self.family.clone())
            .try_insert("font-size", self.size)
            .try_insert("font-style", self.style)
            .try_insert("font-variant", self.variant)
            .try_insert("font-weight", self.weight)
    }
}

#[derive(Clone, Debug, PartialEq, Display, From)]
pub enum Family {
    #[display(fmt = "{}", "_0.join(\" \")")]
    Family(Vec<Cow<'static, str>>),
    Initial(css::Initial),
    Inherit(css::Inherit),
}

impl From<Cow<'static, str>> for Family {
    fn from(source: Cow<'static, str>) -> Self {
        Family::Family(vec![source])
    }
}

impl From<String> for Family {
    fn from(source: String) -> Self {
        Family::Family(vec![source.into()])
    }
}

impl From<&'static str> for Family {
    fn from(source: &'static str) -> Self {
        Family::Family(vec![source.into()])
    }
}

impl From<Vec<String>> for Family {
    fn from(source: Vec<String>) -> Self {
        Family::Family(source.into_iter().map(Into::into).collect())
    }
}

impl From<Vec<&'static str>> for Family {
    fn from(source: Vec<&'static str>) -> Self {
        Family::Family(source.into_iter().map(Into::into).collect())
    }
}

#[derive(Clone, Debug, PartialEq, Display, From)]
pub enum Size {
    Medium(css::Medium),
    XXSmall(css::XXSmall),
    XSmall(css::XSmall),
    Small(css::Small),
    Large(css::Large),
    XLarge(css::XLarge),
    XXLarge(css::XXLarge),
    Smaller(css::Smaller),
    Larger(css::Larger),
    Length(Length),
    Percent(Percent),
    Initial(css::Initial),
    Inherit(css::Inherit),
}

#[derive(Clone, Copy, Debug, PartialEq, Display, From)]
pub enum Style {
    Normal(css::Normal),
    Italic(css::Italic),
    Oblique(css::Oblique),
    Initial(css::Initial),
    Inherit(css::Inherit),
}

#[derive(Clone, Copy, Debug, PartialEq, Display, From)]
pub enum Variant {
    Normal(css::Normal),
    SmallCaps(css::SmallCaps),
    Initial(css::Initial),
    Inherit(css::Inherit),
}

#[derive(Clone, Copy, Debug, PartialEq, Display, From)]
pub enum Weight {
    #[from]
    Normal(css::Normal),
    #[from]
    Bold(css::Bold),
    #[from]
    Bolder(css::Bolder),
    #[from]
    Lighter(css::Lighter),
    #[display(fmt = "100")]
    L100,
    #[display(fmt = "200")]
    L200,
    #[display(fmt = "300")]
    L300,
    #[display(fmt = "400")]
    L400,
    #[display(fmt = "500")]
    L500,
    #[display(fmt = "600")]
    L600,
    #[display(fmt = "700")]
    L700,
    #[display(fmt = "800")]
    L800,
    #[display(fmt = "900")]
    L900,
    #[from]
    Initial(css::Initial),
    #[from]
    Inherit(css::Inherit),
}