Utilize confy for reading and saving history

This commit is contained in:
Neshura 2023-12-30 01:02:20 +01:00
parent f08e60a89d
commit cbb50e5d53
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
2 changed files with 12 additions and 66 deletions

View file

@ -96,10 +96,7 @@ pub(crate) async fn run(data: Arc<RwLock<SharedData>>) {
{ {
let mut write = data.write().await; let mut write = data.write().await;
write.post_history = match SeriesHistory::load_history() { write.post_history = SeriesHistory::load_history();
Ok(data) => data,
Err(_) => continue,
};
} }
{ {
@ -243,14 +240,7 @@ async fn handle_series(
series_history.set_part(post_part_info.as_string().as_str(), part_history); series_history.set_part(post_part_info.as_string().as_str(), part_history);
let mut write = data.write().await; let mut write = data.write().await;
write.post_history.set_series(series.slug.as_str(), series_history); write.post_history.set_series(series.slug.as_str(), series_history);
let _ = match write.post_history.save_history() { write.post_history.save_history();
Ok(data) => data,
Err(e) => {
let err_msg = format!("{e}");
write_error(err_msg);
return Err(())
}
};
} }
Ok(()) Ok(())
} }

View file

@ -1,69 +1,25 @@
use std::collections::HashMap; 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 serde_derive::{Deserialize, Serialize};
use crate::write_error; use crate::write_error;
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Serialize, Deserialize, Default, Clone, Debug)]
pub(crate) struct SeriesHistory { pub(crate) struct SeriesHistory {
pub(crate) series: HashMap<String, PostHistory>, pub(crate) series: HashMap<String, PostHistory>,
} }
impl SeriesHistory { impl SeriesHistory {
pub(crate) fn load_history() -> Result<Self, ()> { pub(crate) fn load_history() -> Self {
let path = confy::get_configuration_file_path(env!("CARGO_PKG_NAME"), "config").expect("Something went wrong with confy"); match confy::load(env!("CARGO_PKG_NAME"), "history") {
let config_dir = path.parent().expect("Something went wrong with confy"); Ok(data) => data,
Err(e) => panic!("history.toml not found: {e}"),
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<SeriesHistory, toml::de::Error> = 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 save_history(&self) -> std::io::Result<usize> { pub(crate) fn save_history(&self) {
let mut file = OpenOptions::new() if let Err(e) = confy::store(env!("CARGO_PKG_NAME"), "history", self) {
.read(true) let err_msg = format!("Unexpected error saving to history.toml: {e}");
.write(true) write_error(err_msg);
.create(true) }
.open("history.toml")
.unwrap();
let json_data = toml::to_string_pretty(&self).unwrap();
file.write(json_data.as_bytes())
} }
pub(crate) fn check_for_post(&self, series: &str, part: &str, title: &str) -> bool { pub(crate) fn check_for_post(&self, series: &str, part: &str, title: &str) -> bool {