diff --git a/Cargo.toml b/Cargo.toml index 34941ba4..a7201197 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rapr" -version = "0.1.0" +version = "0.1.1" authors = ["Uttarayan Mondal "] edition = "2018" @@ -10,6 +10,7 @@ edition = "2018" derive-error = "0.0.5" reqwest = "0.11.2" -json = "0.12.4" tokio = { version = "1.4.0", features = ["full"] } +serde = "1.0.125" +serde_json = "1.0.64" diff --git a/src/lib.rs b/src/lib.rs index c2fd0586..7ec85de9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,10 @@ //!} #[macro_use] extern crate derive_error; +extern crate reqwest; +extern crate serde; +extern crate serde_json; +extern crate tokio; mod rapr; pub use crate::rapr::Error; pub use crate::rapr::{RaPost, RaPostItems, RaSub, RaprClient}; diff --git a/src/rapr.rs b/src/rapr.rs index fb845852..61317cf1 100644 --- a/src/rapr.rs +++ b/src/rapr.rs @@ -1,6 +1,3 @@ -extern crate json; -extern crate reqwest; -extern crate tokio; use std::fmt; /// Error enum @@ -8,15 +5,15 @@ use std::fmt; pub enum Error { UnexpectedJson, NoneError, - JsonParseError(json::Error), + JsonParseError(serde_json::Error), RequestError(reqwest::Error), } /// Items related to the post #[derive(Debug, Clone)] pub struct RaPostItems { - pub upvotes: u32, - pub downvotes: u32, + pub upvotes: u64, + pub downvotes: u64, pub permalink: String, pub url: Option, } @@ -24,7 +21,7 @@ pub struct RaPostItems { impl RaPostItems { /// Create a new struct of items for a post /// You probably don't need this fucntion. - pub fn new(upvotes: u32, downvotes: u32, permalink: &str, url: Option) -> Self { + pub fn new(upvotes: u64, downvotes: u64, permalink: &str, url: Option) -> Self { Self { upvotes, downvotes, @@ -43,7 +40,7 @@ pub struct RaPost { pub title: String, pub text: Option, pub items: RaPostItems, - pub json: json::JsonValue, + pub json: serde_json::Value, } impl fmt::Debug for RaPost { @@ -65,7 +62,7 @@ impl RaPost { title: &str, text: Option<&str>, items: RaPostItems, - json: &json::JsonValue, + json: &serde_json::Value, ) -> Self { Self { id: id.to_string(), @@ -78,12 +75,17 @@ impl RaPost { /// Parse json from `json['data']['children']` array elements. /// Note: In case of url mission reddit make url the permalink - pub fn parse(post: &json::JsonValue) -> Result { + pub fn parse(post: &serde_json::Value) -> Result { let id = post["name"].as_str().ok_or(Error::UnexpectedJson)?; + let title = post["title"].as_str().ok_or(Error::UnexpectedJson)?; - let upvotes = post["ups"].as_u32().ok_or(Error::UnexpectedJson)?; - let downvotes = post["downs"].as_u32().ok_or(Error::UnexpectedJson)?; + + let upvotes = post["ups"].as_u64().ok_or(Error::UnexpectedJson)?; + + let downvotes = post["downs"].as_u64().ok_or(Error::UnexpectedJson)?; + let permalink = post["permalink"].as_str().ok_or(Error::UnexpectedJson)?; + let url = post["url"].as_str().map(String::from); let mut selftext = post["selftext"].as_str(); @@ -192,10 +194,10 @@ impl RaprClient { } }; - let mut parsed = json::parse(res.text().await?.as_str())?; + let mut parsed: serde_json::Value = serde_json::from_str(res.text().await?.as_str())?; - let raw_posts: Vec = match parsed["data"]["children"].take() { - json::JsonValue::Array(arr) => arr, + let raw_posts: Vec = match parsed["data"]["children"].take() { + serde_json::Value::Array(arr) => arr, _ => return Err(Error::UnexpectedJson), };