Compare commits

...

2 Commits

Author SHA1 Message Date
Josh McKinney
258005a41a fix: cargo lint sorting and color parsing 2024-06-17 05:46:17 -07:00
Josh McKinney
9692366d12 chore: add clippy::string_slice
Fixes: https://github.com/ratatui-org/ratatui/issues/1056
2024-06-17 05:41:28 -07:00
2 changed files with 18 additions and 12 deletions

View File

@@ -313,8 +313,20 @@ impl FromStr for Color {
_ => {
if let Ok(index) = s.parse::<u8>() {
Self::Indexed(index)
} else if let Some((r, g, b)) = parse_hex_color(s) {
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);
}
@@ -324,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 {

View File

@@ -1,3 +1,7 @@
// The code that works on text pays attention to unicode in this file by working in graphemes
// instead of byte indexes. This is important for languages that use double-width characters.
#![allow(clippy::string_slice)]
use std::{collections::VecDeque, vec::IntoIter};
use unicode_segmentation::UnicodeSegmentation;