use std::{fmt::Display, str::FromStr};
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub enum Unit {
Em,
Ex,
Px,
In,
Cm,
Mm,
Pt,
Pc,
Percentages,
}
impl FromStr for Unit {
type Err = crate::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"em" => Ok(Unit::Em),
"ex" => Ok(Unit::Ex),
"px" => Ok(Unit::Px),
"in" => Ok(Unit::In),
"cm" => Ok(Unit::Cm),
"mm" => Ok(Unit::Mm),
"pt" => Ok(Unit::Pt),
"pc" => Ok(Unit::Pc),
"%" => Ok(Unit::Percentages),
_ => Err(crate::Error::LengthStr(s.to_owned())),
}
}
}
impl Display for Unit {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_ref())
}
}
impl AsRef<str> for Unit {
fn as_ref(&self) -> &str {
match self {
Unit::Em => "em",
Unit::Ex => "ex",
Unit::Px => "px",
Unit::In => "in",
Unit::Cm => "cm",
Unit::Mm => "mm",
Unit::Pt => "pt",
Unit::Pc => "pc",
Unit::Percentages => "%",
}
}
}