Skip to content

Commit 6892f49

Browse files
committed
fix(lockfile): rename errors for clarity
1 parent 0436f86 commit 6892f49

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

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

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,18 @@ 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();
108108
let (kind, url) = source.split_once('+').ok_or_else(|| {
109-
EncodableSourceIdError(EncodableSourceIdErrorKind::InvalidSource(source.clone()).into())
109+
TomlLockfileSourceIdError(
110+
TomlLockfileSourceIdErrorKind::InvalidSource(source.clone()).into(),
111+
)
110112
})?;
111113

112114
// Sparse URLs store the kind prefix (sparse+) in the URL. Therefore, for sparse kinds, we
113115
// want to use the raw `source` instead of the splitted `url`.
114116
let url = Url::parse(if kind == "sparse" { &source } else { url }).map_err(|msg| {
115-
EncodableSourceIdErrorKind::InvalidUrl {
117+
TomlLockfileSourceIdErrorKind::InvalidUrl {
116118
url: url.to_string(),
117119
msg: msg.to_string(),
118120
}
@@ -127,7 +129,9 @@ impl TomlLockfileSourceId {
127129
"sparse" => SourceKind::SparseRegistry,
128130
"path" => SourceKind::Path,
129131
kind => {
130-
return Err(EncodableSourceIdErrorKind::UnsupportedSource(kind.to_string()).into());
132+
return Err(
133+
TomlLockfileSourceIdErrorKind::UnsupportedSource(kind.to_string()).into(),
134+
);
131135
}
132136
};
133137

@@ -230,7 +234,7 @@ impl fmt::Display for TomlLockfilePackageId {
230234
}
231235

232236
impl FromStr for TomlLockfilePackageId {
233-
type Err = EncodablePackageIdError;
237+
type Err = TomlLockfilePackageIdError;
234238

235239
fn from_str(s: &str) -> Result<TomlLockfilePackageId, Self::Err> {
236240
let mut s = s.splitn(3, ' ');
@@ -241,7 +245,7 @@ impl FromStr for TomlLockfilePackageId {
241245
if let Some(s) = s.strip_prefix('(').and_then(|s| s.strip_suffix(')')) {
242246
Some(TomlLockfileSourceId::new(s.to_string())?)
243247
} else {
244-
return Err(EncodablePackageIdErrorKind::InvalidSerializedPackageId.into());
248+
return Err(TomlLockfilePackageIdErrorKind::InvalidSerializedPackageId.into());
245249
}
246250
}
247251
None => None,
@@ -279,11 +283,11 @@ impl<'de> de::Deserialize<'de> for TomlLockfilePackageId {
279283

280284
#[derive(Debug, thiserror::Error)]
281285
#[error(transparent)]
282-
pub struct EncodableSourceIdError(#[from] EncodableSourceIdErrorKind);
286+
pub struct TomlLockfileSourceIdError(#[from] TomlLockfileSourceIdErrorKind);
283287

284288
#[non_exhaustive]
285289
#[derive(Debug, thiserror::Error)]
286-
enum EncodableSourceIdErrorKind {
290+
enum TomlLockfileSourceIdErrorKind {
287291
#[error("invalid source `{0}`")]
288292
InvalidSource(String),
289293

@@ -296,22 +300,22 @@ enum EncodableSourceIdErrorKind {
296300

297301
#[derive(Debug, thiserror::Error)]
298302
#[error(transparent)]
299-
pub struct EncodablePackageIdError(#[from] EncodablePackageIdErrorKind);
303+
pub struct TomlLockfilePackageIdError(#[from] TomlLockfilePackageIdErrorKind);
300304

301-
impl From<EncodableSourceIdError> for EncodablePackageIdError {
302-
fn from(value: EncodableSourceIdError) -> Self {
303-
EncodablePackageIdErrorKind::Source(value).into()
305+
impl From<TomlLockfileSourceIdError> for TomlLockfilePackageIdError {
306+
fn from(value: TomlLockfileSourceIdError) -> Self {
307+
TomlLockfilePackageIdErrorKind::Source(value).into()
304308
}
305309
}
306310

307311
#[non_exhaustive]
308312
#[derive(Debug, thiserror::Error)]
309-
enum EncodablePackageIdErrorKind {
313+
enum TomlLockfilePackageIdErrorKind {
310314
#[error("invalid serialied PackageId")]
311315
InvalidSerializedPackageId,
312316

313317
#[error(transparent)]
314-
Source(#[from] EncodableSourceIdError),
318+
Source(#[from] TomlLockfileSourceIdError),
315319
}
316320

317321
#[cfg(feature = "unstable-schema")]
@@ -325,7 +329,7 @@ fn dump_lockfile_schema() {
325329
#[cfg(test)]
326330
mod tests {
327331
use crate::core::{GitReference, SourceKind};
328-
use crate::lockfile::{EncodableSourceIdErrorKind, TomlLockfileSourceId};
332+
use crate::lockfile::{TomlLockfileSourceId, TomlLockfileSourceIdErrorKind};
329333

330334
#[track_caller]
331335
fn ok(source_str: &str, source_kind: SourceKind, url: &str) {
@@ -389,15 +393,15 @@ mod tests {
389393
fn bad_sources() {
390394
err!(
391395
"unknown+https://my-crates.io",
392-
EncodableSourceIdErrorKind::UnsupportedSource(..)
396+
TomlLockfileSourceIdErrorKind::UnsupportedSource(..)
393397
);
394398
err!(
395399
"registry+https//github.com/rust-lang/crates.io-index",
396-
EncodableSourceIdErrorKind::InvalidUrl { .. }
400+
TomlLockfileSourceIdErrorKind::InvalidUrl { .. }
397401
);
398402
err!(
399403
"https//github.com/rust-lang/crates.io-index",
400-
EncodableSourceIdErrorKind::InvalidSource(..)
404+
TomlLockfileSourceIdErrorKind::InvalidSource(..)
401405
);
402406
}
403407
}

0 commit comments

Comments
 (0)