diff --git a/src/bot.rs b/src/bot.rs index 35c9902..8b0a61a 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -96,10 +96,7 @@ pub(crate) async fn run(data: Arc>) { { let mut write = data.write().await; - write.post_history = match SeriesHistory::load_history() { - Ok(data) => data, - Err(_) => continue, - }; + write.post_history = SeriesHistory::load_history(); } { @@ -243,14 +240,7 @@ async fn handle_series( series_history.set_part(post_part_info.as_string().as_str(), part_history); let mut write = data.write().await; write.post_history.set_series(series.slug.as_str(), series_history); - let _ = match write.post_history.save_history() { - Ok(data) => data, - Err(e) => { - let err_msg = format!("{e}"); - write_error(err_msg); - return Err(()) - } - }; + write.post_history.save_history(); } Ok(()) } diff --git a/src/post_history.rs b/src/post_history.rs index 7f8e1a3..364f444 100644 --- a/src/post_history.rs +++ b/src/post_history.rs @@ -1,69 +1,25 @@ use std::collections::HashMap; -use std::fs; -use std::fs::OpenOptions; -use std::io::Write; -use std::path::Path; use serde_derive::{Deserialize, Serialize}; use crate::write_error; -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, Default, Clone, Debug)] pub(crate) struct SeriesHistory { pub(crate) series: HashMap, } impl SeriesHistory { - pub(crate) fn load_history() -> Result { - let path = confy::get_configuration_file_path(env!("CARGO_PKG_NAME"), "config").expect("Something went wrong with confy"); - let config_dir = path.parent().expect("Something went wrong with confy"); - - - let path = format!("{}/history.toml", config_dir.to_str().expect("Conversion to str should not fail for a dir")); - match Path::new(path.as_str()).exists() { - true => { - let file_contents: String = match fs::read_to_string(path.as_str()) { - Ok(data) => data, - Err(e) => { - let err_msg = format!("{e}"); - write_error(err_msg); - return Err(()) - }, - }; - - let history: Result = match file_contents.len() { - 0 => return Ok(SeriesHistory { - series: HashMap::new(), - }), - _ => toml::from_str(file_contents.as_str()), - }; - - match history { - Ok(data) => Ok(data), - Err(e) => { - let err_msg = format!("{e}"); - write_error(err_msg); - Err(()) - } - } - }, - false => { - Ok(SeriesHistory { - series: HashMap::new(), - }) - } + pub(crate) fn load_history() -> Self { + match confy::load(env!("CARGO_PKG_NAME"), "history") { + Ok(data) => data, + Err(e) => panic!("history.toml not found: {e}"), } } - pub(crate) fn save_history(&self) -> std::io::Result { - let mut file = OpenOptions::new() - .read(true) - .write(true) - .create(true) - .open("history.toml") - .unwrap(); - - let json_data = toml::to_string_pretty(&self).unwrap(); - - file.write(json_data.as_bytes()) + pub(crate) fn save_history(&self) { + if let Err(e) = confy::store(env!("CARGO_PKG_NAME"), "history", self) { + let err_msg = format!("Unexpected error saving to history.toml: {e}"); + write_error(err_msg); + } } pub(crate) fn check_for_post(&self, series: &str, part: &str, title: &str) -> bool {