From d9b34538c1004d79e96162c9b6a9c094b4938967 Mon Sep 17 00:00:00 2001 From: SadlyNotSappho Date: Fri, 16 Jun 2023 12:56:13 -0700 Subject: [PATCH] read config, ensure cache exists, fuck lifetimes --- Cargo.toml | 2 ++ src/lib.rs | 59 ++++++++++++++++++++++++++++++++++++----------------- src/main.rs | 4 +++- 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3fd7447..6c73ee9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,5 @@ iced = "0.9.0" regex = "1.8.4" reqwest = "0.11.18" scraper = "0.16.0" +serde = { version = "1.0.164", features = ["derive"] } +serde_json = "1.0.97" diff --git a/src/lib.rs b/src/lib.rs index 8c8c473..81ca91b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,6 @@ -use std::fs; +use std::{fs, path::Path}; + +use serde::{Deserialize, Serialize}; pub struct ConfigFile { pub latest_date: String, @@ -8,32 +10,51 @@ pub struct ConfigFile { pub fn get_home() -> String { let home = match std::env::consts::OS { "linux" => std::env::var("HOME").expect("how is there not a $HOME"), - "windows" => std::env::var("userprofile").expect("yell at me if this doesn't work please i'm not going to touch windows"), - _ => std::env::var("HOME").expect("if this doesn't work, you're probably on macos, which i literally cannot test on"), + "windows" => std::env::var("userprofile") + .expect("yell at me if this doesn't work please i'm not going to touch windows"), + _ => std::env::var("HOME").expect( + "if this doesn't work, you're probably on macos, which i literally cannot test on", + ), }; // println!("{home}"); home } -pub fn create_config(file: String) -> bool { - let replaced = file.replace('~', &get_home()[..]); - let mut paths = replaced.split('/').collect::>(); - let filename = paths.pop(); +pub fn create_config(file: &String) -> bool { + let replaced = file.replace('~', &get_home()[..]); // replace ~ with $HOME + let mut paths = replaced.split('/').collect::>(); // split path by / + paths.pop(); let path = paths.join("/"); - let file_path = std::path::Path::new(&path); - let file_contents = "{\"latest_date\": \"20021104\",\"cache_folder\": \"~/.cache/ggg\"}"; - if let Some(p) = file_path.parent() { - fs::create_dir_all(p).expect("couldn't create needed folders") + let file_path = std::path::Path::new(&path); // Path without the filename + + if let Some(_) = file_path.parent() { + // if the file_path has a parent folder + fs::create_dir_all(&path).expect("couldn't create needed folders") // create folders }; - let cfg_path = std::path::Path::new(format!("{}/{}", file_path.display(), filename.expect("filename somehow died, if you know how please tell me"))); - - if cfg_path.exists() { - false - } else { - - fs::write(cfg_path, file_contents).expect("couldn't write to config file"); + if file_path.exists() && !Path::new(&replaced).exists() { + let file_contents = "{\"latest_date\": \"20021104\",\"cache_folder\": \"~/.cache/ggg\"}"; + fs::write(replaced, file_contents).expect("couldn't write to config file"); true + } else { + false } - +} + +pub fn read_config(file: &String) -> Config { + let read = fs::read_to_string(file).expect("couldn't read config file"); + serde_json::from_str(&read[..]).expect("couldn't parse json") +} + +pub fn ensure_exists(path: String) { + let p = Path::new(&path); + if !p.exists() { + fs::create_dir_all(p).expect("couldn't create folder") + } +} + +#[derive(Serialize, Deserialize)] +pub struct Config { + pub latest_date: String, + pub cache_folder: String, } diff --git a/src/main.rs b/src/main.rs index ef81fe1..aa0e3aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,5 +12,7 @@ fn main() { let config_file = args.config_file; - ggg::create_config(config_file); + ggg::create_config(&config_file); + let config = ggg::read_config(&config_file); + ggg::ensure_exists(config.cache_folder) }