From 9d341e9e7c90b0a400b27f1faa825a1c8dcc5491 Mon Sep 17 00:00:00 2001 From: Tim Biermann Date: Fri, 5 Jan 2024 19:32:40 +0100 Subject: [PATCH] updated dependencies, optimized regex, better error handling --- Cargo.lock | 9 +++++---- Cargo.toml | 1 + src/main.rs | 36 ++++++++++++++++++++++++------------ 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 856e9e5..221a0c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -92,9 +92,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.149" +version = "0.2.151" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" +checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" [[package]] name = "libnotify" @@ -126,9 +126,9 @@ dependencies = [ [[package]] name = "pkg-config" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" +checksum = "69d3587f8a9e599cc7ec2c00e331f71c4e69a5f9a4b8a6efd5b07466b9736f9a" [[package]] name = "regex" @@ -159,6 +159,7 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" name = "scun" version = "0.1.0" dependencies = [ + "lazy_static", "libnotify", "regex", ] diff --git a/Cargo.toml b/Cargo.toml index d5d42b4..9cfe4cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Tim Biermann "] edition = "2021" [dependencies] +lazy_static = "1.4.0" libnotify = "1.0.3" regex = { version = "1.10.2", default-features = false } diff --git a/src/main.rs b/src/main.rs index 1791a15..6b6fa12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use lazy_static::lazy_static; use regex::Regex; use std::env; use std::error::Error; @@ -16,7 +17,7 @@ fn find_ports_in_repositories(package_name: &str) -> Result Result Option { let pkgfile_path = format!("{port_dir}/Pkgfile"); if !Path::new(&pkgfile_path).exists() { @@ -42,17 +48,15 @@ fn extract_pkgfile_version(port_dir: &str) -> Option { } let pkgfile_content = std::fs::read_to_string(pkgfile_path).ok()?; - let version_regex = Regex::new(r#"version=(.+)"#).ok()?; - let release_regex = Regex::new(r#"release=(.+)"#).ok()?; - let version = match version_regex.captures(&pkgfile_content) { + let version = match VERSION_REGEX.captures(&pkgfile_content) { Some(captures) => captures.get(1).map(|m| m.as_str()).unwrap_or(""), None => { eprintln!("Failed to extract version from Pkgfile for: {port_dir}"); "" } }; - let release = release_regex + let release = RELEASE_REGEX .captures(&pkgfile_content)? .get(1) .map(|m| m.as_str()) @@ -82,7 +86,9 @@ fn list_installed_packages(filename: &str) -> Result, Box Result, Box) -> Result<(), Box> { - libnotify::init("scun").unwrap(); + if let Err(e) = libnotify::init("scun") { + eprintln!("Failed to initialize libnotify: {}", e); + return Err(e.into()); + } let notification_body: String = output.join("\n"); let n = libnotify::Notification::new("Port Updates", ¬ification_body as &str, None); n.set_timeout(5000); - n.show().unwrap(); + if let Err(e) = n.show() { + eprintln!("Failed to show notification: {}", e); + return Err(e.into()); + } libnotify::uninit(); Ok(()) @@ -157,11 +169,11 @@ fn main() -> Result<(), Box> { let args: Vec = env::args().collect(); if args.len() < 2 { - let message = "Usage: scun [notify|print] + let message = r#"Usage: scun [notify|print] notify: send a list via libnotify print: prints the number of available updates --icon|-i: adds an icon - --long|-l: prints the whole list"; + --long|-l: prints the whole list"#; eprintln!("{message}"); process::exit(1); } @@ -173,9 +185,9 @@ print: prints the number of available updates _ => Option::None, }; - if mode == "notify" { + if mode == "notify" || mode == "n" { notify_mode(output)?; - } else if mode == "print" { + } else if mode == "print" || mode == "p" { print_mode(output, submode)?; } else { eprintln!("Invalid mode: {mode}. Use 'notify' or 'print'.");