diff --git a/.travis.yml b/.travis.yml index 15c31659d70..529c3c4fb5f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ addons: - libcurl4-openssl-dev - libelf-dev - libdw-dev + - binutils-dev rust: - nightly @@ -14,24 +15,26 @@ rust: - stable before_script: - - | - pip install 'travis-cargo<0.2' --user && - export PATH=$HOME/.local/bin:$PATH + - cargo install cargo-kcov + - cargo kcov --print-install-kcov-sh | $SHELL script: - | - travis-cargo build && - travis-cargo test && - travis-cargo bench && - travis-cargo --only stable doc + cargo build && + cargo kcov --coveralls && + cargo bench && + cargo doc + +after_success: | + [ "${TRAVIS_BRANCH}" = master ] && + [ "${TRAVIS_PULL_REQUEST}" == false ] && + [ "${TRAVIS_RUST_VERSION}" == stable ] && + cargo install ghp && + ghp target/doc && + git config user.name "Travis Documentation" && + git config user.email "name@example.com" && + git push -qf https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git gh-pages 2>&1 > /dev/null -after_success: - # upload the documentation from the build with stable (automatically only actually - # runs on the master branch, not individual PRs) - - travis-cargo --only stable doc-upload - # measure code coverage and upload to coveralls.io - - travis-cargo coveralls --no-sudo env: global: - - TRAVIS_CARGO_NIGHTLY_FEATURE="" - secure: "N0VHBpQ8Co81/xVM+MQn2JZIFkJMNYf1nn+lwxUrfotesRQUAR6Wh3KHl/tgCdAFw+WvYk6kfxQ0ODFJd7iRdlZez2nujwxczXJj4pU+vqcFkJhlr1uBl2HrWeboGVv3M/60HWyERRz5nI5lGR5FP5qgPQTE/KRTnrPYADBgKNo=" diff --git a/Cargo.toml b/Cargo.toml index 15359124407..ded0d775e9e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,5 +10,7 @@ description = "Unofficial Rust library for the Twitter API." documentation = "https://gifnksm.github.io/twitter-api-rs" [dependencies] -oauth-client = "0.1" -rustc-serialize = "0.3" +oauth-client = "0.2" +serde = "1.0" +serde_derive = "1.0" +serde_json = "1.0" diff --git a/examples/tweet.rs b/examples/tweet.rs index bd0e79ecaea..b686af892f1 100644 --- a/examples/tweet.rs +++ b/examples/tweet.rs @@ -3,19 +3,19 @@ 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 std::convert::AsRef; -use std::io; -use std::io::prelude::*; use std::env; use std::fs::{File, OpenOptions}; +use std::io; +use std::io::prelude::*; use std::path::Path; use std::path::PathBuf; -use rustc_serialize::Decodable; -use rustc_serialize::json::{self, Json}; -use oauth::Token; const TWITTER_CONF_FILENAME: &'static str = ".twitter.conf"; @@ -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, @@ -42,16 +42,18 @@ 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) { - let mut file = match OpenOptions::new().write(true).create(true).open(path_file) { + let mut file = match OpenOptions::new() + .write(true) + .create(true) + .open(path_file) { 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) { diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 00000000000..33709c55122 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,5 @@ +reorder_imports = true +reorder_imported_names = true +use_try_shorthand = true +write_mode = "overwrite" + diff --git a/src/error.rs b/src/error.rs index d63352cdc92..dd14fadc8a2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,13 +1,12 @@ -use std::{error, fmt, string}; 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 { @@ -15,8 +14,7 @@ impl fmt::Display for Error { 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), } } } @@ -26,8 +24,7 @@ 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(), } } @@ -35,8 +32,7 @@ impl error::Error for 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), } } } @@ -53,15 +49,8 @@ impl From for Error { } } -impl From for Error { - fn from(err: json::BuilderError) -> Error { - Error::JsonBuilder(err) - } -} - - -impl From for Error { - fn from(err: json::DecoderError) -> Error { - Error::JsonDecoder(err) +impl From for Error { + fn from(err: serde_json::Error) -> Error { + Error::Json(err) } } diff --git a/src/lib.rs b/src/lib.rs index 8333b03ef39..c1ac9ffcaa4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,15 +7,14 @@ #![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 std::borrow::Cow; use std::collections::HashMap; -use oauth::Token; -use rustc_serialize::Decodable; -use rustc_serialize::json::{self, Json}; - -pub use error::Error; mod error; @@ -31,7 +30,7 @@ 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, @@ -39,9 +38,8 @@ pub struct Tweet { impl Tweet { pub fn parse_timeline(json_string: String) -> Result, Error> { - let conf = try!(Json::from_str(&json_string)); - let d = try!(Decodable::decode(&mut json::Decoder::new(conf))); - Ok(d) + let conf = serde_json::from_str(&json_string)?; + Ok(conf) } } @@ -57,8 +55,8 @@ fn split_query<'a>(query: &'a str) -> HashMap, Cow<'a, str>> { } pub fn get_request_token(consumer: &Token) -> Result, Error> { - let bytes = try!(oauth::get(api_twitter_oauth::REQUEST_TOKEN, consumer, None, None)); - let resp = try!(String::from_utf8(bytes)); + let bytes = oauth::get(api_twitter_oauth::REQUEST_TOKEN, consumer, None, None)?; + let resp = String::from_utf8(bytes)?; let param = split_query(&resp); let token = Token::new(param.get("oauth_token").unwrap().to_string(), param.get("oauth_token_secret").unwrap().to_string()); @@ -77,11 +75,11 @@ pub fn get_access_token(consumer: &Token, -> Result, Error> { let mut param = HashMap::new(); let _ = param.insert("oauth_verifier".into(), pin.into()); - let bytes = try!(oauth::get(api_twitter_oauth::ACCESS_TOKEN, - consumer, - Some(request), - Some(¶m))); - let resp = try!(String::from_utf8(bytes)); + let bytes = oauth::get(api_twitter_oauth::ACCESS_TOKEN, + consumer, + Some(request), + Some(¶m))?; + let resp = String::from_utf8(bytes)?; let param = split_query(&resp); let token = Token::new(param.get("oauth_token").unwrap().to_string(), param.get("oauth_token_secret").unwrap().to_string()); @@ -93,19 +91,19 @@ pub fn get_access_token(consumer: &Token, pub fn update_status(consumer: &Token, access: &Token, status: &str) -> Result<(), Error> { let mut param = HashMap::new(); let _ = param.insert("status".into(), status.into()); - let _ = try!(oauth::post(api_twitter_soft::UPDATE_STATUS, - consumer, - Some(access), - Some(¶m))); + let _ = oauth::post(api_twitter_soft::UPDATE_STATUS, + consumer, + Some(access), + Some(¶m))?; Ok(()) } pub fn get_last_tweets(consumer: &Token, access: &Token) -> Result, Error> { - let bytes = try!(oauth::get(api_twitter_soft::HOME_TIMELINE, - consumer, - Some(access), - None)); - let last_tweets_json = try!(String::from_utf8(bytes)); - let ts = try!(Tweet::parse_timeline(last_tweets_json)); + let bytes = oauth::get(api_twitter_soft::HOME_TIMELINE, + consumer, + Some(access), + None)?; + let last_tweets_json = String::from_utf8(bytes)?; + let ts = Tweet::parse_timeline(last_tweets_json)?; Ok(ts) }