From f2568ec7b9ae3b148a99ef80800f297a4923d56e Mon Sep 17 00:00:00 2001 From: gifnksm Date: Tue, 2 May 2017 15:55:23 +0900 Subject: [PATCH 1/4] Update dependency --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 15359124407..5079eae2908 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,5 +10,5 @@ description = "Unofficial Rust library for the Twitter API." documentation = "https://gifnksm.github.io/twitter-api-rs" [dependencies] -oauth-client = "0.1" +oauth-client = "0.2" rustc-serialize = "0.3" From 333ba0d2141f3c047de5a43d5008113410aae78f Mon Sep 17 00:00:00 2001 From: gifnksm Date: Tue, 2 May 2017 15:56:21 +0900 Subject: [PATCH 2/4] Run rustfmt --- examples/tweet.rs | 15 +++++++++------ rustfmt.toml | 5 +++++ src/error.rs | 3 ++- src/lib.rs | 46 +++++++++++++++++++++++----------------------- 4 files changed, 39 insertions(+), 30 deletions(-) create mode 100644 rustfmt.toml diff --git a/examples/tweet.rs b/examples/tweet.rs index bd0e79ecaea..56bbdc89179 100644 --- a/examples/tweet.rs +++ b/examples/tweet.rs @@ -6,16 +6,16 @@ extern crate twitter_api as twitter; extern crate rustc_serialize as rustc_serialize; 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::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"; @@ -47,7 +47,10 @@ impl Config { } 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), }; 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..0c4ee8cf27f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,7 @@ -use std::{error, fmt, string}; + use oauth; use rustc_serialize::json; +use std::{error, fmt, string}; #[derive(Debug)] pub enum Error { diff --git a/src/lib.rs b/src/lib.rs index 8333b03ef39..e5532703704 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,13 +9,13 @@ extern crate oauth_client as oauth; extern crate rustc_serialize as rustc_serialize; -use std::borrow::Cow; -use std::collections::HashMap; + +pub use error::Error; use oauth::Token; use rustc_serialize::Decodable; use rustc_serialize::json::{self, Json}; - -pub use error::Error; +use std::borrow::Cow; +use std::collections::HashMap; mod error; @@ -39,8 +39,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))); + let conf = Json::from_str(&json_string)?; + let d = Decodable::decode(&mut json::Decoder::new(conf))?; Ok(d) } } @@ -57,8 +57,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 +77,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 +93,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) } From 1c22f1a95bac53b05931cceb5fbb4d2ab1044106 Mon Sep 17 00:00:00 2001 From: gifnksm Date: Tue, 2 May 2017 15:57:12 +0900 Subject: [PATCH 3/4] Stop depending on travis-cargo --- .travis.yml | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) 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=" From b8e4609d2295dcda1e7c41f516b532c1fbc62a69 Mon Sep 17 00:00:00 2001 From: gifnksm Date: Tue, 2 May 2017 16:06:48 +0900 Subject: [PATCH 4/4] Migrate to serde --- Cargo.toml | 4 +++- examples/tweet.rs | 13 ++++++------- src/error.rs | 28 ++++++++-------------------- src/lib.rs | 14 ++++++-------- 4 files changed, 23 insertions(+), 36 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5079eae2908..ded0d775e9e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/examples/tweet.rs b/examples/tweet.rs index 56bbdc89179..b686af892f1 100644 --- a/examples/tweet.rs +++ b/examples/tweet.rs @@ -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}; @@ -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,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) { @@ -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) { diff --git a/src/error.rs b/src/error.rs index 0c4ee8cf27f..dd14fadc8a2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,14 +1,12 @@ - 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 { @@ -16,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), } } } @@ -27,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(), } } @@ -36,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), } } } @@ -54,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 e5532703704..c1ac9ffcaa4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -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 = 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) } }