@@ -7,12 +7,14 @@ 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 percent_encoding:: { percent_encode, NON_ALPHANUMERIC } ;
13
13
use serde:: { Deserialize , Serialize } ;
14
14
use url:: Url ;
15
15
16
+ pub type Result < T > = std:: result:: Result < T , Error > ;
17
+
16
18
pub struct Registry {
17
19
/// The base URL for issuing API requests.
18
20
host : String ,
@@ -189,6 +191,24 @@ impl From<curl::Error> for Error {
189
191
}
190
192
}
191
193
194
+ impl From < anyhow:: Error > for Error {
195
+ fn from ( error : anyhow:: Error ) -> Self {
196
+ Self :: Other ( error)
197
+ }
198
+ }
199
+
200
+ impl From < serde_json:: Error > for Error {
201
+ fn from ( error : serde_json:: Error ) -> Self {
202
+ Self :: Other ( error. into ( ) )
203
+ }
204
+ }
205
+
206
+ impl From < url:: ParseError > for Error {
207
+ fn from ( error : url:: ParseError ) -> Self {
208
+ Self :: Other ( error. into ( ) )
209
+ }
210
+ }
211
+
192
212
impl Registry {
193
213
/// Creates a new `Registry`.
194
214
///
@@ -224,7 +244,9 @@ impl Registry {
224
244
fn token ( & self ) -> Result < & str > {
225
245
let token = match self . token . as_ref ( ) {
226
246
Some ( s) => s,
227
- None => bail ! ( "no upload token found, please run `cargo login`" ) ,
247
+ None => {
248
+ return Err ( format_err ! ( "no upload token found, please run `cargo login`" ) . into ( ) )
249
+ }
228
250
} ;
229
251
check_token ( token) ?;
230
252
Ok ( token)
@@ -411,10 +433,7 @@ impl Registry {
411
433
}
412
434
}
413
435
414
- fn handle (
415
- & mut self ,
416
- read : & mut dyn FnMut ( & mut [ u8 ] ) -> usize ,
417
- ) -> std:: result:: Result < String , Error > {
436
+ fn handle ( & mut self , read : & mut dyn FnMut ( & mut [ u8 ] ) -> usize ) -> Result < String > {
418
437
let mut headers = Vec :: new ( ) ;
419
438
let mut body = Vec :: new ( ) ;
420
439
{
@@ -529,7 +548,7 @@ pub fn is_url_crates_io(url: &str) -> bool {
529
548
/// registries only create tokens in that format so that is as less restricted as possible.
530
549
pub fn check_token ( token : & str ) -> Result < ( ) > {
531
550
if token. is_empty ( ) {
532
- bail ! ( "please provide a non-empty token" ) ;
551
+ return Err ( format_err ! ( "please provide a non-empty token" ) . into ( ) ) ;
533
552
}
534
553
if token. bytes ( ) . all ( |b| {
535
554
// This is essentially the US-ASCII limitation of
@@ -540,9 +559,10 @@ pub fn check_token(token: &str) -> Result<()> {
540
559
} ) {
541
560
Ok ( ( ) )
542
561
} else {
543
- Err ( anyhow :: anyhow !(
562
+ Err ( format_err ! (
544
563
"token contains invalid characters.\n Only printable ISO-8859-1 characters \
545
564
are allowed as it is sent in a HTTPS header."
546
- ) )
565
+ )
566
+ . into ( ) )
547
567
}
548
568
}
0 commit comments