Skip to content

Commit 17a4b39

Browse files
authored
Lockfile schemas error cleanup (#16039)
### What does this PR try to resolve? Cleans up some of the error handling in the lockfile schemas in `cargo-util-schemas`. This change includes renaming the error structs to follow the `TomlLockfile` naming scheme of lockfile schemas. ### How to test and review this PR? Commit by commit.
2 parents 5bb56cd + 2232dc2 commit 17a4b39

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,16 @@ pub struct TomlLockfileSourceId {
103103
}
104104

105105
impl TomlLockfileSourceId {
106-
pub fn new(source: String) -> Result<Self, EncodableSourceIdError> {
106+
pub fn new(source: String) -> Result<Self, TomlLockfileSourceIdError> {
107107
let source_str = source.clone();
108-
let (kind, url) = source.split_once('+').ok_or_else(|| {
109-
EncodableSourceIdError(EncodableSourceIdErrorKind::InvalidSource(source.clone()).into())
110-
})?;
108+
let (kind, url) = source
109+
.split_once('+')
110+
.ok_or_else(|| TomlLockfileSourceIdErrorKind::InvalidSource(source.clone()))?;
111111

112112
// Sparse URLs store the kind prefix (sparse+) in the URL. Therefore, for sparse kinds, we
113113
// want to use the raw `source` instead of the splitted `url`.
114114
let url = Url::parse(if kind == "sparse" { &source } else { url }).map_err(|msg| {
115-
EncodableSourceIdErrorKind::InvalidUrl {
115+
TomlLockfileSourceIdErrorKind::InvalidUrl {
116116
url: url.to_string(),
117117
msg: msg.to_string(),
118118
}
@@ -127,7 +127,9 @@ impl TomlLockfileSourceId {
127127
"sparse" => SourceKind::SparseRegistry,
128128
"path" => SourceKind::Path,
129129
kind => {
130-
return Err(EncodableSourceIdErrorKind::UnsupportedSource(kind.to_string()).into());
130+
return Err(
131+
TomlLockfileSourceIdErrorKind::UnsupportedSource(kind.to_string()).into(),
132+
);
131133
}
132134
};
133135

@@ -230,7 +232,7 @@ impl fmt::Display for TomlLockfilePackageId {
230232
}
231233

232234
impl FromStr for TomlLockfilePackageId {
233-
type Err = EncodablePackageIdError;
235+
type Err = TomlLockfilePackageIdError;
234236

235237
fn from_str(s: &str) -> Result<TomlLockfilePackageId, Self::Err> {
236238
let mut s = s.splitn(3, ' ');
@@ -241,7 +243,7 @@ impl FromStr for TomlLockfilePackageId {
241243
if let Some(s) = s.strip_prefix('(').and_then(|s| s.strip_suffix(')')) {
242244
Some(TomlLockfileSourceId::new(s.to_string())?)
243245
} else {
244-
return Err(EncodablePackageIdErrorKind::InvalidSerializedPackageId.into());
246+
return Err(TomlLockfilePackageIdErrorKind::InvalidSerializedPackageId.into());
245247
}
246248
}
247249
None => None,
@@ -279,11 +281,11 @@ impl<'de> de::Deserialize<'de> for TomlLockfilePackageId {
279281

280282
#[derive(Debug, thiserror::Error)]
281283
#[error(transparent)]
282-
pub struct EncodableSourceIdError(#[from] EncodableSourceIdErrorKind);
284+
pub struct TomlLockfileSourceIdError(#[from] TomlLockfileSourceIdErrorKind);
283285

284286
#[non_exhaustive]
285287
#[derive(Debug, thiserror::Error)]
286-
enum EncodableSourceIdErrorKind {
288+
enum TomlLockfileSourceIdErrorKind {
287289
#[error("invalid source `{0}`")]
288290
InvalidSource(String),
289291

@@ -296,22 +298,22 @@ enum EncodableSourceIdErrorKind {
296298

297299
#[derive(Debug, thiserror::Error)]
298300
#[error(transparent)]
299-
pub struct EncodablePackageIdError(#[from] EncodablePackageIdErrorKind);
301+
pub struct TomlLockfilePackageIdError(#[from] TomlLockfilePackageIdErrorKind);
300302

301-
impl From<EncodableSourceIdError> for EncodablePackageIdError {
302-
fn from(value: EncodableSourceIdError) -> Self {
303-
EncodablePackageIdErrorKind::Source(value).into()
303+
impl From<TomlLockfileSourceIdError> for TomlLockfilePackageIdError {
304+
fn from(value: TomlLockfileSourceIdError) -> Self {
305+
TomlLockfilePackageIdErrorKind::Source(value).into()
304306
}
305307
}
306308

307309
#[non_exhaustive]
308310
#[derive(Debug, thiserror::Error)]
309-
enum EncodablePackageIdErrorKind {
311+
enum TomlLockfilePackageIdErrorKind {
310312
#[error("invalid serialied PackageId")]
311313
InvalidSerializedPackageId,
312314

313315
#[error(transparent)]
314-
Source(#[from] EncodableSourceIdError),
316+
Source(#[from] TomlLockfileSourceIdError),
315317
}
316318

317319
#[cfg(feature = "unstable-schema")]
@@ -325,7 +327,7 @@ fn dump_lockfile_schema() {
325327
#[cfg(test)]
326328
mod tests {
327329
use crate::core::{GitReference, SourceKind};
328-
use crate::lockfile::{EncodableSourceIdErrorKind, TomlLockfileSourceId};
330+
use crate::lockfile::{TomlLockfileSourceId, TomlLockfileSourceIdErrorKind};
329331

330332
#[track_caller]
331333
fn ok(source_str: &str, source_kind: SourceKind, url: &str) {
@@ -389,15 +391,15 @@ mod tests {
389391
fn bad_sources() {
390392
err!(
391393
"unknown+https://my-crates.io",
392-
EncodableSourceIdErrorKind::UnsupportedSource(..)
394+
TomlLockfileSourceIdErrorKind::UnsupportedSource(..)
393395
);
394396
err!(
395397
"registry+https//github.com/rust-lang/crates.io-index",
396-
EncodableSourceIdErrorKind::InvalidUrl { .. }
398+
TomlLockfileSourceIdErrorKind::InvalidUrl { .. }
397399
);
398400
err!(
399401
"https//github.com/rust-lang/crates.io-index",
400-
EncodableSourceIdErrorKind::InvalidSource(..)
402+
TomlLockfileSourceIdErrorKind::InvalidSource(..)
401403
);
402404
}
403405
}

0 commit comments

Comments
 (0)