refactor: various clippy-suggested improvements

most notably, change a lot fo match in main.rs to if-let
This commit is contained in:
Compositr 2024-11-06 17:00:54 +11:00
parent 8676e8fd61
commit 2d6df9f737
7 changed files with 46 additions and 62 deletions

View file

@ -1,4 +1,4 @@
pub const SERVER_INFO: &'static str = r#"luciders - Lucide SVG icons server <https://lucide.dev/>
pub const SERVER_INFO: &str = r#"luciders - Lucide SVG icons server <https://lucide.dev/>
GET /info - Get server information
GET /icons/[icon].svg - Get an icon in SVG format (equivalent to fetching them directly from Lucide)

View file

@ -94,7 +94,7 @@ impl Handlers {
};
}
return Ok(());
Ok(())
}
fn match_handler<'a>(matchers: &'a HandlersMap, url: &URL) -> Option<&'a Arc<BoxedHandlerFn>> {
@ -110,7 +110,7 @@ impl Handlers {
// If the URL has more segments than the path, it can't match
// or if the path has no segments, it can't match
if (url_segments.len() != path_segments.len()) || (path_segments.len() == 0) {
if (url_segments.len() != path_segments.len()) || (path_segments.is_empty()) {
continue;
}

View file

@ -25,7 +25,7 @@ impl URL {
None => continue,
};
// Skip empty keys should they appear
if key.len() == 0 {
if key.is_empty() {
continue;
}

View file

@ -88,7 +88,7 @@ fn send_response(mut stream: TcpStream, status: u16, body: Body) -> UnitOrBoxedE
// Special handling for Bytes
if let Body::Bytes(b, _) = &body {
match stream.write(b) {
match stream.write_all(b) {
Ok(_) => {}
Err(e) => {
return Err(Box::new(e));

View file

@ -1,6 +1,6 @@
use std::{env, ffi::OsString, fs};
const ICON_FILE_EXTENSION: &'static str = ".svg";
const ICON_FILE_EXTENSION: &str = ".svg";
pub const ICON_FILE_EXTENSION_LEN: usize = ICON_FILE_EXTENSION.len();
pub struct Icons {
@ -42,11 +42,10 @@ impl Icons {
pub fn has_iconname(&self, icon: &str) -> bool {
self.icon_filenames
.iter()
.find(|&i| match i.to_str() {
.any(|i| match i.to_str() {
Some(i) => &i[..i.len() - ICON_FILE_EXTENSION_LEN] == icon,
None => false,
})
.is_some()
}
/// Get the number of icons

View file

@ -76,7 +76,7 @@ fn main() {
}
};
return Response::new(req, 200, Body::Bytes(icon, "image/svg+xml"));
Response::new(req, 200, Body::Bytes(icon, "image/svg+xml"))
}
});
@ -108,62 +108,48 @@ fn main() {
// Options handling
let mut scale = 1;
match req.url.query.get("scale") {
Some(scale_str) => {
match scale_str.parse::<u32>() {
Ok(scale_val) => {
if scale_val <= 0 || scale_val > 100 {
return Response::new(req, 400, Body::Static("Invalid scale value. Scale value must be an integer > 0 and <= 100"));
}
scale = scale_val;
if let Some(scale_str) = req.url.query.get("scale") {
match scale_str.parse::<u32>() {
Ok(scale_val) => {
if scale_val == 0 || scale_val > 100 {
return Response::new(req, 400, Body::Static("Invalid scale value. Scale value must be an integer > 0 and <= 100"));
}
Err(_) => return Response::new(req, 400, Body::Static("Invalid scale value. Scale value must be an integer > 0 and <= 100"))
scale = scale_val;
}
Err(_) => return Response::new(req, 400, Body::Static("Invalid scale value. Scale value must be an integer > 0 and <= 100"))
}
None => {}
}
let mut padding = 0;
match req.url.query.get("padding") {
Some(padding_str) => {
match padding_str.parse::<u32>() {
Ok(padding_val) => {
if padding_val > 100 {
return Response::new(req, 400, Body::Static("Invalid padding value. Padding value must be an integer >= 0 and <= 100"));
}
padding = padding_val;
}
Err(_) => return Response::new(req, 400, Body::Static("Invalid padding value. Padding value must be an integer >= 0 and <= 100"))
if let Some(padding_str) = req.url.query.get("padding") {
if let Ok(padding_val) = padding_str.parse::<u32>() {
if padding_val > 100 {
return Response::new(req, 400, Body::Static("Invalid padding value. Padding value must be an integer >= 0 and <= 100"));
}
padding = padding_val;
} else {
return Response::new(req, 400, Body::Static("Invalid padding value. Padding value must be an integer >= 0 and <= 100"))
}
None => {}
}
match req.url.query.get("discord_compatibility") {
Some(_) => {
padding = 8;
}
None => {}
if let Some(_) = req.url.query.get("discord_compatibility") {
padding = 8;
}
let mut background = Color::TRANSPARENT;
match req.url.query.get("background") {
Some(background_str) => {
match u32::from_str_radix(&background_str, 16) {
Ok(background_val) => {
if background_val > 0xFFFFFF {
return Response::new(req, 400, Body::Static("Invalid background value. Background value must be a valid hex color #000000 - #FFFFFF"));
}
let red = ((background_val >> 16) & 0xFF) as u8;
let green = ((background_val >> 8) & 0xFF) as u8;
let blue = (background_val & 0xFF) as u8;
background = Color::from_rgba8(red, green, blue, 255);
}
Err(_) => return Response::new(req, 400, Body::Static("Invalid background value. Background value must be a valid hex color"))
if let Some(background_str) = req.url.query.get("background") {
if let Ok(background_val) = u32::from_str_radix(background_str, 16) {
if background_val > 0xFFFFFF {
return Response::new(req, 400, Body::Static("Invalid background value. Background value must be a valid hex color #000000 - #FFFFFF"));
}
let red = ((background_val >> 16) & 0xFF) as u8;
let green = ((background_val >> 8) & 0xFF) as u8;
let blue = (background_val & 0xFF) as u8;
background = Color::from_rgba8(red, green, blue, 255);
} else {
return Response::new(req, 400, Body::Static("Invalid background value. Background value must be a valid hex color"))
}
None => {}
}
let mut svg_stroke = "currentColor";
@ -173,18 +159,15 @@ fn main() {
_ => {}
}
icon_string = icon_string.replace("currentColor", &svg_stroke);
icon_string = icon_string.replace("currentColor", svg_stroke);
let mut backwards_compatibility = false;
match req.url.query.get("backwards_compatibility") {
Some(_) => {
backwards_compatibility = true;
// Icons were 80x80 plus 8px of padding; Icons start at 24x24 now so: 80 - 8 = 72, 72 / 24 = 3 therefore scale is 3x.
scale = 3;
padding = 8;
if let Some(_) = req.url.query.get("backwards_compatibility") {
backwards_compatibility = true;
// Icons were 80x80 plus 8px of padding; Icons start at 24x24 now so: 80 - 8 = 72, 72 / 24 = 3 therefore scale is 3x.
scale = 3;
padding = 8;
}
None => {}
}
// Rendering
@ -224,7 +207,7 @@ fn main() {
},
};
return Response::new(req, 200, Body::Bytes(png, "image/png"));
Response::new(req, 200, Body::Bytes(png, "image/png"))
}
});

View file

@ -77,7 +77,7 @@ impl Worker {
fn build(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Option<Self> {
let builder = thread::Builder::new().name(format!("worker-{}", id));
let thread = match builder.spawn(move || {
let spawned = builder.spawn(move || {
println!("Worker {} spawned", id);
loop {
let job = match receiver
@ -91,7 +91,9 @@ impl Worker {
job();
}
}) {
});
let thread = match spawned {
Ok(thread) => Some(thread),
Err(_) => return None,
};