From 61ab4ba38df9766a0970d1cc527d49e3a054aa26 Mon Sep 17 00:00:00 2001 From: Compositr Date: Mon, 4 Nov 2024 22:13:26 +1100 Subject: [PATCH] perf: extract error fn to a macro --- src/macros.rs | 6 ++++++ src/main.rs | 15 ++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) create mode 100644 src/macros.rs diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..5b00572 --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,6 @@ +#[macro_export] +macro_rules! error { + ($msg:expr) => { + eprintln!("[ERROR] {}", $msg); + }; +} diff --git a/src/main.rs b/src/main.rs index 5a5f397..f0a116e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ mod handlers; mod http; mod icons; mod threads; +mod macros; fn main() { println!("luciders starting..."); @@ -68,7 +69,7 @@ fn main() { let icon = match icons.get_icon(icon_name) { Some(icon) => icon, None => { - error("Failed to read icon"); + error!("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) { Some(icon) => icon, None => { - error("Failed to read icon"); + error!("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()) { Ok(tree) => tree, 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")) } }; @@ -199,7 +200,7 @@ fn main() { let mut pixmap = match Pixmap::new(length, length) { Some(pixmap) => pixmap, None => { - error("Failed to create pixmap"); + error!("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() { Ok(png) => png, 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")) }, }; @@ -237,7 +238,3 @@ fn fatal(msg: &'static str) -> ! { eprintln!("[FATAL] {}", msg); process::exit(1); } - -fn error(msg: &str) { - eprintln!("[ERROR] {}", msg); -}