Removes the TUI in favor of event based logging #15

Merged
Neshura merged 17 commits from remove_tui into main 2023-12-30 00:28:47 +00:00
2 changed files with 12 additions and 66 deletions
Showing only changes of commit cbb50e5d53 - Show all commits

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