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;
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(())
}

View file

@ -1,70 +1,26 @@
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<String, PostHistory>,
}
impl SeriesHistory {
pub(crate) fn load_history() -> Result<Self, ()> {
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()) {
pub(crate) fn load_history() -> Self {
match confy::load(env!("CARGO_PKG_NAME"), "history") {
Ok(data) => data,
Err(e) => {
let err_msg = format!("{e}");
Err(e) => panic!("history.toml not found: {e}"),
}
}
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);
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> {
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 check_for_post(&self, series: &str, part: &str, title: &str) -> bool {
if let Some(series_map) = self.series.get(series) {