Skip to content

Commit 535f773

Browse files
committed
refactor: define lockfile errors in cargo-util-schemas
Some of the fields are made public to make the moving phase easier. They will be reverted back to private when its done moving over.
1 parent 982ee85 commit 535f773

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

crates/cargo-util-schemas/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1111
pub mod core;
1212
pub mod index;
13+
pub mod lockfile;
1314
pub mod manifest;
1415
pub mod messages;
1516
#[cfg(feature = "unstable-schema")]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#[derive(Debug, thiserror::Error)]
2+
#[error(transparent)]
3+
pub struct EncodableSourceIdError(#[from] pub EncodableSourceIdErrorKind);
4+
5+
#[non_exhaustive]
6+
#[derive(Debug, thiserror::Error)]
7+
pub enum EncodableSourceIdErrorKind {
8+
#[error("invalid source `{0}`")]
9+
InvalidSource(String),
10+
11+
#[error("invalid url `{url}`: {msg}")]
12+
InvalidUrl { url: String, msg: String },
13+
14+
#[error("unsupported source protocol: {0}")]
15+
UnsupportedSource(String),
16+
}
17+
18+
#[derive(Debug, thiserror::Error)]
19+
#[error(transparent)]
20+
pub struct EncodablePackageIdError(#[from] EncodablePackageIdErrorKind);
21+
22+
impl From<EncodableSourceIdError> for EncodablePackageIdError {
23+
fn from(value: EncodableSourceIdError) -> Self {
24+
EncodablePackageIdErrorKind::Source(value).into()
25+
}
26+
}
27+
28+
#[non_exhaustive]
29+
#[derive(Debug, thiserror::Error)]
30+
pub enum EncodablePackageIdErrorKind {
31+
#[error("invalid serialied PackageId")]
32+
InvalidSerializedPackageId,
33+
34+
#[error(transparent)]
35+
Source(#[from] EncodableSourceIdError),
36+
}

src/cargo/core/resolver/encode.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ use crate::util::interning::InternedString;
118118
use crate::util::{Graph, internal};
119119
use anyhow::{Context as _, bail};
120120
use cargo_util_schemas::core::SourceKind;
121+
use cargo_util_schemas::lockfile::{
122+
EncodablePackageIdError, EncodablePackageIdErrorKind, EncodableSourceIdError,
123+
EncodableSourceIdErrorKind,
124+
};
121125
use serde::de;
122126
use serde::ser;
123127
use serde::{Deserialize, Serialize};
@@ -550,14 +554,16 @@ pub struct EncodableSourceId {
550554
}
551555

552556
impl EncodableSourceId {
553-
pub fn new(source: String) -> CargoResult<Self> {
557+
pub fn new(source: String) -> Result<Self, EncodableSourceIdError> {
554558
let source_str = source.clone();
555-
let (kind, url) = source
556-
.split_once('+')
557-
.ok_or_else(|| anyhow::format_err!("invalid source `{}`", source_str))?;
559+
let (kind, url) = source.split_once('+').ok_or_else(|| {
560+
EncodableSourceIdError(EncodableSourceIdErrorKind::InvalidSource(source.clone()).into())
561+
})?;
558562

559-
let url =
560-
Url::parse(url).map_err(|s| anyhow::format_err!("invalid url `{}`: {}", url, s))?;
563+
let url = Url::parse(url).map_err(|msg| EncodableSourceIdErrorKind::InvalidUrl {
564+
url: url.to_string(),
565+
msg: msg.to_string(),
566+
})?;
561567

562568
let kind = match kind {
563569
"git" => {
@@ -567,7 +573,9 @@ impl EncodableSourceId {
567573
"registry" => SourceKind::Registry,
568574
"sparse" => SourceKind::SparseRegistry,
569575
"path" => SourceKind::Path,
570-
kind => anyhow::bail!("unsupported source protocol: {}", kind),
576+
kind => {
577+
return Err(EncodableSourceIdErrorKind::UnsupportedSource(kind.to_string()).into());
578+
}
571579
};
572580

573581
Ok(Self {
@@ -663,9 +671,9 @@ impl fmt::Display for EncodablePackageId {
663671
}
664672

665673
impl FromStr for EncodablePackageId {
666-
type Err = anyhow::Error;
674+
type Err = EncodablePackageIdError;
667675

668-
fn from_str(s: &str) -> CargoResult<EncodablePackageId> {
676+
fn from_str(s: &str) -> Result<EncodablePackageId, Self::Err> {
669677
let mut s = s.splitn(3, ' ');
670678
let name = s.next().unwrap();
671679
let version = s.next();
@@ -674,7 +682,7 @@ impl FromStr for EncodablePackageId {
674682
if let Some(s) = s.strip_prefix('(').and_then(|s| s.strip_suffix(')')) {
675683
Some(EncodableSourceId::new(s.to_string())?)
676684
} else {
677-
anyhow::bail!("invalid serialized PackageId")
685+
return Err(EncodablePackageIdErrorKind::InvalidSerializedPackageId.into());
678686
}
679687
}
680688
None => None,

0 commit comments

Comments
 (0)