From 33f0b6a5e93558d27352510134a71ad2b919926e Mon Sep 17 00:00:00 2001 From: Fries Date: Mon, 3 Jul 2023 22:41:32 -0700 Subject: [PATCH] don't panic on parsing error with running watcher this means the watcher doesn't stop working if someone makes an edit to names.json that results in a parser error. it will just print out the error and keep the old configuration. if you restart the program and the file still has an error, the program won't start. --- cli/src/commands/print.rs | 6 +++--- cli/src/commands/remove.rs | 2 +- cli/src/commands/utils.rs | 2 +- src/sites.rs | 17 ++++++++++------- src/watcher.rs | 10 ++++++---- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/cli/src/commands/print.rs b/cli/src/commands/print.rs index ca60815..bb67e65 100644 --- a/cli/src/commands/print.rs +++ b/cli/src/commands/print.rs @@ -19,7 +19,7 @@ pub(crate) fn print( if let Some(filter) = filter { names.retain(|f| &f.url == filter); - if names.len() == 0 { + if names.is_empty() { return Err(Error { status: ErrorStatus::NotFoundError, data: "this url was not found in names.json".into(), @@ -50,8 +50,8 @@ fn filter_site( json_printing(site)?; return Ok(()); } - - return Ok(printing(separator, site, group)); + printing(separator, site, group); + Ok(()) } fn json_printing(site: &Site) -> Result<(), Error> { diff --git a/cli/src/commands/remove.rs b/cli/src/commands/remove.rs index 603d94f..ebf68b7 100644 --- a/cli/src/commands/remove.rs +++ b/cli/src/commands/remove.rs @@ -11,7 +11,7 @@ pub(crate) fn remove(path: &Path, url: &String, separator: &String) -> Result<() if &site.url == url { println!( "removing {} from names.json", - site_string(&site, PrintOptions::All, separator) + site_string(site, PrintOptions::All, separator) ); } &site.url != url diff --git a/cli/src/commands/utils.rs b/cli/src/commands/utils.rs index 61ab040..f772cf0 100644 --- a/cli/src/commands/utils.rs +++ b/cli/src/commands/utils.rs @@ -24,5 +24,5 @@ pub(super) fn site_string(site: &Site, options: PrintOptions, separator: &String } } - return string; + string } diff --git a/src/sites.rs b/src/sites.rs index 94b6b04..ead801c 100644 --- a/src/sites.rs +++ b/src/sites.rs @@ -4,14 +4,17 @@ use shared::{ errors::{Error, ErrorStatus}, names::{self, Site}, }; -use std::sync::{OnceLock}; +use std::sync::OnceLock; pub async fn get_global_names() -> Vec { NAMES.get().unwrap().lock().await.clone() } pub fn set_names() { - *NAMES.get().unwrap().blocking_lock() = get_names(); + match get_names() { + Ok(names) => *NAMES.get().unwrap().blocking_lock() = names, + Err(err) => println!("{:?}", err), + } } static NAMES: OnceLock>> = OnceLock::new(); @@ -21,7 +24,7 @@ pub fn init_names() -> Result<(), Error> { "names.json path: {}", directories::get_names_path().unwrap().display() ); - match NAMES.set(Mutex::new(get_names())) { + match NAMES.set(Mutex::new(get_names().unwrap())) { Ok(_) => Ok(()), Err(_) => Err(Error { status: ErrorStatus::GenericError, @@ -30,8 +33,8 @@ pub fn init_names() -> Result<(), Error> { } } -fn get_names() -> Vec { - let names_path = directories::get_names_path().unwrap(); - let names_file = names::read_names_file(&names_path).unwrap(); - names::load_names(names_file).unwrap() +fn get_names() -> Result, Error> { + let names_path = directories::get_names_path()?; + let names_file = names::read_names_file(&names_path)?; + names::load_names(names_file) } diff --git a/src/watcher.rs b/src/watcher.rs index 34d589b..d444c76 100644 --- a/src/watcher.rs +++ b/src/watcher.rs @@ -1,9 +1,11 @@ -use notify::{Result, Watcher}; +use notify::{ + event::{DataChange, ModifyKind}, + EventKind, Result, Watcher, +}; use shared::directories; use crate::sites; - pub(crate) fn hot_reloading() { let (tx, rx) = std::sync::mpsc::channel(); let names_path = directories::get_names_project_path().unwrap(); @@ -21,8 +23,8 @@ pub(crate) fn hot_reloading() { fn watch(res: Result) { match res { Ok(event) => { - if event.kind.is_modify() { - sites::set_names(); + if event.kind == EventKind::Modify(ModifyKind::Data(DataChange::Any)) { + sites::set_names(); } } Err(err) => println!("Error: {}", err),