Skip to content

Commit

Permalink
Make warnings about invalid crate names be JSON instead of text
Browse files Browse the repository at this point in the history
And cargo will handle making nice English messages out of them.
  • Loading branch information
carols10cents committed Dec 5, 2016
1 parent b69fa22 commit 1b04800
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
13 changes: 7 additions & 6 deletions src/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,11 +745,6 @@ pub fn new(req: &mut Request) -> CargoResult<Response> {
let ignored_invalid_categories = try!(
Category::update_crate(try!(req.tx()), &krate, &categories)
);
let warnings: Vec<String> =
ignored_invalid_categories.iter().map(|category| {
format!("'{}' is not a recognized category name \
and has been ignored.", category)
}).collect();

// Upload the crate to S3
let mut handle = req.app().handle();
Expand Down Expand Up @@ -806,7 +801,13 @@ pub fn new(req: &mut Request) -> CargoResult<Response> {
bomb.path = None;

#[derive(RustcEncodable)]
struct R { krate: EncodableCrate, warnings: Vec<String> }
struct Warnings { invalid_categories: Vec<String> }
let warnings = Warnings {
invalid_categories: ignored_invalid_categories,
};

#[derive(RustcEncodable)]
struct R { krate: EncodableCrate, warnings: Warnings }
Ok(req.json(&R { krate: krate.minimal_encodable(), warnings: warnings }))
}

Expand Down
9 changes: 5 additions & 4 deletions src/tests/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ struct CrateMeta { total: i32 }
#[derive(RustcDecodable)]
struct GitCrate { name: String, vers: String, deps: Vec<String>, cksum: String }
#[derive(RustcDecodable)]
struct GoodCrate { krate: EncodableCrate, warnings: Vec<String> }
struct Warnings { invalid_categories: Vec<String> }
#[derive(RustcDecodable)]
struct GoodCrate { krate: EncodableCrate, warnings: Warnings }
#[derive(RustcDecodable)]
struct CrateResponse { krate: EncodableCrate, versions: Vec<EncodableVersion>, keywords: Vec<EncodableKeyword> }
#[derive(RustcDecodable)]
Expand Down Expand Up @@ -896,7 +898,7 @@ fn good_categories() {
let json: GoodCrate = ::json(&mut response);
assert_eq!(json.krate.name, "foo");
assert_eq!(json.krate.max_version, "1.0.0");
assert_eq!(json.warnings.len(), 0);
assert_eq!(json.warnings.invalid_categories.len(), 0);
}

#[test]
Expand All @@ -910,8 +912,7 @@ fn ignored_categories() {
let json: GoodCrate = ::json(&mut response);
assert_eq!(json.krate.name, "foo");
assert_eq!(json.krate.max_version, "1.0.0");
assert_eq!(json.warnings, vec!["\'bar\' is not a recognized category name \
and has been ignored.".to_string()]);
assert_eq!(json.warnings.invalid_categories, vec!["bar".to_string()]);
}

#[test]
Expand Down

0 comments on commit 1b04800

Please sign in to comment.