chore(cli): apply clippy fixes

This commit is contained in:
Amaan Qureshi 2024-02-04 01:30:33 -05:00
parent 1fb16a72ac
commit 04ff704bca
No known key found for this signature in database
GPG key ID: E67890ADC4227273
49 changed files with 1094 additions and 1277 deletions

View file

@ -12,7 +12,7 @@ use std::{fs, io, path, str, usize};
use tree_sitter_highlight::{HighlightConfiguration, HighlightEvent, Highlighter, HtmlRenderer};
use tree_sitter_loader::Loader;
pub const HTML_HEADER: &'static str = "
pub const HTML_HEADER: &str = "
<!doctype HTML>
<head>
<title>Tree-sitter Highlighting</title>
@ -34,7 +34,7 @@ pub const HTML_HEADER: &'static str = "
<body>
";
pub const HTML_FOOTER: &'static str = "
pub const HTML_FOOTER: &str = "
</body>
";
@ -67,13 +67,14 @@ impl Theme {
Ok(serde_json::from_str(&json).unwrap_or_default())
}
#[must_use]
pub fn default_style(&self) -> Style {
Style::default()
}
}
impl<'de> Deserialize<'de> for Theme {
fn deserialize<D>(deserializer: D) -> std::result::Result<Theme, D::Error>
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
@ -181,17 +182,17 @@ fn parse_style(style: &mut Style, json: Value) {
match property_name.as_str() {
"bold" => {
if value == Value::Bool(true) {
style.ansi = style.ansi.bold()
style.ansi = style.ansi.bold();
}
}
"italic" => {
if value == Value::Bool(true) {
style.ansi = style.ansi.italic()
style.ansi = style.ansi.italic();
}
}
"underline" => {
if value == Value::Bool(true) {
style.ansi = style.ansi.underline()
style.ansi = style.ansi.underline();
}
}
"color" => {
@ -219,10 +220,7 @@ fn parse_style(style: &mut Style, json: Value) {
fn parse_color(json: Value) -> Option<Color> {
match json {
Value::Number(n) => match n.as_u64() {
Some(n) => Some(Color::Fixed(n as u8)),
_ => None,
},
Value::Number(n) => n.as_u64().map(|n| Color::Fixed(n as u8)),
Value::String(s) => match s.to_lowercase().as_str() {
"black" => Some(Color::Black),
"blue" => Some(Color::Blue),
@ -233,7 +231,7 @@ fn parse_color(json: Value) -> Option<Color> {
"white" => Some(Color::White),
"yellow" => Some(Color::Yellow),
s => {
if let Some((red, green, blue)) = hex_string_to_rgb(&s) {
if let Some((red, green, blue)) = hex_string_to_rgb(s) {
Some(Color::RGB(red, green, blue))
} else {
None
@ -245,7 +243,7 @@ fn parse_color(json: Value) -> Option<Color> {
}
fn hex_string_to_rgb(s: &str) -> Option<(u8, u8, u8)> {
if s.starts_with("#") && s.len() >= 7 {
if s.starts_with('#') && s.len() >= 7 {
if let (Ok(red), Ok(green), Ok(blue)) = (
u8::from_str_radix(&s[1..3], 16),
u8::from_str_radix(&s[3..5], 16),
@ -280,7 +278,7 @@ fn style_to_css(style: ansi_term::Style) -> String {
fn write_color(buffer: &mut String, color: Color) {
if let Color::RGB(r, g, b) = &color {
write!(buffer, "color: #{r:02x}{g:02x}{b:02x}").unwrap()
write!(buffer, "color: #{r:02x}{g:02x}{b:02x}").unwrap();
} else {
write!(
buffer,
@ -298,18 +296,14 @@ fn write_color(buffer: &mut String, color: Color) {
Color::RGB(_, _, _) => unreachable!(),
}
)
.unwrap()
.unwrap();
}
}
fn terminal_supports_truecolor() -> bool {
use std::env;
if let Ok(truecolor) = env::var("COLORTERM") {
std::env::var("COLORTERM").map_or(false, |truecolor| {
truecolor == "truecolor" || truecolor == "24bit"
} else {
false
}
})
}
fn closest_xterm_color(red: u8, green: u8, blue: u8) -> Color {
@ -399,25 +393,23 @@ pub fn html(
let mut renderer = HtmlRenderer::new();
renderer.render(events, source, &move |highlight| {
if let Some(css_style) = &theme.styles[highlight.0].css {
css_style.as_bytes()
} else {
"".as_bytes()
}
theme.styles[highlight.0]
.css
.as_ref()
.map_or_else(|| "".as_bytes(), |css_style| css_style.as_bytes())
})?;
if !quiet {
write!(&mut stdout, "<table>\n")?;
writeln!(&mut stdout, "<table>")?;
for (i, line) in renderer.lines().enumerate() {
write!(
writeln!(
&mut stdout,
"<tr><td class=line-number>{}</td><td class=line>{}</td></tr>\n",
"<tr><td class=line-number>{}</td><td class=line>{line}</td></tr>",
i + 1,
line
)?;
}
write!(&mut stdout, "</table>\n")?;
writeln!(&mut stdout, "</table>")?;
}
if print_time {
@ -432,8 +424,8 @@ mod tests {
use super::*;
use std::env;
const JUNGLE_GREEN: &'static str = "#26A69A";
const DARK_CYAN: &'static str = "#00AF87";
const JUNGLE_GREEN: &str = "#26A69A";
const DARK_CYAN: &str = "#00AF87";
#[test]
fn test_parse_style() {