add ability to update config file, implement debug for Config, and add a utility function

This commit is contained in:
SadlyNotSappho 2023-08-25 12:42:32 -07:00
parent b9def3324e
commit d55c600eee
2 changed files with 15 additions and 1 deletions

View File

@ -40,6 +40,18 @@ pub fn read_config(file: &String) -> Config {
serde_json::from_str(&read[..]).expect("couldn't parse config file's json")
}
pub fn update_latest_date(file: &String, new_date: String) {
let mut prevconfig = read_config(file);
prevconfig.latest_date = new_date;
let newconfig = serde_json::to_string(&prevconfig).expect("couldn't serialize json data");
fs::write(file.replace('~', &get_home()[..]), newconfig).expect("couldn't write to config file");
}
pub fn link_to_datestring(link: String) -> String {
link.split("=").collect::<Vec<&str>>().pop().unwrap().to_string()
}
pub fn ensure_exists(path: String) {
let p = Path::new(&path);
if !p.exists() {
@ -47,7 +59,7 @@ pub fn ensure_exists(path: String) {
}
}
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
pub latest_date: String,
pub cache_folder: String,

View File

@ -20,4 +20,6 @@ async fn main() {
let current_page = ggg::get_page(config.latest_date).await;
println!("{current_page:?}");
ggg::update_latest_date(&config_file, ggg::link_to_datestring(current_page.next_page.unwrap()));
}