Skip to content

Commit a561e8b

Browse files
committed
feat(crates-io): use our own Result wrapper everywhere
1 parent ed6692b commit a561e8b

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

crates/crates-io/lib.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ use std::io::prelude::*;
77
use std::io::{Cursor, SeekFrom};
88
use std::time::Instant;
99

10-
use anyhow::{bail, format_err, Context, Result};
10+
use anyhow::{format_err, Context};
1111
use curl::easy::{Easy, List};
1212
use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
1313
use serde::{Deserialize, Serialize};
1414
use url::Url;
1515

16+
pub type Result<T> = std::result::Result<T, Error>;
17+
1618
pub struct Registry {
1719
/// The base URL for issuing API requests.
1820
host: String,
@@ -189,6 +191,24 @@ impl From<curl::Error> for Error {
189191
}
190192
}
191193

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+
192212
impl Registry {
193213
/// Creates a new `Registry`.
194214
///
@@ -224,7 +244,9 @@ impl Registry {
224244
fn token(&self) -> Result<&str> {
225245
let token = match self.token.as_ref() {
226246
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+
}
228250
};
229251
check_token(token)?;
230252
Ok(token)
@@ -411,10 +433,7 @@ impl Registry {
411433
}
412434
}
413435

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> {
418437
let mut headers = Vec::new();
419438
let mut body = Vec::new();
420439
{
@@ -529,7 +548,7 @@ pub fn is_url_crates_io(url: &str) -> bool {
529548
/// registries only create tokens in that format so that is as less restricted as possible.
530549
pub fn check_token(token: &str) -> Result<()> {
531550
if token.is_empty() {
532-
bail!("please provide a non-empty token");
551+
return Err(format_err!("please provide a non-empty token").into());
533552
}
534553
if token.bytes().all(|b| {
535554
// This is essentially the US-ASCII limitation of
@@ -540,9 +559,10 @@ pub fn check_token(token: &str) -> Result<()> {
540559
}) {
541560
Ok(())
542561
} else {
543-
Err(anyhow::anyhow!(
562+
Err(format_err!(
544563
"token contains invalid characters.\nOnly printable ISO-8859-1 characters \
545564
are allowed as it is sent in a HTTPS header."
546-
))
565+
)
566+
.into())
547567
}
548568
}

0 commit comments

Comments
 (0)