simplify Unit::From<String>

This commit is contained in:
ConnorSkees 2020-05-06 12:01:40 -04:00
parent 01858ff543
commit 1b774a9b91
2 changed files with 41 additions and 41 deletions

View File

@ -155,45 +155,45 @@ impl Unit {
}
}
impl From<&String> for Unit {
fn from(unit: &String) -> Self {
match unit.to_ascii_lowercase().as_bytes() {
b"px" => Unit::Px,
b"mm" => Unit::Mm,
b"in" => Unit::In,
b"cm" => Unit::Cm,
b"q" => Unit::Q,
b"pt" => Unit::Pt,
b"pc" => Unit::Pc,
b"em" => Unit::Em,
b"rem" => Unit::Rem,
b"lh" => Unit::Lh,
b"%" => Unit::Percent,
b"ex" => Unit::Ex,
b"ch" => Unit::Ch,
b"cap" => Unit::Cap,
b"ic" => Unit::Ic,
b"rlh" => Unit::Rlh,
b"vw" => Unit::Vw,
b"vh" => Unit::Vh,
b"vmin" => Unit::Vmin,
b"vmax" => Unit::Vmax,
b"vi" => Unit::Vi,
b"vb" => Unit::Vb,
b"deg" => Unit::Deg,
b"grad" => Unit::Grad,
b"rad" => Unit::Rad,
b"turn" => Unit::Turn,
b"s" => Unit::S,
b"ms" => Unit::Ms,
b"hz" => Unit::Hz,
b"khz" => Unit::Khz,
b"dpi" => Unit::Dpi,
b"dpcm" => Unit::Dpcm,
b"dppx" => Unit::Dppx,
b"x" => Unit::X,
b"fr" => Unit::Fr,
_ => Unit::Unknown(String::from(unit)),
impl From<String> for Unit {
fn from(unit: String) -> Self {
match unit.to_ascii_lowercase().as_str() {
"px" => Unit::Px,
"mm" => Unit::Mm,
"in" => Unit::In,
"cm" => Unit::Cm,
"q" => Unit::Q,
"pt" => Unit::Pt,
"pc" => Unit::Pc,
"em" => Unit::Em,
"rem" => Unit::Rem,
"lh" => Unit::Lh,
"%" => Unit::Percent,
"ex" => Unit::Ex,
"ch" => Unit::Ch,
"cap" => Unit::Cap,
"ic" => Unit::Ic,
"rlh" => Unit::Rlh,
"vw" => Unit::Vw,
"vh" => Unit::Vh,
"vmin" => Unit::Vmin,
"vmax" => Unit::Vmax,
"vi" => Unit::Vi,
"vb" => Unit::Vb,
"deg" => Unit::Deg,
"grad" => Unit::Grad,
"rad" => Unit::Rad,
"turn" => Unit::Turn,
"s" => Unit::S,
"ms" => Unit::Ms,
"hz" => Unit::Hz,
"khz" => Unit::Khz,
"dpi" => Unit::Dpi,
"dpcm" => Unit::Dpcm,
"dppx" => Unit::Dppx,
"x" => Unit::X,
"fr" => Unit::Fr,
_ => Unit::Unknown(unit),
}
}
}
@ -237,7 +237,7 @@ impl fmt::Display for Unit {
Unit::X => write!(f, "x"),
Unit::Fr => write!(f, "fr"),
Unit::Unknown(s) => write!(f, "{}", s),
Unit::None => write!(f, ""),
Unit::None => Ok(()),
Unit::Mul(u) => write!(
f,
"{}",

View File

@ -651,7 +651,7 @@ impl Value {
Err(e) => return Some(Err(e)),
};
span = span.merge(u.span);
Unit::from(&u.node)
Unit::from(u.node)
}
'%' => {
span = span.merge(toks.next().unwrap().pos());