clean up get_home() and improve several error messages

This commit is contained in:
SadlyNotSappho 2023-08-23 12:37:51 -07:00
parent 04893105df
commit de4fece245
1 changed files with 5 additions and 11 deletions

View File

@ -4,15 +4,13 @@ use scraper::{Html, Selector};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
pub fn get_home() -> String { pub fn get_home() -> String {
let home = match std::env::consts::OS { match std::env::consts::OS {
"linux" => std::env::var("HOME").expect("how is there not a $HOME"), "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"), "windows" => std::env::var("userprofile").expect("yell at me if this doesn't work please"),
_ => std::env::var("HOME").expect( _ => std::env::var("HOME").expect(
"if this doesn't work, you're probably on macos, which i literally cannot test on", "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 { pub fn create_config(file: &String) -> bool {
@ -39,13 +37,13 @@ pub fn create_config(file: &String) -> bool {
pub fn read_config(file: &String) -> Config { pub fn read_config(file: &String) -> Config {
let replaced = file.replace('~', &get_home()[..]); // replace ~ with $HOME let replaced = file.replace('~', &get_home()[..]); // replace ~ with $HOME
let read = fs::read_to_string(replaced).expect("couldn't read config file"); let read = fs::read_to_string(replaced).expect("couldn't read config file");
serde_json::from_str(&read[..]).expect("couldn't parse json") serde_json::from_str(&read[..]).expect("couldn't parse config file's json")
} }
pub fn ensure_exists(path: String) { pub fn ensure_exists(path: String) {
let p = Path::new(&path); let p = Path::new(&path);
if !p.exists() { if !p.exists() {
fs::create_dir_all(p).expect("couldn't create folder") fs::create_dir_all(p).expect(&format!("couldn't create folder {path}")[..])
} }
} }
@ -70,9 +68,7 @@ pub async fn get_page(date: String) {
.unwrap() .unwrap()
.text() .text()
.await .await
.unwrap()[..]; .unwrap()[..]; // ah i love rust
// println!("{page_html}");
let parsed = Html::parse_document(page_html); let parsed = Html::parse_document(page_html);
@ -83,5 +79,3 @@ pub async fn get_page(date: String) {
} }
println!("{url}"); println!("{url}");
} }
// url: https://girlgeniusonline.com/comic.php?date={date}