Skip to content

Commit

Permalink
Auto merge of #3682 - alexcrichton:serde, r=brson
Browse files Browse the repository at this point in the history
Migrate from rustc-serialize to Serde

This commit migrates Cargo as much as possible from rustc-serialize to
Serde. This not only provides an excellent testing ground for the toml
0.3 release but it also is a big boost to the speed of parsing the JSON
bits of the registry.

This doesn't completely excise the dependency just yet as docopt still
requires it along with handlebars. I'm sure though that in time those
crates will migrate to serde!
  • Loading branch information
bors committed Feb 22, 2017
2 parents e278ea3 + a5a298f commit be0b499
Show file tree
Hide file tree
Showing 35 changed files with 673 additions and 564 deletions.
101 changes: 98 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ log = "0.3"
num_cpus = "1.0"
rustc-serialize = "0.3"
semver = "0.6.0"
serde = "0.9"
serde_derive = "0.9"
serde_json = "0.9"
serde_ignored = "0.0.2"
shell-escape = "0.1"
tar = { version = "0.4", default-features = false }
tempdir = "0.3"
term = "0.4.4"
toml = "0.2"
toml = "0.3"
url = "1.1"

[target.'cfg(unix)'.dependencies]
Expand Down
3 changes: 3 additions & 0 deletions src/bin/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ extern crate rustc_serialize;
extern crate toml;
#[macro_use]
extern crate log;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;

use std::collections::BTreeSet;
use std::collections::HashMap;
Expand Down
2 changes: 1 addition & 1 deletion src/bin/locate_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Options:
-h, --help Print this message
";

#[derive(RustcEncodable)]
#[derive(Serialize)]
pub struct ProjectLocation {
root: String
}
Expand Down
11 changes: 5 additions & 6 deletions src/bin/verify_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::process;
use cargo;
use cargo::util::important_paths::{find_root_manifest_for_wd};
use cargo::util::{CliResult, Config};
use rustc_serialize::json;
use serde_json;
use toml;

#[derive(RustcDecodable)]
Expand Down Expand Up @@ -55,10 +55,9 @@ pub fn execute(args: Flags, config: &Config) -> CliResult {
Ok(_) => {},
Err(e) => fail("invalid", &format!("error reading file: {}", e))
};
match toml::Parser::new(&contents).parse() {
None => fail("invalid", "invalid-format"),
Some(..) => {}
};
if contents.parse::<toml::Value>().is_err() {
fail("invalid", "invalid-format");
}

let mut h = HashMap::new();
h.insert("success".to_string(), "true".to_string());
Expand All @@ -69,6 +68,6 @@ pub fn execute(args: Flags, config: &Config) -> CliResult {
fn fail(reason: &str, value: &str) -> ! {
let mut h = HashMap::new();
h.insert(reason.to_string(), value.to_string());
println!("{}", json::encode(&h).unwrap());
println!("{}", serde_json::to_string(&h).unwrap());
process::exit(1)
}
30 changes: 18 additions & 12 deletions src/cargo/core/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::str::FromStr;

use semver::VersionReq;
use semver::ReqParseError;
use rustc_serialize::{Encoder, Encodable};
use serde::ser;

use core::{SourceId, Summary, PackageId};
use util::{CargoError, CargoResult, Cfg, CfgExpr, ChainError, human, Config};
Expand Down Expand Up @@ -41,7 +41,7 @@ pub enum Platform {
Cfg(CfgExpr),
}

#[derive(RustcEncodable)]
#[derive(Serialize)]
struct SerializedDependency<'a> {
name: &'a str,
source: &'a SourceId,
Expand All @@ -54,8 +54,10 @@ struct SerializedDependency<'a> {
target: Option<&'a Platform>,
}

impl Encodable for Dependency {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
impl ser::Serialize for Dependency {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where S: ser::Serializer,
{
SerializedDependency {
name: self.name(),
source: &self.source_id(),
Expand All @@ -65,7 +67,7 @@ impl Encodable for Dependency {
uses_default_features: self.uses_default_features(),
features: self.features(),
target: self.platform(),
}.encode(s)
}.serialize(s)
}
}

Expand All @@ -76,13 +78,15 @@ pub enum Kind {
Build,
}

impl Encodable for Kind {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
impl ser::Serialize for Kind {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where S: ser::Serializer,
{
match *self {
Kind::Normal => None,
Kind::Development => Some("dev"),
Kind::Build => Some("build"),
}.encode(s)
}.serialize(s)
}
}

Expand Down Expand Up @@ -136,7 +140,7 @@ This will soon become a hard error, so it's either recommended to
update to a fixed version or contact the upstream maintainer about
this warning.
",
req, inside.name(), inside.version(), requirement);
req, inside.name(), inside.version(), requirement);
config.shell().warn(&msg)?;

Ok(requirement)
Expand Down Expand Up @@ -348,9 +352,11 @@ impl Platform {
}
}

impl Encodable for Platform {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
self.to_string().encode(s)
impl ser::Serialize for Platform {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where S: ser::Serializer,
{
self.to_string().serialize(s)
}
}

Expand Down
Loading

0 comments on commit be0b499

Please sign in to comment.