From 535f7730fab9d3bffbe52f5c7f1ebbcb11ec5db5 Mon Sep 17 00:00:00 2001 From: Vito Secona Date: Fri, 19 Sep 2025 00:38:56 +0700 Subject: [PATCH] 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. --- crates/cargo-util-schemas/src/lib.rs | 1 + crates/cargo-util-schemas/src/lockfile.rs | 36 +++++++++++++++++++++++ src/cargo/core/resolver/encode.rs | 28 +++++++++++------- 3 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 crates/cargo-util-schemas/src/lockfile.rs diff --git a/crates/cargo-util-schemas/src/lib.rs b/crates/cargo-util-schemas/src/lib.rs index 9324480f1..f03be81ce 100644 --- a/crates/cargo-util-schemas/src/lib.rs +++ b/crates/cargo-util-schemas/src/lib.rs @@ -10,6 +10,7 @@ pub mod core; pub mod index; +pub mod lockfile; pub mod manifest; pub mod messages; #[cfg(feature = "unstable-schema")] diff --git a/crates/cargo-util-schemas/src/lockfile.rs b/crates/cargo-util-schemas/src/lockfile.rs new file mode 100644 index 000000000..23323c93b --- /dev/null +++ b/crates/cargo-util-schemas/src/lockfile.rs @@ -0,0 +1,36 @@ +#[derive(Debug, thiserror::Error)] +#[error(transparent)] +pub struct EncodableSourceIdError(#[from] pub EncodableSourceIdErrorKind); + +#[non_exhaustive] +#[derive(Debug, thiserror::Error)] +pub enum EncodableSourceIdErrorKind { + #[error("invalid source `{0}`")] + InvalidSource(String), + + #[error("invalid url `{url}`: {msg}")] + InvalidUrl { url: String, msg: String }, + + #[error("unsupported source protocol: {0}")] + UnsupportedSource(String), +} + +#[derive(Debug, thiserror::Error)] +#[error(transparent)] +pub struct EncodablePackageIdError(#[from] EncodablePackageIdErrorKind); + +impl From for EncodablePackageIdError { + fn from(value: EncodableSourceIdError) -> Self { + EncodablePackageIdErrorKind::Source(value).into() + } +} + +#[non_exhaustive] +#[derive(Debug, thiserror::Error)] +pub enum EncodablePackageIdErrorKind { + #[error("invalid serialied PackageId")] + InvalidSerializedPackageId, + + #[error(transparent)] + Source(#[from] EncodableSourceIdError), +} diff --git a/src/cargo/core/resolver/encode.rs b/src/cargo/core/resolver/encode.rs index bf41df356..3569bc9a1 100644 --- a/src/cargo/core/resolver/encode.rs +++ b/src/cargo/core/resolver/encode.rs @@ -118,6 +118,10 @@ use crate::util::interning::InternedString; use crate::util::{Graph, internal}; use anyhow::{Context as _, bail}; use cargo_util_schemas::core::SourceKind; +use cargo_util_schemas::lockfile::{ + EncodablePackageIdError, EncodablePackageIdErrorKind, EncodableSourceIdError, + EncodableSourceIdErrorKind, +}; use serde::de; use serde::ser; use serde::{Deserialize, Serialize}; @@ -550,14 +554,16 @@ pub struct EncodableSourceId { } impl EncodableSourceId { - pub fn new(source: String) -> CargoResult { + pub fn new(source: String) -> Result { let source_str = source.clone(); - let (kind, url) = source - .split_once('+') - .ok_or_else(|| anyhow::format_err!("invalid source `{}`", source_str))?; + let (kind, url) = source.split_once('+').ok_or_else(|| { + EncodableSourceIdError(EncodableSourceIdErrorKind::InvalidSource(source.clone()).into()) + })?; - let url = - Url::parse(url).map_err(|s| anyhow::format_err!("invalid url `{}`: {}", url, s))?; + let url = Url::parse(url).map_err(|msg| EncodableSourceIdErrorKind::InvalidUrl { + url: url.to_string(), + msg: msg.to_string(), + })?; let kind = match kind { "git" => { @@ -567,7 +573,9 @@ impl EncodableSourceId { "registry" => SourceKind::Registry, "sparse" => SourceKind::SparseRegistry, "path" => SourceKind::Path, - kind => anyhow::bail!("unsupported source protocol: {}", kind), + kind => { + return Err(EncodableSourceIdErrorKind::UnsupportedSource(kind.to_string()).into()); + } }; Ok(Self { @@ -663,9 +671,9 @@ impl fmt::Display for EncodablePackageId { } impl FromStr for EncodablePackageId { - type Err = anyhow::Error; + type Err = EncodablePackageIdError; - fn from_str(s: &str) -> CargoResult { + fn from_str(s: &str) -> Result { let mut s = s.splitn(3, ' '); let name = s.next().unwrap(); let version = s.next(); @@ -674,7 +682,7 @@ impl FromStr for EncodablePackageId { if let Some(s) = s.strip_prefix('(').and_then(|s| s.strip_suffix(')')) { Some(EncodableSourceId::new(s.to_string())?) } else { - anyhow::bail!("invalid serialized PackageId") + return Err(EncodablePackageIdErrorKind::InvalidSerializedPackageId.into()); } } None => None,