refactor: various clippy-suggested improvements
most notably, change a lot fo match in main.rs to if-let
This commit is contained in:
parent
8676e8fd61
commit
2d6df9f737
7 changed files with 46 additions and 62 deletions
|
@ -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 /info - Get server information
|
||||||
GET /icons/[icon].svg - Get an icon in SVG format (equivalent to fetching them directly from Lucide)
|
GET /icons/[icon].svg - Get an icon in SVG format (equivalent to fetching them directly from Lucide)
|
||||||
|
|
|
@ -94,7 +94,7 @@ impl Handlers {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn match_handler<'a>(matchers: &'a HandlersMap, url: &URL) -> Option<&'a Arc<BoxedHandlerFn>> {
|
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
|
// If the URL has more segments than the path, it can't match
|
||||||
// or if the path has no segments, 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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ impl URL {
|
||||||
None => continue,
|
None => continue,
|
||||||
};
|
};
|
||||||
// Skip empty keys should they appear
|
// Skip empty keys should they appear
|
||||||
if key.len() == 0 {
|
if key.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -88,7 +88,7 @@ fn send_response(mut stream: TcpStream, status: u16, body: Body) -> UnitOrBoxedE
|
||||||
|
|
||||||
// Special handling for Bytes
|
// Special handling for Bytes
|
||||||
if let Body::Bytes(b, _) = &body {
|
if let Body::Bytes(b, _) = &body {
|
||||||
match stream.write(b) {
|
match stream.write_all(b) {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
return Err(Box::new(e));
|
return Err(Box::new(e));
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{env, ffi::OsString, fs};
|
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 const ICON_FILE_EXTENSION_LEN: usize = ICON_FILE_EXTENSION.len();
|
||||||
|
|
||||||
pub struct Icons {
|
pub struct Icons {
|
||||||
|
@ -42,11 +42,10 @@ impl Icons {
|
||||||
pub fn has_iconname(&self, icon: &str) -> bool {
|
pub fn has_iconname(&self, icon: &str) -> bool {
|
||||||
self.icon_filenames
|
self.icon_filenames
|
||||||
.iter()
|
.iter()
|
||||||
.find(|&i| match i.to_str() {
|
.any(|i| match i.to_str() {
|
||||||
Some(i) => &i[..i.len() - ICON_FILE_EXTENSION_LEN] == icon,
|
Some(i) => &i[..i.len() - ICON_FILE_EXTENSION_LEN] == icon,
|
||||||
None => false,
|
None => false,
|
||||||
})
|
})
|
||||||
.is_some()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the number of icons
|
/// Get the number of icons
|
||||||
|
|
47
src/main.rs
47
src/main.rs
|
@ -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,11 +108,10 @@ fn main() {
|
||||||
|
|
||||||
// Options handling
|
// Options handling
|
||||||
let mut scale = 1;
|
let mut scale = 1;
|
||||||
match req.url.query.get("scale") {
|
if let Some(scale_str) = req.url.query.get("scale") {
|
||||||
Some(scale_str) => {
|
|
||||||
match scale_str.parse::<u32>() {
|
match scale_str.parse::<u32>() {
|
||||||
Ok(scale_val) => {
|
Ok(scale_val) => {
|
||||||
if scale_val <= 0 || scale_val > 100 {
|
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"));
|
return Response::new(req, 400, Body::Static("Invalid scale value. Scale value must be an integer > 0 and <= 100"));
|
||||||
}
|
}
|
||||||
scale = scale_val;
|
scale = scale_val;
|
||||||
|
@ -120,38 +119,27 @@ fn main() {
|
||||||
Err(_) => 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"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut padding = 0;
|
let mut padding = 0;
|
||||||
match req.url.query.get("padding") {
|
if let Some(padding_str) = req.url.query.get("padding") {
|
||||||
Some(padding_str) => {
|
if let Ok(padding_val) = padding_str.parse::<u32>() {
|
||||||
match padding_str.parse::<u32>() {
|
|
||||||
Ok(padding_val) => {
|
|
||||||
if padding_val > 100 {
|
if padding_val > 100 {
|
||||||
return Response::new(req, 400, Body::Static("Invalid padding value. Padding value must be an integer >= 0 and <= 100"));
|
return Response::new(req, 400, Body::Static("Invalid padding value. Padding value must be an integer >= 0 and <= 100"));
|
||||||
}
|
}
|
||||||
|
|
||||||
padding = padding_val;
|
padding = padding_val;
|
||||||
|
} else {
|
||||||
|
return Response::new(req, 400, Body::Static("Invalid padding value. Padding value must be an integer >= 0 and <= 100"))
|
||||||
}
|
}
|
||||||
Err(_) => 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") {
|
if let Some(_) = req.url.query.get("discord_compatibility") {
|
||||||
Some(_) => {
|
|
||||||
padding = 8;
|
padding = 8;
|
||||||
}
|
}
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut background = Color::TRANSPARENT;
|
let mut background = Color::TRANSPARENT;
|
||||||
match req.url.query.get("background") {
|
if let Some(background_str) = req.url.query.get("background") {
|
||||||
Some(background_str) => {
|
if let Ok(background_val) = u32::from_str_radix(background_str, 16) {
|
||||||
match u32::from_str_radix(&background_str, 16) {
|
|
||||||
Ok(background_val) => {
|
|
||||||
if background_val > 0xFFFFFF {
|
if background_val > 0xFFFFFF {
|
||||||
return Response::new(req, 400, Body::Static("Invalid background value. Background value must be a valid hex color #000000 - #FFFFFF"));
|
return Response::new(req, 400, Body::Static("Invalid background value. Background value must be a valid hex color #000000 - #FFFFFF"));
|
||||||
}
|
}
|
||||||
|
@ -159,11 +147,9 @@ fn main() {
|
||||||
let green = ((background_val >> 8) & 0xFF) as u8;
|
let green = ((background_val >> 8) & 0xFF) as u8;
|
||||||
let blue = (background_val & 0xFF) as u8;
|
let blue = (background_val & 0xFF) as u8;
|
||||||
background = Color::from_rgba8(red, green, blue, 255);
|
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"))
|
||||||
}
|
}
|
||||||
Err(_) => return Response::new(req, 400, Body::Static("Invalid background value. Background value must be a valid hex color"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut svg_stroke = "currentColor";
|
let mut svg_stroke = "currentColor";
|
||||||
|
@ -173,19 +159,16 @@ fn main() {
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
icon_string = icon_string.replace("currentColor", &svg_stroke);
|
icon_string = icon_string.replace("currentColor", svg_stroke);
|
||||||
|
|
||||||
let mut backwards_compatibility = false;
|
let mut backwards_compatibility = false;
|
||||||
match req.url.query.get("backwards_compatibility") {
|
if let Some(_) = req.url.query.get("backwards_compatibility") {
|
||||||
Some(_) => {
|
|
||||||
backwards_compatibility = true;
|
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.
|
// 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;
|
scale = 3;
|
||||||
padding = 8;
|
padding = 8;
|
||||||
|
|
||||||
}
|
}
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rendering
|
// Rendering
|
||||||
let tree = match usvg::Tree::from_str(&icon_string, &usvg::Options::default()) {
|
let tree = match usvg::Tree::from_str(&icon_string, &usvg::Options::default()) {
|
||||||
|
@ -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"))
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -77,7 +77,7 @@ impl Worker {
|
||||||
fn build(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Option<Self> {
|
fn build(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Option<Self> {
|
||||||
let builder = thread::Builder::new().name(format!("worker-{}", id));
|
let builder = thread::Builder::new().name(format!("worker-{}", id));
|
||||||
|
|
||||||
let thread = match builder.spawn(move || {
|
let spawned = builder.spawn(move || {
|
||||||
println!("Worker {} spawned", id);
|
println!("Worker {} spawned", id);
|
||||||
loop {
|
loop {
|
||||||
let job = match receiver
|
let job = match receiver
|
||||||
|
@ -91,7 +91,9 @@ impl Worker {
|
||||||
|
|
||||||
job();
|
job();
|
||||||
}
|
}
|
||||||
}) {
|
});
|
||||||
|
|
||||||
|
let thread = match spawned {
|
||||||
Ok(thread) => Some(thread),
|
Ok(thread) => Some(thread),
|
||||||
Err(_) => return None,
|
Err(_) => return None,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue