Skip to content

Commit

Permalink
Less indexing, less unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
sftse committed Dec 16, 2024
1 parent e9786ac commit 61113d8
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/css/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,13 @@ fn parse_faulty_color(
text: &str,
) -> Result<Colour, nom::Err<nom::error::Error<&'static str>>> {
let text = text.trim();
if text.chars().all(|c| c.is_hex_digit()) && text.len() == 6 {
let r = u8::from_str_radix(&text[0..2], 16).unwrap();
let g = u8::from_str_radix(&text[2..4], 16).unwrap();
let b = u8::from_str_radix(&text[4..6], 16).unwrap();
return Ok(Colour::Rgb(r, g, b));
if text.len() == 6 {
let r = u8::from_str_radix(&text[0..2], 16);
let g = u8::from_str_radix(&text[2..4], 16);
let b = u8::from_str_radix(&text[4..6], 16);
if let (Ok(r), Ok(g), Ok(b)) = (r, g, b) {
return Ok(Colour::Rgb(r, g, b));
}
}
Err(e)
}
Expand Down Expand Up @@ -618,7 +620,7 @@ fn parse_string_token(text: &str) -> IResult<&str, Token> {

loop {
match chars.next() {
None => return Ok((&text[text.len()..], Token::String(s.into()))),
None => return Ok(("", Token::String(s.into()))),
Some((i, c)) if c == end_char => {
return Ok((&text[i + 1..], Token::String(s.into())));
}
Expand Down

0 comments on commit 61113d8

Please sign in to comment.