diff --git a/src/api.rs b/src/api.rs index 79f8950..1db29ff 100644 --- a/src/api.rs +++ b/src/api.rs @@ -28,22 +28,22 @@ pub fn get_client() -> reqwest::Client { pub async fn get_sauce_info( client: reqwest::Client, sauce: i32, -) -> Result { +) -> Result { let mut uri = String::from("https://nhentai.net/api/gallery/"); uri.push_str(&sauce.to_string()); let resp = client.get(&uri).send().await?; - Ok(serde_json::from_str(&resp.text().await?).unwrap()) + Ok(serde_json::from_str(&resp.text().await?)?) } pub async fn get_search_info( client: reqwest::Client, search_query: &str, -) -> Result { +) -> Result { let uri = "https://nhentai.net/api/galleries/search"; let resp = client .get(uri) .query(&[("query", search_query)]) .send() .await?; - Ok(serde_json::from_str(&resp.text().await?).unwrap()) + Ok(serde_json::from_str(&resp.text().await?)?) } diff --git a/src/structs.rs b/src/structs.rs index 02e5b97..975c33a 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -108,6 +108,36 @@ impl fmt::Display for GalleryInfoSuccess { } } +#[derive(Debug)] +pub enum Error { + Reqwest(reqwest::Error), + SerdeJSON(serde_json::Error), +} + +impl From for Error { + #[inline] + fn from(error: reqwest::Error) -> Error { + Error::Reqwest(error) + } +} + +impl From for Error { + #[inline] + fn from(error: serde_json::Error) -> Error { + Error::SerdeJSON(error) + } +} + +impl fmt::Display for Error { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + let str = match self { + Error::Reqwest(err) => format!("reqwest error: {}", err), + Error::SerdeJSON(err) => format!("serde_json error: {}", err), + }; + formatter.write_str(&str) + } +} + fn convert_to_i32<'de, D>(deserializer: D) -> Result where D: Deserializer<'de>,