use crate::traits::{ComputeValue, ToCss};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Spacing {
S0,
Px,
S0_5,
S1,
S1_5,
S2,
S2_5,
S3,
S3_5,
S4,
S5,
S6,
S7,
S8,
S9,
S10,
S11,
S12,
S14,
S16,
S20,
S24,
S28,
S32,
S36,
S40,
S44,
S48,
S52,
S56,
S60,
S64,
S72,
S80,
S96,
Auto,
}
impl Spacing {
pub fn to_rem(&self) -> Option<f32> {
match self {
Spacing::S0 => Some(0.0),
Spacing::Px => None, Spacing::S0_5 => Some(0.125),
Spacing::S1 => Some(0.25),
Spacing::S1_5 => Some(0.375),
Spacing::S2 => Some(0.5),
Spacing::S2_5 => Some(0.625),
Spacing::S3 => Some(0.75),
Spacing::S3_5 => Some(0.875),
Spacing::S4 => Some(1.0),
Spacing::S5 => Some(1.25),
Spacing::S6 => Some(1.5),
Spacing::S7 => Some(1.75),
Spacing::S8 => Some(2.0),
Spacing::S9 => Some(2.25),
Spacing::S10 => Some(2.5),
Spacing::S11 => Some(2.75),
Spacing::S12 => Some(3.0),
Spacing::S14 => Some(3.5),
Spacing::S16 => Some(4.0),
Spacing::S20 => Some(5.0),
Spacing::S24 => Some(6.0),
Spacing::S28 => Some(7.0),
Spacing::S32 => Some(8.0),
Spacing::S36 => Some(9.0),
Spacing::S40 => Some(10.0),
Spacing::S44 => Some(11.0),
Spacing::S48 => Some(12.0),
Spacing::S52 => Some(13.0),
Spacing::S56 => Some(14.0),
Spacing::S60 => Some(15.0),
Spacing::S64 => Some(16.0),
Spacing::S72 => Some(18.0),
Spacing::S80 => Some(20.0),
Spacing::S96 => Some(24.0),
Spacing::Auto => None,
}
}
pub fn to_px(&self) -> Option<i32> {
match self {
Spacing::S0 => Some(0),
Spacing::Px => Some(1),
Spacing::S0_5 => Some(2),
Spacing::S1 => Some(4),
Spacing::S1_5 => Some(6),
Spacing::S2 => Some(8),
Spacing::S2_5 => Some(10),
Spacing::S3 => Some(12),
Spacing::S3_5 => Some(14),
Spacing::S4 => Some(16),
Spacing::S5 => Some(20),
Spacing::S6 => Some(24),
Spacing::S7 => Some(28),
Spacing::S8 => Some(32),
Spacing::S9 => Some(36),
Spacing::S10 => Some(40),
Spacing::S11 => Some(44),
Spacing::S12 => Some(48),
Spacing::S14 => Some(56),
Spacing::S16 => Some(64),
Spacing::S20 => Some(80),
Spacing::S24 => Some(96),
Spacing::S28 => Some(112),
Spacing::S32 => Some(128),
Spacing::S36 => Some(144),
Spacing::S40 => Some(160),
Spacing::S44 => Some(176),
Spacing::S48 => Some(192),
Spacing::S52 => Some(208),
Spacing::S56 => Some(224),
Spacing::S60 => Some(240),
Spacing::S64 => Some(256),
Spacing::S72 => Some(288),
Spacing::S80 => Some(320),
Spacing::S96 => Some(384),
Spacing::Auto => None,
}
}
}
impl ComputeValue for Spacing {
type Output = String;
fn compute(&self) -> Self::Output {
match self {
Spacing::S0 => "0".to_string(),
Spacing::Px => "1px".to_string(),
Spacing::Auto => "auto".to_string(),
_ => {
if let Some(rem) = self.to_rem() {
format!("{}rem", rem)
} else {
"0".to_string()
}
}
}
}
}
impl ToCss for Spacing {
fn to_css(&self) -> String {
self.compute()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Percentage {
S0,
S1_2,
S1_3,
S2_3,
S1_4,
S2_4,
S3_4,
S1_5,
S2_5,
S3_5,
S4_5,
S1_6,
S2_6,
S3_6,
S4_6,
S5_6,
Full,
Min,
Max,
Fit,
}
impl ToCss for Percentage {
fn to_css(&self) -> String {
match self {
Percentage::S0 => "0%".to_string(),
Percentage::S1_2 => "50%".to_string(),
Percentage::S1_3 => "33.333333%".to_string(),
Percentage::S2_3 => "66.666667%".to_string(),
Percentage::S1_4 => "25%".to_string(),
Percentage::S2_4 => "50%".to_string(),
Percentage::S3_4 => "75%".to_string(),
Percentage::S1_5 => "20%".to_string(),
Percentage::S2_5 => "40%".to_string(),
Percentage::S3_5 => "60%".to_string(),
Percentage::S4_5 => "80%".to_string(),
Percentage::S1_6 => "16.666667%".to_string(),
Percentage::S2_6 => "33.333333%".to_string(),
Percentage::S3_6 => "50%".to_string(),
Percentage::S4_6 => "66.666667%".to_string(),
Percentage::S5_6 => "83.333333%".to_string(),
Percentage::Full => "100%".to_string(),
Percentage::Min => "min-content".to_string(),
Percentage::Max => "max-content".to_string(),
Percentage::Fit => "fit-content".to_string(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Container {
S3xs,
S2xs,
Xs,
Sm,
Md,
Lg,
Xl,
S2xl,
S3xl,
S4xl,
S5xl,
S6xl,
S7xl,
}
impl ToCss for Container {
fn to_css(&self) -> String {
match self {
Container::S3xs => "16rem".to_string(),
Container::S2xs => "18rem".to_string(),
Container::Xs => "20rem".to_string(),
Container::Sm => "24rem".to_string(),
Container::Md => "28rem".to_string(),
Container::Lg => "32rem".to_string(),
Container::Xl => "36rem".to_string(),
Container::S2xl => "42rem".to_string(),
Container::S3xl => "48rem".to_string(),
Container::S4xl => "56rem".to_string(),
Container::S5xl => "64rem".to_string(),
Container::S6xl => "72rem".to_string(),
Container::S7xl => "80rem".to_string(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_spacing_rem() {
assert_eq!(Spacing::S4.to_rem(), Some(1.0));
assert_eq!(Spacing::S8.to_rem(), Some(2.0));
assert_eq!(Spacing::S16.to_rem(), Some(4.0));
}
#[test]
fn test_spacing_px() {
assert_eq!(Spacing::S4.to_px(), Some(16));
assert_eq!(Spacing::Px.to_px(), Some(1));
}
#[test]
fn test_spacing_css() {
assert_eq!(Spacing::S4.to_css(), "1rem");
assert_eq!(Spacing::Px.to_css(), "1px");
assert_eq!(Spacing::Auto.to_css(), "auto");
}
}