@@ -10,7 +10,7 @@ use std::io::{self, Cursor};
10
10
use std:: result;
11
11
12
12
use curl:: easy:: { Easy , List } ;
13
- use rustc_serialize:: json;
13
+ use rustc_serialize:: json:: { self , Json } ;
14
14
15
15
use url:: percent_encoding:: { percent_encode, QUERY_ENCODE_SET } ;
16
16
@@ -39,6 +39,7 @@ pub enum Error {
39
39
NotFound ,
40
40
JsonEncodeError ( json:: EncoderError ) ,
41
41
JsonDecodeError ( json:: DecoderError ) ,
42
+ JsonParseError ( json:: ParserError ) ,
42
43
}
43
44
44
45
impl From < json:: EncoderError > for Error {
@@ -53,6 +54,12 @@ impl From<json::DecoderError> for Error {
53
54
}
54
55
}
55
56
57
+ impl From < json:: ParserError > for Error {
58
+ fn from ( err : json:: ParserError ) -> Error {
59
+ Error :: JsonParseError ( err)
60
+ }
61
+ }
62
+
56
63
impl From < curl:: Error > for Error {
57
64
fn from ( err : curl:: Error ) -> Error {
58
65
Error :: Curl ( err)
@@ -111,7 +118,6 @@ pub struct User {
111
118
#[ derive( RustcDecodable ) ] struct Users { users : Vec < User > }
112
119
#[ derive( RustcDecodable ) ] struct TotalCrates { total : u32 }
113
120
#[ derive( RustcDecodable ) ] struct Crates { crates : Vec < Crate > , meta : TotalCrates }
114
-
115
121
impl Registry {
116
122
pub fn new ( host : String , token : Option < String > ) -> Registry {
117
123
Registry :: new_handle ( host, token, Easy :: new ( ) )
@@ -148,7 +154,8 @@ impl Registry {
148
154
Ok ( json:: decode :: < Users > ( & body) ?. users )
149
155
}
150
156
151
- pub fn publish ( & mut self , krate : & NewCrate , tarball : & File ) -> Result < ( ) > {
157
+ pub fn publish ( & mut self , krate : & NewCrate , tarball : & File )
158
+ -> Result < Vec < String > > {
152
159
let json = json:: encode ( krate) ?;
153
160
// Prepare the body. The format of the upload request is:
154
161
//
@@ -191,10 +198,20 @@ impl Registry {
191
198
headers. append ( & format ! ( "Authorization: {}" , token) ) ?;
192
199
self . handle . http_headers ( headers) ?;
193
200
194
- let _body = handle ( & mut self . handle , & mut |buf| {
201
+ let body = handle ( & mut self . handle , & mut |buf| {
195
202
body. read ( buf) . unwrap_or ( 0 )
196
203
} ) ?;
197
- Ok ( ( ) )
204
+ // Can't derive RustcDecodable because JSON has a key named "crate" :(
205
+ let response = Json :: from_str ( & body) ?;
206
+ let invalid_categories: Vec < String > =
207
+ response
208
+ . find_path ( & [ "warnings" , "invalid_categories" ] )
209
+ . and_then ( Json :: as_array)
210
+ . map ( |x| {
211
+ x. iter ( ) . flat_map ( Json :: as_string) . map ( Into :: into) . collect ( )
212
+ } )
213
+ . unwrap_or_else ( Vec :: new) ;
214
+ Ok ( invalid_categories)
198
215
}
199
216
200
217
pub fn search ( & mut self , query : & str , limit : u8 ) -> Result < ( Vec < Crate > , u32 ) > {
@@ -329,6 +346,7 @@ impl fmt::Display for Error {
329
346
Error :: NotFound => write ! ( f, "cannot find crate" ) ,
330
347
Error :: JsonEncodeError ( ref e) => write ! ( f, "json encode error: {}" , e) ,
331
348
Error :: JsonDecodeError ( ref e) => write ! ( f, "json decode error: {}" , e) ,
349
+ Error :: JsonParseError ( ref e) => write ! ( f, "json parse error: {}" , e) ,
332
350
}
333
351
}
334
352
}
0 commit comments