@@ -7,7 +7,7 @@ use std::io::prelude::*;
7
7
use std:: io:: { Cursor , SeekFrom } ;
8
8
use std:: time:: Instant ;
9
9
10
- use anyhow:: { bail , format_err, Context , Result } ;
10
+ use anyhow:: { format_err, Context } ;
11
11
use curl:: easy:: { Easy , List } ;
12
12
use http_auth:: ChallengeParser ;
13
13
use percent_encoding:: { percent_encode, NON_ALPHANUMERIC } ;
@@ -200,7 +200,25 @@ impl fmt::Display for Error {
200
200
201
201
impl From < curl:: Error > for Error {
202
202
fn from ( error : curl:: Error ) -> Self {
203
- Error :: Curl ( error)
203
+ Self :: Curl ( error)
204
+ }
205
+ }
206
+
207
+ impl From < anyhow:: Error > for Error {
208
+ fn from ( error : anyhow:: Error ) -> Self {
209
+ Self :: Other ( error)
210
+ }
211
+ }
212
+
213
+ impl From < serde_json:: Error > for Error {
214
+ fn from ( error : serde_json:: Error ) -> Self {
215
+ Self :: Other ( error. into ( ) )
216
+ }
217
+ }
218
+
219
+ impl From < url:: ParseError > for Error {
220
+ fn from ( error : url:: ParseError ) -> Self {
221
+ Self :: Other ( error. into ( ) )
204
222
}
205
223
}
206
224
@@ -236,10 +254,12 @@ impl Registry {
236
254
self . token = token;
237
255
}
238
256
239
- fn token ( & self ) -> Result < & str > {
257
+ fn token ( & self ) -> Result < & str , Error > {
240
258
let token = match self . token . as_ref ( ) {
241
259
Some ( s) => s,
242
- None => bail ! ( "no upload token found, please run `cargo login`" ) ,
260
+ None => {
261
+ return Err ( format_err ! ( "no upload token found, please run `cargo login`" ) . into ( ) )
262
+ }
243
263
} ;
244
264
check_token ( token) ?;
245
265
Ok ( token)
@@ -253,26 +273,26 @@ impl Registry {
253
273
is_url_crates_io ( & self . host )
254
274
}
255
275
256
- pub fn add_owners ( & mut self , krate : & str , owners : & [ & str ] ) -> Result < String > {
276
+ pub fn add_owners ( & mut self , krate : & str , owners : & [ & str ] ) -> Result < String , Error > {
257
277
let body = serde_json:: to_string ( & OwnersReq { users : owners } ) ?;
258
278
let body = self . put ( & format ! ( "/crates/{}/owners" , krate) , body. as_bytes ( ) ) ?;
259
279
assert ! ( serde_json:: from_str:: <OwnerResponse >( & body) ?. ok) ;
260
280
Ok ( serde_json:: from_str :: < OwnerResponse > ( & body) ?. msg )
261
281
}
262
282
263
- pub fn remove_owners ( & mut self , krate : & str , owners : & [ & str ] ) -> Result < ( ) > {
283
+ pub fn remove_owners ( & mut self , krate : & str , owners : & [ & str ] ) -> Result < ( ) , Error > {
264
284
let body = serde_json:: to_string ( & OwnersReq { users : owners } ) ?;
265
285
let body = self . delete ( & format ! ( "/crates/{}/owners" , krate) , Some ( body. as_bytes ( ) ) ) ?;
266
286
assert ! ( serde_json:: from_str:: <OwnerResponse >( & body) ?. ok) ;
267
287
Ok ( ( ) )
268
288
}
269
289
270
- pub fn list_owners ( & mut self , krate : & str ) -> Result < Vec < User > > {
290
+ pub fn list_owners ( & mut self , krate : & str ) -> Result < Vec < User > , Error > {
271
291
let body = self . get ( & format ! ( "/crates/{}/owners" , krate) ) ?;
272
292
Ok ( serde_json:: from_str :: < Users > ( & body) ?. users )
273
293
}
274
294
275
- pub fn publish ( & mut self , krate : & NewCrate , mut tarball : & File ) -> Result < Warnings > {
295
+ pub fn publish ( & mut self , krate : & NewCrate , mut tarball : & File ) -> Result < Warnings , Error > {
276
296
let json = serde_json:: to_string ( krate) ?;
277
297
// Prepare the body. The format of the upload request is:
278
298
//
@@ -366,7 +386,7 @@ impl Registry {
366
386
} )
367
387
}
368
388
369
- pub fn search ( & mut self , query : & str , limit : u32 ) -> Result < ( Vec < Crate > , u32 ) > {
389
+ pub fn search ( & mut self , query : & str , limit : u32 ) -> Result < ( Vec < Crate > , u32 ) , Error > {
370
390
let formatted_query = percent_encode ( query. as_bytes ( ) , NON_ALPHANUMERIC ) ;
371
391
let body = self . req (
372
392
& format ! ( "/crates?q={}&per_page={}" , formatted_query, limit) ,
@@ -378,34 +398,34 @@ impl Registry {
378
398
Ok ( ( crates. crates , crates. meta . total ) )
379
399
}
380
400
381
- pub fn yank ( & mut self , krate : & str , version : & str ) -> Result < ( ) > {
401
+ pub fn yank ( & mut self , krate : & str , version : & str ) -> Result < ( ) , Error > {
382
402
let body = self . delete ( & format ! ( "/crates/{}/{}/yank" , krate, version) , None ) ?;
383
403
assert ! ( serde_json:: from_str:: <R >( & body) ?. ok) ;
384
404
Ok ( ( ) )
385
405
}
386
406
387
- pub fn unyank ( & mut self , krate : & str , version : & str ) -> Result < ( ) > {
407
+ pub fn unyank ( & mut self , krate : & str , version : & str ) -> Result < ( ) , Error > {
388
408
let body = self . put ( & format ! ( "/crates/{}/{}/unyank" , krate, version) , & [ ] ) ?;
389
409
assert ! ( serde_json:: from_str:: <R >( & body) ?. ok) ;
390
410
Ok ( ( ) )
391
411
}
392
412
393
- fn put ( & mut self , path : & str , b : & [ u8 ] ) -> Result < String > {
413
+ fn put ( & mut self , path : & str , b : & [ u8 ] ) -> Result < String , Error > {
394
414
self . handle . put ( true ) ?;
395
415
self . req ( path, Some ( b) , Auth :: Authorized )
396
416
}
397
417
398
- fn get ( & mut self , path : & str ) -> Result < String > {
418
+ fn get ( & mut self , path : & str ) -> Result < String , Error > {
399
419
self . handle . get ( true ) ?;
400
420
self . req ( path, None , Auth :: Authorized )
401
421
}
402
422
403
- fn delete ( & mut self , path : & str , b : Option < & [ u8 ] > ) -> Result < String > {
423
+ fn delete ( & mut self , path : & str , b : Option < & [ u8 ] > ) -> Result < String , Error > {
404
424
self . handle . custom_request ( "DELETE" ) ?;
405
425
self . req ( path, b, Auth :: Authorized )
406
426
}
407
427
408
- fn req ( & mut self , path : & str , body : Option < & [ u8 ] > , authorized : Auth ) -> Result < String > {
428
+ fn req ( & mut self , path : & str , body : Option < & [ u8 ] > , authorized : Auth ) -> Result < String , Error > {
409
429
self . handle . url ( & format ! ( "{}/api/v1{}" , self . host, path) ) ?;
410
430
let mut headers = List :: new ( ) ;
411
431
headers. append ( "Accept: application/json" ) ?;
@@ -426,10 +446,7 @@ impl Registry {
426
446
}
427
447
}
428
448
429
- fn handle (
430
- & mut self ,
431
- read : & mut dyn FnMut ( & mut [ u8 ] ) -> usize ,
432
- ) -> std:: result:: Result < String , Error > {
449
+ fn handle ( & mut self , read : & mut dyn FnMut ( & mut [ u8 ] ) -> usize ) -> Result < String , Error > {
433
450
let mut headers = Vec :: new ( ) ;
434
451
let mut body = Vec :: new ( ) ;
435
452
{
@@ -604,9 +621,9 @@ pub fn is_url_crates_io(url: &str) -> bool {
604
621
/// This check is necessary to prevent sending tokens which create an invalid HTTP request.
605
622
/// It would be easier to check just for alphanumeric tokens, but we can't be sure that all
606
623
/// registries only create tokens in that format so that is as less restricted as possible.
607
- pub fn check_token ( token : & str ) -> Result < ( ) > {
624
+ pub fn check_token ( token : & str ) -> Result < ( ) , Error > {
608
625
if token. is_empty ( ) {
609
- bail ! ( "please provide a non-empty token" ) ;
626
+ return Err ( format_err ! ( "please provide a non-empty token" ) . into ( ) ) ;
610
627
}
611
628
if token. bytes ( ) . all ( |b| {
612
629
// This is essentially the US-ASCII limitation of
@@ -617,10 +634,11 @@ pub fn check_token(token: &str) -> Result<()> {
617
634
} ) {
618
635
Ok ( ( ) )
619
636
} else {
620
- Err ( anyhow :: anyhow !(
637
+ Err ( format_err ! (
621
638
"token contains invalid characters.\n Only printable ISO-8859-1 characters \
622
639
are allowed as it is sent in a HTTPS header."
623
- ) )
640
+ )
641
+ . into ( ) )
624
642
}
625
643
}
626
644
0 commit comments