error handling and get clippy to stop yelling at me (it all still compiles so who cares !!!)
This commit is contained in:
parent
d8d3311d25
commit
2a8cca37e5
|
@ -1,6 +1,6 @@
|
||||||
use std::{fs, process};
|
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
|
let replaced = cache_path.replace('~', &crate::get_home()[..]); // replace ~ with $HOME
|
||||||
crate::ensure_exists(&replaced);
|
crate::ensure_exists(&replaced);
|
||||||
|
|
||||||
|
|
101
src/lib.rs
101
src/lib.rs
|
@ -27,53 +27,104 @@ pub fn get_home() -> String {
|
||||||
eprintln!("lib::get_home: Couldn't get $HOME on other OS: {why:?}");
|
eprintln!("lib::get_home: Couldn't get $HOME on other OS: {why:?}");
|
||||||
process::exit(1)
|
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 replaced = file.replace('~', &get_home()[..]); // replace ~ with $HOME
|
||||||
let mut paths = replaced.split('/').collect::<Vec<&str>>(); // split path by /
|
let mut paths = replaced.split('/').collect::<Vec<&str>>(); // split path by /
|
||||||
paths.pop();
|
paths.pop();
|
||||||
let path = paths.join("/");
|
let path = paths.join("/");
|
||||||
let file_path = std::path::Path::new(&path); // Path without the filename
|
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
|
// 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() {
|
if file_path.exists() && !Path::new(&replaced).exists() {
|
||||||
let file_contents = "{\"latest_date\": \"20021104\",\"cache_folder\": \"~/.cache/ggg\"}";
|
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
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
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 replaced = file.replace('~', &get_home()[..]); // replace ~ with $HOME
|
||||||
let read = fs::read_to_string(replaced).expect("couldn't read config file");
|
let read = match fs::read_to_string(replaced) {
|
||||||
serde_json::from_str(&read[..]).expect("couldn't parse config file's json")
|
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);
|
let mut prevconfig = read_config(file);
|
||||||
prevconfig.latest_date = new_date;
|
prevconfig.latest_date = new_date;
|
||||||
|
|
||||||
let newconfig = serde_json::to_string(&prevconfig).expect("couldn't serialize json data");
|
let newconfig = match serde_json::to_string(&prevconfig) {
|
||||||
fs::write(file.replace('~', &get_home()[..]), newconfig).expect("couldn't write to config file");
|
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 {
|
pub fn link_to_datestring(link: String) -> String {
|
||||||
link.split("=").collect::<Vec<&str>>().pop().unwrap().to_string()
|
match link.split('=').collect::<Vec<&str>>().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);
|
let p = Path::new(path);
|
||||||
if !p.exists() {
|
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 image: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_page(date: &String) -> Page {
|
pub async fn get_page(date: &str) -> Page {
|
||||||
let page_html = &reqwest::get(format!(
|
let page_html = &match match reqwest::get(format!(
|
||||||
"https://girlgeniusonline.com/comic.php?date={date}"
|
"https://girlgeniusonline.com/comic.php?date={date}"
|
||||||
))
|
))
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
{
|
||||||
|
Ok(var) => var,
|
||||||
|
Err(why) => {
|
||||||
|
eprintln!("lib::get_page: Couldn't get the page {date}: {why:?}");
|
||||||
|
process::exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
.text()
|
.text()
|
||||||
.await
|
.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);
|
let parsed = Html::parse_document(page_html);
|
||||||
|
|
||||||
|
@ -124,7 +187,7 @@ pub async fn get_page(date: &String) -> Page {
|
||||||
}
|
}
|
||||||
|
|
||||||
Page {
|
Page {
|
||||||
date: date.clone(),
|
date: date.to_string(),
|
||||||
next_page,
|
next_page,
|
||||||
prev_page,
|
prev_page,
|
||||||
image,
|
image,
|
||||||
|
|
Loading…
Reference in New Issue