perf: extract error fn to a macro

This commit is contained in:
Compositr 2024-11-04 22:13:26 +11:00
parent a3383084fb
commit 61ab4ba38d
2 changed files with 12 additions and 9 deletions

6
src/macros.rs Normal file
View file

@ -0,0 +1,6 @@
#[macro_export]
macro_rules! error {
($msg:expr) => {
eprintln!("[ERROR] {}", $msg);
};
}

View file

@ -12,6 +12,7 @@ mod handlers;
mod http; mod http;
mod icons; mod icons;
mod threads; mod threads;
mod macros;
fn main() { fn main() {
println!("luciders starting..."); println!("luciders starting...");
@ -68,7 +69,7 @@ fn main() {
let icon = match icons.get_icon(icon_name) { let icon = match icons.get_icon(icon_name) {
Some(icon) => icon, Some(icon) => icon,
None => { None => {
error("Failed to read icon"); error!("Failed to read icon");
return Response::new(req, 500, Body::Static("Failed to read icon")); return Response::new(req, 500, Body::Static("Failed to read icon"));
} }
}; };
@ -98,7 +99,7 @@ fn main() {
let mut icon_string = match icons.get_icon_string(icon_name) { let mut icon_string = match icons.get_icon_string(icon_name) {
Some(icon) => icon, Some(icon) => icon,
None => { None => {
error("Failed to read icon"); error!("Failed to read icon");
return Response::new(req, 500, Body::Static("Failed to read icon")) return Response::new(req, 500, Body::Static("Failed to read icon"))
}, },
}; };
@ -188,7 +189,7 @@ fn main() {
let tree = match usvg::Tree::from_str(&icon_string, &usvg::Options::default()) { let tree = match usvg::Tree::from_str(&icon_string, &usvg::Options::default()) {
Ok(tree) => tree, Ok(tree) => tree,
Err(e) => { Err(e) => {
error(&format!("Failed to load icon into tree: {}", e)); error!(&format!("Failed to load icon into tree: {}", e));
return Response::new(req, 500, Body::Static("Failed to load icon into tree")) return Response::new(req, 500, Body::Static("Failed to load icon into tree"))
} }
}; };
@ -199,7 +200,7 @@ fn main() {
let mut pixmap = match Pixmap::new(length, length) { let mut pixmap = match Pixmap::new(length, length) {
Some(pixmap) => pixmap, Some(pixmap) => pixmap,
None => { None => {
error("Failed to create pixmap"); error!("Failed to create pixmap");
return Response::new(req, 500, Body::Static("Failed to create pixmap")) return Response::new(req, 500, Body::Static("Failed to create pixmap"))
}, },
}; };
@ -216,7 +217,7 @@ fn main() {
let png = match pixmap.encode_png() { let png = match pixmap.encode_png() {
Ok(png) => png, Ok(png) => png,
Err(e) => { Err(e) => {
error(&format!("Failed to encode PNG: {}", e)); error!(&format!("Failed to encode PNG: {}", e));
return Response::new(req, 500, Body::Static("Failed to encode PNG")) return Response::new(req, 500, Body::Static("Failed to encode PNG"))
}, },
}; };
@ -237,7 +238,3 @@ fn fatal(msg: &'static str) -> ! {
eprintln!("[FATAL] {}", msg); eprintln!("[FATAL] {}", msg);
process::exit(1); process::exit(1);
} }
fn error(msg: &str) {
eprintln!("[ERROR] {}", msg);
}