Skip to content

Commit ae2c37c

Browse files
committed
refactor(source): Pull out protocol logic
1 parent 07244c3 commit ae2c37c

File tree

1 file changed

+29
-44
lines changed

1 file changed

+29
-44
lines changed

src/cargo/core/source_id.rs

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,20 @@ impl PartialEq for SourceIdInner {
752752
}
753753
}
754754

755+
impl SourceKind {
756+
pub fn protocol(&self) -> Option<&str> {
757+
match self {
758+
SourceKind::Path => Some("path"),
759+
SourceKind::Git(_) => Some("git"),
760+
SourceKind::Registry => Some("registry"),
761+
// Sparse registry URL already includes the `sparse+` prefix
762+
SourceKind::SparseRegistry => None,
763+
SourceKind::LocalRegistry => Some("local-registry"),
764+
SourceKind::Directory => Some("directory"),
765+
}
766+
}
767+
}
768+
755769
/// Forwards to `Ord`
756770
impl PartialOrd for SourceKind {
757771
fn partial_cmp(&self, other: &SourceKind) -> Option<Ordering> {
@@ -848,53 +862,24 @@ pub struct SourceIdAsUrl<'a> {
848862

849863
impl<'a> fmt::Display for SourceIdAsUrl<'a> {
850864
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
851-
match *self.inner {
852-
SourceIdInner {
853-
kind: SourceKind::Path,
854-
ref url,
855-
..
856-
} => write!(f, "path+{}", url),
857-
SourceIdInner {
858-
kind: SourceKind::Git(ref reference),
859-
ref url,
860-
ref precise,
861-
..
862-
} => {
863-
write!(f, "git+{}", url)?;
864-
if let Some(pretty) = reference.pretty_ref(self.encoded) {
865-
write!(f, "?{}", pretty)?;
866-
}
867-
if let Some(precise) = precise.as_ref() {
868-
write!(f, "#{}", precise)?;
869-
}
870-
Ok(())
871-
}
872-
SourceIdInner {
873-
kind: SourceKind::Registry,
874-
ref url,
875-
..
876-
} => {
877-
write!(f, "registry+{url}")
865+
if let Some(protocol) = self.inner.kind.protocol() {
866+
write!(f, "{protocol}+")?;
867+
}
868+
write!(f, "{}", self.inner.url)?;
869+
if let SourceIdInner {
870+
kind: SourceKind::Git(ref reference),
871+
ref precise,
872+
..
873+
} = *self.inner
874+
{
875+
if let Some(pretty) = reference.pretty_ref(self.encoded) {
876+
write!(f, "?{}", pretty)?;
878877
}
879-
SourceIdInner {
880-
kind: SourceKind::SparseRegistry,
881-
ref url,
882-
..
883-
} => {
884-
// Sparse registry URL already includes the `sparse+` prefix
885-
write!(f, "{url}")
878+
if let Some(precise) = precise.as_ref() {
879+
write!(f, "#{}", precise)?;
886880
}
887-
SourceIdInner {
888-
kind: SourceKind::LocalRegistry,
889-
ref url,
890-
..
891-
} => write!(f, "local-registry+{}", url),
892-
SourceIdInner {
893-
kind: SourceKind::Directory,
894-
ref url,
895-
..
896-
} => write!(f, "directory+{}", url),
897881
}
882+
Ok(())
898883
}
899884
}
900885

0 commit comments

Comments
 (0)