Changed from json to serde_json
Changed upvotes and downvotes to u64s.
This commit is contained in:
+3
-2
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rapr"
|
name = "rapr"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
authors = ["Uttarayan Mondal <uttarayan21@gmail.com>"]
|
authors = ["Uttarayan Mondal <uttarayan21@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
@@ -10,6 +10,7 @@ edition = "2018"
|
|||||||
|
|
||||||
derive-error = "0.0.5"
|
derive-error = "0.0.5"
|
||||||
reqwest = "0.11.2"
|
reqwest = "0.11.2"
|
||||||
json = "0.12.4"
|
|
||||||
tokio = { version = "1.4.0", features = ["full"] }
|
tokio = { version = "1.4.0", features = ["full"] }
|
||||||
|
serde = "1.0.125"
|
||||||
|
serde_json = "1.0.64"
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,10 @@
|
|||||||
//!}
|
//!}
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate derive_error;
|
extern crate derive_error;
|
||||||
|
extern crate reqwest;
|
||||||
|
extern crate serde;
|
||||||
|
extern crate serde_json;
|
||||||
|
extern crate tokio;
|
||||||
mod rapr;
|
mod rapr;
|
||||||
pub use crate::rapr::Error;
|
pub use crate::rapr::Error;
|
||||||
pub use crate::rapr::{RaPost, RaPostItems, RaSub, RaprClient};
|
pub use crate::rapr::{RaPost, RaPostItems, RaSub, RaprClient};
|
||||||
|
|||||||
+17
-15
@@ -1,6 +1,3 @@
|
|||||||
extern crate json;
|
|
||||||
extern crate reqwest;
|
|
||||||
extern crate tokio;
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
/// Error enum
|
/// Error enum
|
||||||
@@ -8,15 +5,15 @@ use std::fmt;
|
|||||||
pub enum Error {
|
pub enum Error {
|
||||||
UnexpectedJson,
|
UnexpectedJson,
|
||||||
NoneError,
|
NoneError,
|
||||||
JsonParseError(json::Error),
|
JsonParseError(serde_json::Error),
|
||||||
RequestError(reqwest::Error),
|
RequestError(reqwest::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Items related to the post
|
/// Items related to the post
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct RaPostItems {
|
pub struct RaPostItems {
|
||||||
pub upvotes: u32,
|
pub upvotes: u64,
|
||||||
pub downvotes: u32,
|
pub downvotes: u64,
|
||||||
pub permalink: String,
|
pub permalink: String,
|
||||||
pub url: Option<String>,
|
pub url: Option<String>,
|
||||||
}
|
}
|
||||||
@@ -24,7 +21,7 @@ pub struct RaPostItems {
|
|||||||
impl RaPostItems {
|
impl RaPostItems {
|
||||||
/// Create a new struct of items for a post
|
/// Create a new struct of items for a post
|
||||||
/// You probably don't need this fucntion.
|
/// You probably don't need this fucntion.
|
||||||
pub fn new(upvotes: u32, downvotes: u32, permalink: &str, url: Option<String>) -> Self {
|
pub fn new(upvotes: u64, downvotes: u64, permalink: &str, url: Option<String>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
upvotes,
|
upvotes,
|
||||||
downvotes,
|
downvotes,
|
||||||
@@ -43,7 +40,7 @@ pub struct RaPost {
|
|||||||
pub title: String,
|
pub title: String,
|
||||||
pub text: Option<String>,
|
pub text: Option<String>,
|
||||||
pub items: RaPostItems,
|
pub items: RaPostItems,
|
||||||
pub json: json::JsonValue,
|
pub json: serde_json::Value,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for RaPost {
|
impl fmt::Debug for RaPost {
|
||||||
@@ -65,7 +62,7 @@ impl RaPost {
|
|||||||
title: &str,
|
title: &str,
|
||||||
text: Option<&str>,
|
text: Option<&str>,
|
||||||
items: RaPostItems,
|
items: RaPostItems,
|
||||||
json: &json::JsonValue,
|
json: &serde_json::Value,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
id: id.to_string(),
|
id: id.to_string(),
|
||||||
@@ -78,12 +75,17 @@ impl RaPost {
|
|||||||
|
|
||||||
/// Parse json from `json['data']['children']` array elements.
|
/// Parse json from `json['data']['children']` array elements.
|
||||||
/// Note: In case of url mission reddit make url the permalink
|
/// Note: In case of url mission reddit make url the permalink
|
||||||
pub fn parse(post: &json::JsonValue) -> Result<RaPost, Error> {
|
pub fn parse(post: &serde_json::Value) -> Result<RaPost, Error> {
|
||||||
let id = post["name"].as_str().ok_or(Error::UnexpectedJson)?;
|
let id = post["name"].as_str().ok_or(Error::UnexpectedJson)?;
|
||||||
|
|
||||||
let title = post["title"].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 permalink = post["permalink"].as_str().ok_or(Error::UnexpectedJson)?;
|
||||||
|
|
||||||
let url = post["url"].as_str().map(String::from);
|
let url = post["url"].as_str().map(String::from);
|
||||||
|
|
||||||
let mut selftext = post["selftext"].as_str();
|
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<json::JsonValue> = match parsed["data"]["children"].take() {
|
let raw_posts: Vec<serde_json::Value> = match parsed["data"]["children"].take() {
|
||||||
json::JsonValue::Array(arr) => arr,
|
serde_json::Value::Array(arr) => arr,
|
||||||
_ => return Err(Error::UnexpectedJson),
|
_ => return Err(Error::UnexpectedJson),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user