From b909001d4b2fd20128c53650512fabd7fd598c24 Mon Sep 17 00:00:00 2001 From: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> Date: Tue, 3 Jun 2025 01:08:03 +1000 Subject: [PATCH] Add `Registry::crate_source` Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> --- crates/binstalk-registry/src/lib.rs | 40 ++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/crates/binstalk-registry/src/lib.rs b/crates/binstalk-registry/src/lib.rs index 6dd19ecc..39febc5a 100644 --- a/crates/binstalk-registry/src/lib.rs +++ b/crates/binstalk-registry/src/lib.rs @@ -7,7 +7,11 @@ use binstalk_downloader::{ download::DownloadError, remote::{Client, Error as RemoteError}, }; -use binstalk_types::cargo_toml_binstall::Meta; +use binstalk_types::{ + cargo_toml_binstall::Meta, + crate_info::{CrateSource, SourceType}, + maybe_owned::MaybeOwned, +}; use cargo_toml_workspace::cargo_toml::{Error as CargoTomlError, Manifest}; use compact_str::CompactString; use leon::{ParseError, RenderError}; @@ -80,7 +84,7 @@ pub enum RegistryError { CargoManifest(#[from] Box), #[error("Failed to parse url: {0}")] - UrlParse(#[from] url::ParseError), + UrlParse(#[from] UrlParseError), #[error(transparent)] Download(#[from] DownloadError), @@ -195,15 +199,37 @@ impl Registry { } } } + + /// Get url of the regsitry + pub fn url(&self) -> impl fmt::Display + '_ { + match self { + #[cfg(feature = "git")] + Registry::Git(registry) => either::Left(registry.url()), + Registry::Sparse(registry) => either::Right(registry.url()), + } + } + + /// Get crate source of this registry + pub fn crate_source(&self) -> Result { + let registry = self.to_string(); + Ok(if registry == "https://index.crates.io/" { + CrateSource::cratesio_registry() + } else { + CrateSource { + source_type: match self { + #[cfg(feature = "git")] + Registry::Git(registry) => SourceType::Git, + Registry::Sparse(registry) => SourceType::Sparse, + }, + url: MaybeOwned::Owned(Url::parse(®istry)?), + } + }) + } } impl fmt::Display for Registry { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - #[cfg(feature = "git")] - Registry::Git(registry) => fmt::Display::fmt(®istry.url(), f), - Registry::Sparse(registry) => fmt::Display::fmt(®istry.url(), f), - } + fmt::Display::fmt(&self.url(), f) } }