Skip to content
This repository has been archived by the owner on May 2, 2023. It is now read-only.

Commit

Permalink
Migrate to serde
Browse files Browse the repository at this point in the history
  • Loading branch information
gifnksm committed May 2, 2017
1 parent 1c22f1a commit b8e4609
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 36 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ documentation = "https://gifnksm.github.io/twitter-api-rs"

[dependencies]
oauth-client = "0.2"
rustc-serialize = "0.3"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
13 changes: 6 additions & 7 deletions examples/tweet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
unused_qualifications, unused_results)]

extern crate twitter_api as twitter;
extern crate rustc_serialize as rustc_serialize;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate oauth_client as oauth;

use oauth::Token;
use rustc_serialize::Decodable;
use rustc_serialize::json::{self, Json};
use std::convert::AsRef;
use std::env;
use std::fs::{File, OpenOptions};
Expand All @@ -28,7 +28,7 @@ fn get_home_dir() -> PathBuf {
}
}

#[derive(Debug, RustcEncodable, RustcDecodable)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
pub consumer_key: String,
pub consumer_secret: String,
Expand All @@ -42,8 +42,7 @@ impl Config {
Ok(f) => f,
Err(_) => return None,
};
let conf = Json::from_reader(&mut file).unwrap();
Decodable::decode(&mut json::Decoder::new(conf)).ok()
serde_json::from_reader(&mut file).ok()
}

pub fn write(&self, path_file: &Path) {
Expand All @@ -54,7 +53,7 @@ impl Config {
Ok(f) => f,
Err(e) => panic!("{}", e),
};
let _ = write!(&mut file, "{}\n", &json::encode(self).unwrap());
let _ = write!(&mut file, "{}\n", &serde_json::to_string(self).unwrap());
}

pub fn create(path_file: &Path) {
Expand Down
28 changes: 8 additions & 20 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@

use oauth;
use rustc_serialize::json;
use serde_json;
use std::{error, fmt, string};

#[derive(Debug)]
pub enum Error {
OAuth(oauth::Error),
FromUtf8(string::FromUtf8Error),
JsonBuilder(json::BuilderError),
JsonDecoder(json::DecoderError),
Json(serde_json::Error),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::OAuth(ref err) => write!(f, "OAuth error: {}", err),
Error::FromUtf8(ref err) => write!(f, "String conversion error: {}", err),
Error::JsonBuilder(ref err) => write!(f, "JSON decoding error: {}", err),
Error::JsonDecoder(ref err) => write!(f, "Decoding to struct error: {}", err),
Error::Json(ref err) => write!(f, "JSON decoding error: {}", err),
}
}
}
Expand All @@ -27,17 +24,15 @@ impl error::Error for Error {
match *self {
Error::OAuth(ref err) => err.description(),
Error::FromUtf8(ref err) => err.description(),
Error::JsonBuilder(ref err) => err.description(),
Error::JsonDecoder(ref err) => err.description(),
Error::Json(ref err) => err.description(),
}
}

fn cause(&self) -> Option<&error::Error> {
match *self {
Error::OAuth(ref err) => Some(err),
Error::FromUtf8(ref err) => Some(err),
Error::JsonBuilder(ref err) => Some(err),
Error::JsonDecoder(ref err) => Some(err),
Error::Json(ref err) => Some(err),
}
}
}
Expand All @@ -54,15 +49,8 @@ impl From<string::FromUtf8Error> for Error {
}
}

impl From<json::BuilderError> for Error {
fn from(err: json::BuilderError) -> Error {
Error::JsonBuilder(err)
}
}


impl From<json::DecoderError> for Error {
fn from(err: json::DecoderError) -> Error {
Error::JsonDecoder(err)
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Error {
Error::Json(err)
}
}
14 changes: 6 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
#![warn(unused_results)]

extern crate oauth_client as oauth;
extern crate rustc_serialize as rustc_serialize;

#[macro_use]
extern crate serde_derive;
extern crate serde_json;

pub use error::Error;
use oauth::Token;
use rustc_serialize::Decodable;
use rustc_serialize::json::{self, Json};
use std::borrow::Cow;
use std::collections::HashMap;

Expand All @@ -31,17 +30,16 @@ mod api_twitter_soft {
json";
}

#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Tweet {
pub created_at: String,
pub text: String,
}

impl Tweet {
pub fn parse_timeline(json_string: String) -> Result<Vec<Tweet>, Error> {
let conf = Json::from_str(&json_string)?;
let d = Decodable::decode(&mut json::Decoder::new(conf))?;
Ok(d)
let conf = serde_json::from_str(&json_string)?;
Ok(conf)
}
}

Expand Down

0 comments on commit b8e4609

Please sign in to comment.