Unify errors into one struct
This commit is contained in:
parent
dd62cc2c19
commit
1d8210967c
|
@ -28,22 +28,22 @@ pub fn get_client() -> reqwest::Client {
|
||||||
pub async fn get_sauce_info(
|
pub async fn get_sauce_info(
|
||||||
client: reqwest::Client,
|
client: reqwest::Client,
|
||||||
sauce: i32,
|
sauce: i32,
|
||||||
) -> Result<structs::GalleryInfo, reqwest::Error> {
|
) -> Result<structs::GalleryInfo, structs::Error> {
|
||||||
let mut uri = String::from("https://nhentai.net/api/gallery/");
|
let mut uri = String::from("https://nhentai.net/api/gallery/");
|
||||||
uri.push_str(&sauce.to_string());
|
uri.push_str(&sauce.to_string());
|
||||||
let resp = client.get(&uri).send().await?;
|
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(
|
pub async fn get_search_info(
|
||||||
client: reqwest::Client,
|
client: reqwest::Client,
|
||||||
search_query: &str,
|
search_query: &str,
|
||||||
) -> Result<structs::SearchInfo, reqwest::Error> {
|
) -> Result<structs::SearchInfo, structs::Error> {
|
||||||
let uri = "https://nhentai.net/api/galleries/search";
|
let uri = "https://nhentai.net/api/galleries/search";
|
||||||
let resp = client
|
let resp = client
|
||||||
.get(uri)
|
.get(uri)
|
||||||
.query(&[("query", search_query)])
|
.query(&[("query", search_query)])
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
Ok(serde_json::from_str(&resp.text().await?).unwrap())
|
Ok(serde_json::from_str(&resp.text().await?)?)
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,6 +108,36 @@ impl fmt::Display for GalleryInfoSuccess {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
Reqwest(reqwest::Error),
|
||||||
|
SerdeJSON(serde_json::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<reqwest::Error> for Error {
|
||||||
|
#[inline]
|
||||||
|
fn from(error: reqwest::Error) -> Error {
|
||||||
|
Error::Reqwest(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<serde_json::Error> 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<i32, D::Error>
|
fn convert_to_i32<'de, D>(deserializer: D) -> Result<i32, D::Error>
|
||||||
where
|
where
|
||||||
D: Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
|
|
Loading…
Reference in New Issue