From 1cee6d8cb8b21b8b009fb5a176de9521ee24240a Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Tue, 29 Nov 2016 11:56:00 -0500 Subject: [PATCH] Make warnings about invalid crate names be JSON instead of text And cargo will handle making nice English messages out of them. --- src/krate.rs | 13 +++++++------ src/tests/krate.rs | 9 +++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/krate.rs b/src/krate.rs index 2638c157980..0a3247b04d9 100644 --- a/src/krate.rs +++ b/src/krate.rs @@ -745,11 +745,6 @@ pub fn new(req: &mut Request) -> CargoResult { let ignored_invalid_categories = try!( Category::update_crate(try!(req.tx()), &krate, &categories) ); - let warnings: Vec = - 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(); @@ -808,7 +803,13 @@ pub fn new(req: &mut Request) -> CargoResult { bomb.path = None; #[derive(RustcEncodable)] - struct R { krate: EncodableCrate, warnings: Vec } + struct Warnings { invalid_categories: Vec } + 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 })) } diff --git a/src/tests/krate.rs b/src/tests/krate.rs index 7b05ff4d922..2f9f75653bf 100644 --- a/src/tests/krate.rs +++ b/src/tests/krate.rs @@ -26,7 +26,9 @@ struct CrateMeta { total: i32 } #[derive(RustcDecodable)] struct GitCrate { name: String, vers: String, deps: Vec, cksum: String } #[derive(RustcDecodable)] -struct GoodCrate { krate: EncodableCrate, warnings: Vec } +struct Warnings { invalid_categories: Vec } +#[derive(RustcDecodable)] +struct GoodCrate { krate: EncodableCrate, warnings: Warnings } #[derive(RustcDecodable)] struct CrateResponse { krate: EncodableCrate, versions: Vec, keywords: Vec } #[derive(RustcDecodable)] @@ -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] @@ -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]