diff --git a/src/post_history/mod.rs b/src/post_history/mod.rs new file mode 100644 index 0000000..5d30a9f --- /dev/null +++ b/src/post_history/mod.rs @@ -0,0 +1,103 @@ +use std::collections::HashMap; +use std::error::Error; +use std::fs; +use std::fs::OpenOptions; +use std::io::Write; +use std::path::Path; +use serde_derive::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Clone)] +pub(crate) struct SeriesHistory { + pub(crate) series: HashMap, +} + +impl SeriesHistory { + pub(crate) fn load_history() -> Result, Box> { + match Path::new("history.toml").exists() { + true => { + let file_contents: String = fs::read_to_string("history.toml")?; + + let history: Result = match file_contents.len() { + 0 => return Ok(None), + _ => toml::from_str(file_contents.as_str()), + }; + + match history { + Ok(data) => Ok(Some(data)), + Err(e) => Err(Box::new(e)) + } + }, + false => { + Ok(None) + } + } + } + + 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 check_for_post(&self, series: &str, part: &str, title: &str) -> bool { + if let Some(series_map) = self.series.get(series) { + if let Some(part_info) = series_map.parts.get(part) { + return part_info.volume == title || part_info.chapter == title + } + } + return false + } + + pub(crate) fn get_series(&self, series: &str) -> PostHistory { + match self.series.get(series) { + Some(history) => history.clone(), + None => PostHistory { + parts: HashMap::new() + } + } + } + + pub(crate) fn set_series(&mut self, series: &str, data: PostHistory) { + self.series.entry(series.to_string()).and_modify(|val| { + *val = data.clone() + }) + .or_insert(data); + } +} + +#[derive(Serialize, Deserialize, Clone)] +pub(crate) struct PostHistory { + parts: HashMap, +} + +impl PostHistory { + pub(crate) fn get_part(&self, part: &str) -> PostHistoryInner { + match self.parts.get(part) { + Some(history) => history.clone(), + None => PostHistoryInner { + volume: "".to_string(), + chapter: "".to_string(), + } + } + } + + pub(crate) fn set_part(&mut self, part: &str, data: PostHistoryInner) { + self.parts.entry(part.to_string()).and_modify(|val| { + *val = data.clone() + }) + .or_insert(data); + } +} + +#[derive(Serialize, Deserialize, Clone)] +pub(crate) struct PostHistoryInner { + pub(crate) volume: String, + pub(crate) chapter: String, +}