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.
This commit is contained in:
Fries 2023-07-03 22:41:32 -07:00
parent 93bd3540e8
commit 33f0b6a5e9
5 changed files with 21 additions and 16 deletions

View file

@ -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> {

View file

@ -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

View file

@ -24,5 +24,5 @@ pub(super) fn site_string(site: &Site, options: PrintOptions, separator: &String
}
}
return string;
string
}

View file

@ -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<Site> {
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<Mutex<Vec<Site>>> = 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<Site> {
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<Vec<Site>, Error> {
let names_path = directories::get_names_path()?;
let names_file = names::read_names_file(&names_path)?;
names::load_names(names_file)
}

View file

@ -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<notify::Event>) {
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),