Skip to content

Commit

Permalink
feat: optimize main function; proper error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
NotAShelf committed Jul 11, 2024
1 parent 73282e5 commit b380a78
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ fn main() {
env_logger::init();

// start tray service
start_tray_service();
match start_tray_service() {
Ok(_) => println!("Tray service started successfully."),
Err(e) => eprintln!("Failed to start the tray service: {}", e),
}

// keep the main thread alive
loop {
park();
}
park();
}
20 changes: 11 additions & 9 deletions src/tray/utils.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use crate::tailscale;
use crate::tray::menu::SysTray;
use std::error::Error;

pub fn start_tray_service() {
// start the tray service
let _handle = ksni::spawn(SysTray {
ctx: tailscale::status::get_current_status()
.expect("Failed to update Tailscale status! Is Tailscale daemon running?"),
})
.unwrap_or_else(|e| {
panic!("Failed to start the tray service: {}", e);
});
type TrayServiceError = Box<dyn Error>;

pub fn start_tray_service() -> Result<(), TrayServiceError> {
let status = tailscale::status::get_current_status()
.map_err(|e| format!("Failed to update Tailscale status: {}", e))?;

let _handle = ksni::spawn(SysTray { ctx: status })
.map_err(|e| format!("Failed to spawn Tray implementation: {}", e))?;

Ok(())
}

0 comments on commit b380a78

Please sign in to comment.