From 2a8cca37e567ae410e4094708078a2e5eeb9d2c4 Mon Sep 17 00:00:00 2001 From: SadlyNotSappho Date: Tue, 29 Aug 2023 14:50:03 -0700 Subject: [PATCH] error handling and get clippy to stop yelling at me (it all still compiles so who cares !!!) --- src/cache.rs | 2 +- src/lib.rs | 101 +++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 83 insertions(+), 20 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index fd90c15..1198647 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -1,6 +1,6 @@ use std::{fs, process}; -pub async fn download_image(cache_path: &String, image_url: String, file_name: String) { +pub async fn download_image(cache_path: &str, image_url: String, file_name: String) { let replaced = cache_path.replace('~', &crate::get_home()[..]); // replace ~ with $HOME crate::ensure_exists(&replaced); diff --git a/src/lib.rs b/src/lib.rs index 8384efb..436629e 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,53 +27,104 @@ pub fn get_home() -> String { eprintln!("lib::get_home: Couldn't get $HOME on other OS: {why:?}"); process::exit(1) } - } + }, } } -pub fn create_config(file: &String) -> bool { +pub fn create_config(file: &str) -> 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); // Path without the filename - if let Some(_) = file_path.parent() { + if file_path.parent().is_some() { // if the file_path has a parent folder - fs::create_dir_all(&path).expect("couldn't create needed folders") // create folders + match fs::create_dir_all(&path) { + Ok(var) => var, + Err(why) => { + eprintln!("lib::create_config: Couldn't create required folder {path}: {why:?}"); + process::exit(1) + } + } }; 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"); + match fs::write(replaced, file_contents) { + Ok(var) => var, + Err(why) => { + eprintln!("lib::create_config: Couldn't write data to config file: {why:?}"); + process::exit(1) + } + } true } else { false } } -pub fn read_config(file: &String) -> Config { +pub fn read_config(file: &str) -> Config { let replaced = file.replace('~', &get_home()[..]); // replace ~ with $HOME - let read = fs::read_to_string(replaced).expect("couldn't read config file"); - serde_json::from_str(&read[..]).expect("couldn't parse config file's json") + let read = match fs::read_to_string(replaced) { + Ok(var) => var, + Err(why) => { + eprintln!("lib::read_config: Couldn't read config file: {why:?}"); + process::exit(1) + } + }; + match serde_json::from_str(&read[..]) { + Ok(var) => var, + Err(why) => { + eprintln!("lib::read_config: Couldn't parse config file: {why:?}"); + process::exit(1) + } + } } -pub fn update_latest_date(file: &String, new_date: String) { +pub fn update_latest_date(file: &str, 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"); + let newconfig = match serde_json::to_string(&prevconfig) { + Ok(var) => var, + Err(why) => { + eprintln!("lib::update_latest_date: Couldn't serialize Config struct: {why:?}"); + process::exit(1) + } + }; + match fs::write(file.replace('~', &get_home()[..]), newconfig) { + Ok(var) => var, + Err(why) => { + eprintln!("lib::update_latest_date: Couldn't write new data to config file: {why:?}"); + process::exit(1) + } + }; } pub fn link_to_datestring(link: String) -> String { - link.split("=").collect::>().pop().unwrap().to_string() + match link.split('=').collect::>().pop() { + Some(var) => var, + None => { + eprintln!( + "lib::link_to_datestring: Couldn't get the latest element of the split link vector" + ); + process::exit(1) + } + } + .to_string() } -pub fn ensure_exists(path: &String) { +pub fn ensure_exists(path: &str) { let p = Path::new(path); if !p.exists() { - fs::create_dir_all(p).expect(&format!("couldn't create folder {path}")[..]) + match fs::create_dir_all(p) { + Ok(var) => var, + Err(why) => { + eprintln!("lib::ensure_exists: Couldn't create directory(ies) {path}: {why:?}"); + process::exit(1) + } + } } } @@ -91,15 +142,27 @@ pub struct Page { pub image: String, } -pub async fn get_page(date: &String) -> Page { - let page_html = &reqwest::get(format!( +pub async fn get_page(date: &str) -> Page { + let page_html = &match match reqwest::get(format!( "https://girlgeniusonline.com/comic.php?date={date}" )) .await - .unwrap() + { + Ok(var) => var, + Err(why) => { + eprintln!("lib::get_page: Couldn't get the page {date}: {why:?}"); + process::exit(1) + } + } .text() .await - .unwrap()[..]; // ah i love rust + { + Ok(var) => var, + Err(why) => { + eprintln!("lib::get_page: Couldn't get the HTML for page {date}: {why:?}"); + process::exit(1) + } + }[..]; // honestly? somewhat surprised that rust lets me do this, but it does make sense let parsed = Html::parse_document(page_html); @@ -124,7 +187,7 @@ pub async fn get_page(date: &String) -> Page { } Page { - date: date.clone(), + date: date.to_string(), next_page, prev_page, image,