diff --git a/src/size.rs b/src/size.rs index edab21d1a..090967837 100644 --- a/src/size.rs +++ b/src/size.rs @@ -352,21 +352,18 @@ impl FromStr for Size { type Err = ParseSizeError; fn from_str(src: &str) -> Result { - if src.len() < 2 { - return Err(ParseSizeError); - } - - let value = src[..src.len() - 2] - .parse::() - .map_err(|_| ParseSizeError)?; - - Ok(match &src[src.len() - 2..] { - "pt" => Size::pt(value), - "mm" => Size::mm(value), - "cm" => Size::cm(value), - "in" => Size::inches(value), + let func = match () { + _ if src.ends_with("pt") => Size::pt, + _ if src.ends_with("mm") => Size::mm, + _ if src.ends_with("cm") => Size::cm, + _ if src.ends_with("in") => Size::inches, _ => return Err(ParseSizeError), - }) + }; + + Ok(func(src[..src.len() - 2] + .parse::() + .map_err(|_| ParseSizeError)?)) + } } diff --git a/src/syntax/parsing.rs b/src/syntax/parsing.rs index 41a8728bb..dcba1d0f4 100644 --- a/src/syntax/parsing.rs +++ b/src/syntax/parsing.rs @@ -253,7 +253,7 @@ impl<'s> Parser<'s> { // This loop does not actually loop, but is used for breaking. loop { if text.ends_with('%') { - if let Ok(percent) = text[..text.len() - 1].parse::() { + if let Ok(percent) = text[.. text.len()-1].parse::() { break Expression::Num(percent / 100.0); } }