fix: cargo lint sorting and color parsing

This commit is contained in:
Josh McKinney 2024-04-26 00:36:32 -07:00
parent 9692366d12
commit 258005a41a
No known key found for this signature in database
GPG key ID: 722287396A903BC5
2 changed files with 14 additions and 22 deletions

View file

@ -99,7 +99,6 @@ string_slice = "warn"
string_to_string = "warn"
unnecessary_self_imports = "warn"
use_self = "warn"
string_slice = "warn"
[features]
#! The crate provides a set of optional features that can be enabled in your `cargo.toml` file.

View file

@ -313,17 +313,20 @@ impl FromStr for Color {
_ => {
if let Ok(index) = s.parse::<u8>() {
Self::Indexed(index)
} else if let (Ok(r), Ok(g), Ok(b)) = {
if !s.starts_with('#') || s.len() != 7 {
return Err(ParseColorError);
}
(
u8::from_str_radix(&s.chars().skip(1).take(2).collect::<String>(), 16),
u8::from_str_radix(&s.chars().skip(3).take(2).collect::<String>(), 16),
u8::from_str_radix(&s.chars().skip(5).take(2).collect::<String>(), 16),
)
} {
Self::Rgb(r, g, b)
} else if s.starts_with('#') && s.len() == 7 {
let red = s
.get(1..3)
.and_then(|v| u8::from_str_radix(v, 16).ok())
.ok_or(ParseColorError)?;
let green = s
.get(3..5)
.and_then(|v| u8::from_str_radix(v, 16).ok())
.ok_or(ParseColorError)?;
let blue = s
.get(5..7)
.and_then(|v| u8::from_str_radix(v, 16).ok())
.ok_or(ParseColorError)?;
Self::Rgb(red, green, blue)
} else {
return Err(ParseColorError);
}
@ -333,16 +336,6 @@ impl FromStr for Color {
}
}
fn parse_hex_color(input: &str) -> Option<(u8, u8, u8)> {
if !input.starts_with('#') || input.len() != 7 {
return None;
}
let r = u8::from_str_radix(input.get(1..3)?, 16).ok()?;
let g = u8::from_str_radix(input.get(3..5)?, 16).ok()?;
let b = u8::from_str_radix(input.get(5..7)?, 16).ok()?;
Some((r, g, b))
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {