diff --git a/Cargo.lock b/Cargo.lock index 9b6609a9c16..79467071cec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,12 +11,14 @@ dependencies = [ "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "multimap 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "strings 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "syntex_errors 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)", "syntex_syntax 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)", "term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", - "toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-segmentation 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -40,6 +42,11 @@ name = "diff" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "dtoa" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "either" version = "1.1.0" @@ -67,6 +74,11 @@ dependencies = [ "either 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "itoa" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -99,6 +111,16 @@ name = "multimap" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "num-traits" +version = "0.1.37" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "quote" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "regex" version = "0.2.1" @@ -130,6 +152,41 @@ dependencies = [ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "serde" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde_derive" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive_internals 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_derive_internals" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", + "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_json" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "dtoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", + "itoa 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "strings" version = "0.0.1" @@ -138,6 +195,24 @@ dependencies = [ "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "syn" +version = "0.11.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "synom" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "syntex_errors" version = "0.58.1" @@ -200,10 +275,10 @@ dependencies = [ [[package]] name = "toml" -version = "0.2.1" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -258,27 +333,37 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum aho-corasick 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "500909c4f87a9e52355b26626d890833e9e1d53ac566db76c36faa984b889699" "checksum bitflags 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1370e9fc2a6ae53aea8b7a5110edbd08836ed87c88736dfabccade1c2b44bff4" "checksum diff 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "0a515461b6c8c08419850ced27bc29e86166dcdcde8fbe76f8b1f0589bb49472" +"checksum dtoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "80c8b71fd71146990a9742fc06dcbbde19161a267e0ad4e572c35162f4578c90" "checksum either 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18785c1ba806c258137c937e44ada9ee7e69a37e3c72077542cd2f069d78562a" "checksum env_logger 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e3856f1697098606fc6cb97a93de88ca3f3bc35bb878c725920e6e82ecf05e83" "checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685" "checksum itertools 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4833d6978da405305126af4ac88569b5d71ff758581ce5a987dbfa3755f694fc" +"checksum itoa 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eb2f404fbc66fd9aac13e998248505e7ecb2ad8e44ab6388684c5fb11c6c251c" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)" = "babb8281da88cba992fa1f4ddec7d63ed96280a1a53ec9b919fd37b53d71e502" "checksum log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "5141eca02775a762cc6cd564d8d2c50f67c0ea3a372cbf1c51592b3e029e10ad" "checksum memchr 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1dbccc0e46f1ea47b9f17e6d67c5a96bd27030519c519c9c91327e31275a47b4" "checksum multimap 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9223f4774d08e06185e44e555b9a7561243d387bac49c78a6205c42d6975fbf2" +"checksum num-traits 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "e1cbfa3781f3fe73dc05321bed52a06d2d491eaa764c52335cf4399f046ece99" +"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum regex 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4278c17d0f6d62dfef0ab00028feb45bd7d2102843f80763474eeb1be8a10c01" "checksum regex-syntax 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9191b1f57603095f105d317e375d19b1c9c5c3185ea9633a99a6dcbed04457" "checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" "checksum same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d931a44fdaa43b8637009e7632a02adc4f2b2e0733c08caa4cf00e8da4a117a7" +"checksum serde 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3b46a59dd63931010fdb1d88538513f3279090d88b5c22ef4fe8440cfffcc6e3" +"checksum serde_derive 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6c06b68790963518008b8ae0152d48be4bbbe77015d2c717f6282eea1824be9a" +"checksum serde_derive_internals 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)" = "021c338d22c7e30f957a6ab7e388cb6098499dda9fd4ba1661ee074ca7a180d1" +"checksum serde_json 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1c62115693d0a9ed8c32d1c760f0fdbe7d4b05cb13c135b9b54137ac0d59fccb" "checksum strings 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "54f86446ab480b4f60782188f4f78886465c5793aee248cbb48b7fdc0d022420" +"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" +"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum syntex_errors 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)" = "867cc5c2d7140ae7eaad2ae9e8bf39cb18a67ca651b7834f88d46ca98faadb9c" "checksum syntex_pos 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)" = "13ad4762fe52abc9f4008e85c4fb1b1fe3aa91ccb99ff4826a439c7c598e1047" "checksum syntex_syntax 0.58.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6e0e4dbae163dd98989464c23dd503161b338790640e11537686f2ef0f25c791" "checksum term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d168af3930b369cfe245132550579d47dfd873d69470755a19c2c6568dbbd989" "checksum thread-id 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4437c97558c70d129e40629a5b385b3fb1ffac301e63941335e4d354081ec14a" "checksum thread_local 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c85048c6260d17cf486ceae3282d9fb6b90be220bf5b28c400f5485ffc29f0c7" -"checksum toml 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "736b60249cb25337bc196faa43ee12c705e426f3d55c214d73a4e7be06f92cb4" +"checksum toml 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3063405db158de3dce8efad5fc89cf1baffb9501a3647dc9505ba109694ce31f" "checksum unicode-segmentation 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "18127285758f0e2c6cf325bb3f3d138a12fee27de4f23e146cd6a179f26c2cf3" "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" "checksum unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91" diff --git a/Cargo.toml b/Cargo.toml index 1629dd94522..568227869b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,8 +19,10 @@ default = ["cargo-fmt"] cargo-fmt = [] [dependencies] -toml = "0.2.1" -rustc-serialize = "0.3" +toml = "0.4" +serde = "1.0" +serde_derive = "1.0" +serde_json = "1.0" unicode-segmentation = "1.0.0" regex = "0.2" term = "0.4" diff --git a/src/bin/cargo-fmt.rs b/src/bin/cargo-fmt.rs index ea747aa4702..442cb9e78f7 100644 --- a/src/bin/cargo-fmt.rs +++ b/src/bin/cargo-fmt.rs @@ -14,7 +14,7 @@ #![deny(warnings)] extern crate getopts; -extern crate rustc_serialize; +extern crate serde_json as json; use std::env; use std::io::Write; @@ -24,8 +24,9 @@ use std::collections::HashSet; use std::iter::FromIterator; +use json::Value; + use getopts::{Options, Matches}; -use rustc_serialize::json::Json; fn main() { let exit_status = execute(); @@ -184,8 +185,9 @@ fn get_targets(workspace_hitlist: WorkspaceHitlist) -> Result, std:: if output.status.success() { // None of the unwraps should fail if output of `cargo read-manifest` is correct let data = &String::from_utf8(output.stdout).unwrap(); - let json = Json::from_str(data).unwrap(); - let jtargets = json.find("targets").unwrap().as_array().unwrap(); + let json: Value = json::from_str(data).unwrap(); + let json_obj = json.as_object().unwrap(); + let jtargets = json_obj.get("targets").unwrap().as_array().unwrap(); for jtarget in jtargets { targets.push(target_from_json(jtarget)); } @@ -205,13 +207,15 @@ fn get_targets(workspace_hitlist: WorkspaceHitlist) -> Result, std:: .output()?; if output.status.success() { let data = &String::from_utf8(output.stdout).unwrap(); - let json = Json::from_str(data).unwrap(); + let json: Value = json::from_str(data).unwrap(); + let json_obj = json.as_object().unwrap(); let mut hitlist: HashSet<&String> = if workspace_hitlist != WorkspaceHitlist::All { HashSet::from_iter(workspace_hitlist.get_some().unwrap()) } else { HashSet::new() // Unused }; - let members: Vec<&Json> = json.find("packages") + let members: Vec<&Value> = json_obj + .get("packages") .unwrap() .as_array() .unwrap() @@ -219,7 +223,8 @@ fn get_targets(workspace_hitlist: WorkspaceHitlist) -> Result, std:: .filter(|member| if workspace_hitlist == WorkspaceHitlist::All { true } else { - let member_name = member.find("name").unwrap().as_string().unwrap(); + let member_obj = member.as_object().unwrap(); + let member_name = member_obj.get("name").unwrap().as_str().unwrap(); hitlist.take(&member_name.to_string()).is_some() }) .collect(); @@ -230,7 +235,8 @@ fn get_targets(workspace_hitlist: WorkspaceHitlist) -> Result, std:: hitlist.iter().next().unwrap()))); } for member in members { - let jtargets = member.find("targets").unwrap().as_array().unwrap(); + let member_obj = member.as_object().unwrap(); + let jtargets = member_obj.get("targets").unwrap().as_array().unwrap(); for jtarget in jtargets { targets.push(target_from_json(jtarget)); } @@ -242,11 +248,11 @@ fn get_targets(workspace_hitlist: WorkspaceHitlist) -> Result, std:: } -fn target_from_json(jtarget: &Json) -> Target { +fn target_from_json(jtarget: &Value) -> Target { let jtarget = jtarget.as_object().unwrap(); - let path = PathBuf::from(jtarget.get("src_path").unwrap().as_string().unwrap()); + let path = PathBuf::from(jtarget.get("src_path").unwrap().as_str().unwrap()); let kinds = jtarget.get("kind").unwrap().as_array().unwrap(); - let kind = match kinds[0].as_string().unwrap() { + let kind = match kinds[0].as_str().unwrap() { "bin" => TargetKind::Bin, "lib" | "dylib" | "staticlib" | "cdylib" | "rlib" => TargetKind::Lib, "test" => TargetKind::Test, diff --git a/src/config.rs b/src/config.rs index bc62c9d3dac..f0f2d529308 100644 --- a/src/config.rs +++ b/src/config.rs @@ -210,7 +210,7 @@ pub fn default(&self) -> &'static str { macro_rules! create_config { ($($i:ident: $ty:ty, $def:expr, $( $dstring:expr ),+ );+ $(;)*) => ( - #[derive(RustcDecodable, Clone)] + #[derive(Deserialize, Clone)] pub struct Config { $(pub $i: $ty),+ } @@ -220,7 +220,7 @@ pub struct Config { // specity all properties of `Config`. // We first parse into `ParsedConfig`, then create a default `Config` // and overwrite the properties with corresponding values from `ParsedConfig` - #[derive(RustcDecodable, Clone)] + #[derive(Deserialize, Clone)] pub struct ParsedConfig { $(pub $i: Option<$ty>),+ } @@ -250,10 +250,10 @@ pub fn from_toml(toml: &str) -> Result { } } } - match toml::decode(parsed) { - Some(parsed_config) => + match parsed.try_into() { + Ok(parsed_config) => Ok(Config::default().fill_from_parsed_config(parsed_config)), - None => { + Err(_) => { err.push_str("Error: Decoding config file failed. "); err.push_str("Please check your config file.\n"); Err(err) diff --git a/src/file_lines.rs b/src/file_lines.rs index f11a0aaf6e2..2fad297a4df 100644 --- a/src/file_lines.rs +++ b/src/file_lines.rs @@ -14,12 +14,12 @@ use itertools::Itertools; use multimap::MultiMap; -use rustc_serialize::{self, json}; +use serde_json as json; use codemap::LineRange; /// A range that is inclusive of both ends. -#[derive(Clone, Copy, Debug, Eq, PartialEq, RustcDecodable)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize)] struct Range { pub lo: usize, pub hi: usize, @@ -177,14 +177,14 @@ impl str::FromStr for FileLines { type Err = String; fn from_str(s: &str) -> Result { - let v: Vec = try!(json::decode(s).map_err(|e| e.to_string())); + let v: Vec = try!(json::from_str(s).map_err(|e| e.to_string())); let m = try!(v.into_iter().map(JsonSpan::into_tuple).collect()); Ok(FileLines::from_multimap(m)) } } // For JSON decoding. -#[derive(Clone, Debug, RustcDecodable)] +#[derive(Clone, Debug, Deserialize)] struct JsonSpan { file: String, range: (usize, usize), @@ -202,8 +202,10 @@ fn into_tuple(self) -> Result<(String, Range), String> { // This impl is needed for inclusion in the `Config` struct. We don't have a toml representation // for `FileLines`, so it will just panic instead. -impl rustc_serialize::Decodable for FileLines { - fn decode(_: &mut D) -> Result { +impl<'de> ::serde::de::Deserialize<'de> for FileLines { + fn deserialize(_: D) -> Result + where D: ::serde::de::Deserializer<'de> + { panic!("FileLines cannot be deserialized from a project rustfmt.toml file: please \ specify it via the `--file-lines` option instead"); } diff --git a/src/lib.rs b/src/lib.rs index f7383fbc41e..6935f0f7acd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,9 +15,13 @@ #[macro_use] extern crate log; +extern crate serde; +#[macro_use] +extern crate serde_derive; +extern crate serde_json; + extern crate syntex_syntax as syntax; extern crate syntex_errors as errors; -extern crate rustc_serialize; extern crate strings; diff --git a/src/utils.rs b/src/utils.rs index 1b1ef99955e..efcf0f1b3e3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -193,16 +193,32 @@ pub fn trim_newlines(input: &str) -> &str { #[macro_export] macro_rules! impl_enum_decodable { ( $e:ident, $( $x:ident ),* ) => { - impl ::rustc_serialize::Decodable for $e { - fn decode(d: &mut D) -> Result { + impl<'de> ::serde::de::Deserialize<'de> for $e { + fn deserialize(d: D) -> Result + where D: ::serde::Deserializer<'de> { use std::ascii::AsciiExt; - let s = try!(d.read_str()); + use serde::de::{Error, Visitor}; + use std::marker::PhantomData; + use std::fmt; + struct StringOnly(PhantomData); + impl<'de, T> Visitor<'de> for StringOnly + where T: ::serde::Deserializer<'de> { + type Value = String; + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("string") + } + fn visit_str(self, value: &str) -> Result { + Ok(String::from(value)) + } + } + let s = try!(d.deserialize_string(StringOnly::(PhantomData))); $( if stringify!($x).eq_ignore_ascii_case(&s) { return Ok($e::$x); } )* - Err(d.error("Bad variant")) + static ALLOWED: &'static[&str] = &[$(stringify!($x),)*]; + Err(D::Error::unknown_variant(&s, ALLOWED)) } }