use directories::ProjectDirs; use domasi::Alert; use domasi::Pomodoro; use std::fs; use std::path::PathBuf; fn main() { let mut args = std::env::args_os(); if let Some(os_string) = args.nth(1) { match os_string.to_str() { Some("-C") | Some("--create-config") => { let _ = create_alert_directory(); } None => {} arg => println!("Unsupported Argument: {}", arg.unwrap()), } } let mut domasi: Pomodoro = Default::default(); match get_alert() { Some(alert) => domasi.start_with_alert(alert), None => domasi.start(), } } fn create_alert_directory() -> std::io::Result<()> { let alert_dir = ProjectDirs::from("moe", "paoda", "domasi") .unwrap() .data_dir() .to_path_buf() .join("alert"); fs::create_dir_all(alert_dir)?; Ok(()) } fn get_alert() -> Option { let alert_dir = ProjectDirs::from("moe", "paoda", "domasi") .unwrap() .data_dir() .to_path_buf() .join("alert"); let file = find_alert_file(alert_dir)?; Some(Alert::new(file)) } fn find_alert_file(path: PathBuf) -> Option { let items = fs::read_dir(path).ok()?; for entry_result in items { if let Ok(entry) = entry_result { if let Some(ext) = entry.path().extension() { if let Some("mp3") | Some("wav") | Some("ogg") | Some("flac") = ext.to_str() { return Some(entry.path()); } } } } None }