From cea920a0809a0230347c56628c41c6a3f2bed80e Mon Sep 17 00:00:00 2001 From: blank X Date: Wed, 2 Dec 2020 23:00:36 +0700 Subject: [PATCH] Remove utils::fix_gallery --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/structs.rs | 103 ++++++++++++++++++++++++++++++++++++++++++++++++- src/utils.rs | 22 +---------- 4 files changed, 106 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fae283e..7c72d0e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -421,7 +421,7 @@ dependencies = [ [[package]] name = "nhentairs" -version = "0.2.1" +version = "0.2.2" dependencies = [ "reqwest", "serde", diff --git a/Cargo.toml b/Cargo.toml index e2130c3..25cba81 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nhentairs" -version = "0.2.1" +version = "0.2.2" authors = ["blank X "] edition = "2018" diff --git a/src/structs.rs b/src/structs.rs index dbb773e..46fd49e 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -1,4 +1,7 @@ -use serde::Deserialize; +use std::fmt; +use std::marker::PhantomData; +use serde::de::{self, Visitor}; +use serde::{Deserialize, Deserializer}; #[derive(Deserialize, Debug)] pub struct GalleryTitleInfo { @@ -32,6 +35,7 @@ pub struct GalleryTagInfo { #[derive(Deserialize, Debug)] pub struct GalleryInfoSuccess { + #[serde(deserialize_with = "convert_to_i32")] pub id: i32, pub media_id: String, pub title: GalleryTitleInfo, @@ -61,3 +65,100 @@ pub struct SearchInfo { pub num_pages: i32, pub per_page: i32 } + +fn convert_to_i32<'de, D>(deserializer: D) -> Result +where + D: Deserializer<'de> +{ + struct ConvertToI32(PhantomData T>); + + impl<'de> Visitor<'de> for ConvertToI32 + { + type Value = i32; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("an integer between -2^31 and 2^31") + } + + fn visit_i8(self, value: i8) -> Result + where + E: de::Error + { + Ok(i32::from(value)) + } + + fn visit_i16(self, value: i16) -> Result + where + E: de::Error + { + Ok(i32::from(value)) + } + + fn visit_i32(self, value: i32) -> Result + where + E: de::Error + { + Ok(value) + } + + fn visit_i64(self, value: i64) -> Result + where + E: de::Error + { + use std::i32; + if value >= i64::from(i32::MIN) && value <= i64::from(i32::MAX) { + Ok(value as i32) + } else { + Err(E::custom(format!("i32 out of range: {}", value))) + } + } + + fn visit_u8(self, value: u8) -> Result + where + E: de::Error + { + Ok(i32::from(value)) + } + + fn visit_u16(self, value: u16) -> Result + where + E: de::Error + { + Ok(i32::from(value)) + } + + fn visit_u32(self, value: u32) -> Result + where + E: de::Error + { + use std::{i32, u32}; + if value <= i32::MAX as u32 { + Ok(value as i32) + } else { + Err(E::custom(format!("i32 out of range: {}", value))) + } + } + + fn visit_u64(self, value: u64) -> Result + where + E: de::Error + { + use std::{i32, u64}; + if value <= i32::MAX as u64 { + Ok(value as i32) + } else { + Err(E::custom(format!("i32 out of range: {}", value))) + } + } + + fn visit_str(self, value: &str) -> Result + where + E: de::Error + { +// https://brokenco.de/2020/08/03/serde-deserialize-with-string.html + value.parse::().map_err(serde::de::Error::custom) + } + } + + deserializer.deserialize_any(ConvertToI32(PhantomData)) +} diff --git a/src/utils.rs b/src/utils.rs index 78ff89f..49d3b78 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -8,25 +8,13 @@ use std::collections::BTreeMap; extern crate serde_json; extern crate reqwest; -fn fix_gallery(body: &mut serde_json::Value) -> Result<(), serde_json::Error> { - if body["id"].is_string() { - body["id"] = serde_json::json!( - body["id"].as_str().unwrap().parse::().unwrap() - ); - } - Ok(()) -} - pub async fn get_sauce_info(client: reqwest::Client, sauce: i32) -> Result { let mut uri = String::from("https://nhentai.net/api/gallery/"); uri.push_str(&sauce.to_string()); let resp = client.get(&uri) .send() .await?; - let body = resp.text().await?; - let mut body: serde_json::Value = serde_json::from_str(&body).unwrap(); - fix_gallery(&mut body).unwrap(); - Ok(serde_json::from_str(&serde_json::to_string(&body).unwrap()).unwrap()) + Ok(serde_json::from_str(&resp.text().await?).unwrap()) } pub async fn get_search_info(client: reqwest::Client, search_query: &str) -> Result { @@ -35,13 +23,7 @@ pub async fn get_search_info(client: reqwest::Client, search_query: &str) -> Res .query(&[("query", search_query)]) .send() .await?; - 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() { - fix_gallery(&mut body["result"][i]).unwrap(); - } - Ok(serde_json::from_str(&serde_json::to_string(&body).unwrap()).unwrap()) + Ok(serde_json::from_str(&resp.text().await?).unwrap()) } pub async fn download_file(client: reqwest::Client, url: &str, file_name: &str) -> Result<(), reqwest::Error> {