Skip to content

Commit 9409586

Browse files
committed
feat(crates-io): use std::result::Result everywhere
1 parent 9b307f7 commit 9409586

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

crates/crates-io/lib.rs

+41-23
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ 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 http_auth::ChallengeParser;
1313
use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
@@ -200,7 +200,25 @@ impl fmt::Display for Error {
200200

201201
impl From<curl::Error> for Error {
202202
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())
204222
}
205223
}
206224

@@ -236,10 +254,12 @@ impl Registry {
236254
self.token = token;
237255
}
238256

239-
fn token(&self) -> Result<&str> {
257+
fn token(&self) -> Result<&str, Error> {
240258
let token = match self.token.as_ref() {
241259
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+
}
243263
};
244264
check_token(token)?;
245265
Ok(token)
@@ -253,26 +273,26 @@ impl Registry {
253273
is_url_crates_io(&self.host)
254274
}
255275

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> {
257277
let body = serde_json::to_string(&OwnersReq { users: owners })?;
258278
let body = self.put(&format!("/crates/{}/owners", krate), body.as_bytes())?;
259279
assert!(serde_json::from_str::<OwnerResponse>(&body)?.ok);
260280
Ok(serde_json::from_str::<OwnerResponse>(&body)?.msg)
261281
}
262282

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> {
264284
let body = serde_json::to_string(&OwnersReq { users: owners })?;
265285
let body = self.delete(&format!("/crates/{}/owners", krate), Some(body.as_bytes()))?;
266286
assert!(serde_json::from_str::<OwnerResponse>(&body)?.ok);
267287
Ok(())
268288
}
269289

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> {
271291
let body = self.get(&format!("/crates/{}/owners", krate))?;
272292
Ok(serde_json::from_str::<Users>(&body)?.users)
273293
}
274294

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> {
276296
let json = serde_json::to_string(krate)?;
277297
// Prepare the body. The format of the upload request is:
278298
//
@@ -366,7 +386,7 @@ impl Registry {
366386
})
367387
}
368388

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> {
370390
let formatted_query = percent_encode(query.as_bytes(), NON_ALPHANUMERIC);
371391
let body = self.req(
372392
&format!("/crates?q={}&per_page={}", formatted_query, limit),
@@ -378,34 +398,34 @@ impl Registry {
378398
Ok((crates.crates, crates.meta.total))
379399
}
380400

381-
pub fn yank(&mut self, krate: &str, version: &str) -> Result<()> {
401+
pub fn yank(&mut self, krate: &str, version: &str) -> Result<(), Error> {
382402
let body = self.delete(&format!("/crates/{}/{}/yank", krate, version), None)?;
383403
assert!(serde_json::from_str::<R>(&body)?.ok);
384404
Ok(())
385405
}
386406

387-
pub fn unyank(&mut self, krate: &str, version: &str) -> Result<()> {
407+
pub fn unyank(&mut self, krate: &str, version: &str) -> Result<(), Error> {
388408
let body = self.put(&format!("/crates/{}/{}/unyank", krate, version), &[])?;
389409
assert!(serde_json::from_str::<R>(&body)?.ok);
390410
Ok(())
391411
}
392412

393-
fn put(&mut self, path: &str, b: &[u8]) -> Result<String> {
413+
fn put(&mut self, path: &str, b: &[u8]) -> Result<String, Error> {
394414
self.handle.put(true)?;
395415
self.req(path, Some(b), Auth::Authorized)
396416
}
397417

398-
fn get(&mut self, path: &str) -> Result<String> {
418+
fn get(&mut self, path: &str) -> Result<String, Error> {
399419
self.handle.get(true)?;
400420
self.req(path, None, Auth::Authorized)
401421
}
402422

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> {
404424
self.handle.custom_request("DELETE")?;
405425
self.req(path, b, Auth::Authorized)
406426
}
407427

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> {
409429
self.handle.url(&format!("{}/api/v1{}", self.host, path))?;
410430
let mut headers = List::new();
411431
headers.append("Accept: application/json")?;
@@ -426,10 +446,7 @@ impl Registry {
426446
}
427447
}
428448

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> {
433450
let mut headers = Vec::new();
434451
let mut body = Vec::new();
435452
{
@@ -604,9 +621,9 @@ pub fn is_url_crates_io(url: &str) -> bool {
604621
/// This check is necessary to prevent sending tokens which create an invalid HTTP request.
605622
/// It would be easier to check just for alphanumeric tokens, but we can't be sure that all
606623
/// 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> {
608625
if token.is_empty() {
609-
bail!("please provide a non-empty token");
626+
return Err(format_err!("please provide a non-empty token").into());
610627
}
611628
if token.bytes().all(|b| {
612629
// This is essentially the US-ASCII limitation of
@@ -617,10 +634,11 @@ pub fn check_token(token: &str) -> Result<()> {
617634
}) {
618635
Ok(())
619636
} else {
620-
Err(anyhow::anyhow!(
637+
Err(format_err!(
621638
"token contains invalid characters.\nOnly printable ISO-8859-1 characters \
622639
are allowed as it is sent in a HTTPS header."
623-
))
640+
)
641+
.into())
624642
}
625643
}
626644

0 commit comments

Comments
 (0)