From 8a67b74ce209eb72c2c0a8589af4b6ea2bf6491c Mon Sep 17 00:00:00 2001 From: blank X Date: Wed, 2 Dec 2020 00:15:45 +0700 Subject: [PATCH] Make info not shit itself when it gets an error --- src/commands/download.rs | 6 +++++- src/commands/view.rs | 10 +++++++--- src/structs.rs | 16 ++++++++++++++-- src/utils.rs | 5 ++--- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/commands/download.rs b/src/commands/download.rs index 11f3bf2..5e06b79 100644 --- a/src/commands/download.rs +++ b/src/commands/download.rs @@ -1,4 +1,5 @@ use crate::utils; +use crate::structs; use std::env; use std::path::Path; @@ -26,7 +27,10 @@ pub async fn run(args: env::Args) { let cloned_client = client.clone(); let mut cloned_tx = tx.clone(); handles.push(tokio::spawn(async move { - let sauce_info = utils::get_sauce_info(cloned_client, sauce).await.unwrap(); + let sauce_info = match utils::get_sauce_info(cloned_client, sauce).await.unwrap() { + structs::GalleryInfo::Info(sauce_info) => sauce_info, + structs::GalleryInfo::Error(sauce_error) => panic!("{} returned: {}", sauce, sauce_error.error) + }; let base_path = sauce_info.id.to_string(); let base_path = Path::new(&base_path); match create_dir(base_path) { diff --git a/src/commands/view.rs b/src/commands/view.rs index 7657d8c..5de670d 100644 --- a/src/commands/view.rs +++ b/src/commands/view.rs @@ -16,15 +16,19 @@ pub async fn run(args: env::Args) { exit(1); } let client = reqwest::Client::new(); - let mut handles: Vec> = Vec::with_capacity(sauces.len()); + let mut handles: Vec> = Vec::with_capacity(sauces.len()); for sauce in sauces { let cloned_client = client.clone(); handles.push(tokio::spawn(async move { - utils::get_sauce_info(cloned_client, sauce).await.unwrap() + (utils::get_sauce_info(cloned_client, sauce).await.unwrap(), sauce) })); } for handle in handles { - println!("{}", utils::human_sauce_info(&handle.await.unwrap())); + let (sauce_info, sauce) = handle.await.unwrap(); + match sauce_info { + structs::GalleryInfo::Info(sauce_info) => println!("{}", utils::human_sauce_info(&sauce_info)), + structs::GalleryInfo::Error(sauce_error) => eprintln!("Sauce: {}\nError: {}", sauce, sauce_error.error) + }; if remaining_to_show > 1 { println!(""); remaining_to_show -= 1; diff --git a/src/structs.rs b/src/structs.rs index d001a00..ddc342c 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -31,7 +31,7 @@ pub struct GalleryTagInfo { } #[derive(Deserialize, Debug)] -pub struct GalleryInfo { +pub struct GalleryInfoSuccess { pub id: usize, pub media_id: String, pub title: GalleryTitleInfo, @@ -43,9 +43,21 @@ pub struct GalleryInfo { pub num_favorites: usize } +#[derive(Deserialize, Debug)] +pub struct GalleryInfoError { + pub error: String +} + +#[derive(Deserialize, Debug)] +#[serde(untagged)] +pub enum GalleryInfo { + Info(GalleryInfoSuccess), + Error(GalleryInfoError) +} + #[derive(Deserialize, Debug)] pub struct SearchInfo { - pub result: Vec, + pub result: Vec, pub num_pages: usize, pub per_page: usize } diff --git a/src/utils.rs b/src/utils.rs index 68f5e2e..40c2e14 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -23,7 +23,6 @@ pub async fn get_sauce_info(client: reqwest::Client, sauce: usize) -> Result Res .query(&[("query", search_query)]) .send() .await?; - assert_eq!(resp.status().is_success(), true); + assert!(resp.status().is_success()); let body = resp.text().await?; let mut body: serde_json::Value = serde_json::from_str(&body).unwrap(); for i in 0..body["result"].as_array().unwrap().len() { @@ -73,7 +72,7 @@ pub fn get_arg_sauces(args: env::Args) -> Result, String> { Ok(sauces) } -pub fn human_sauce_info(sauce_info: &structs::GalleryInfo) -> String { +pub fn human_sauce_info(sauce_info: &structs::GalleryInfoSuccess) -> String { let mut text = format!("Sauce: {}\nTitle: ", sauce_info.id); let japanese_title = sauce_info.title.japanese.as_ref(); let english_title = sauce_info.title.english.as_ref();