Skip to content

Commit

Permalink
Cleanup some trait impls for SourceId
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Dec 13, 2018
1 parent 6459852 commit 1795a03
Showing 1 changed file with 11 additions and 36 deletions.
47 changes: 11 additions & 36 deletions src/cargo/core/source/source_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,46 +395,25 @@ fn url_display(url: &Url) -> String {

impl fmt::Display for SourceId {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match *self.inner {
SourceIdInner {
kind: Kind::Path,
ref url,
..
} => write!(f, "{}", url_display(url)),
SourceIdInner {
kind: Kind::Git(ref reference),
ref url,
ref precise,
..
} => {
match self.inner.kind {
Kind::Git(ref reference) => {
// Don't replace the URL display for git references,
// because those are kind of expected to be URLs.
write!(f, "{}", url)?;
write!(f, "{}", self.inner.url)?;
if let Some(pretty) = reference.pretty_ref() {
write!(f, "?{}", pretty)?;
}

if let Some(ref s) = *precise {
if let Some(ref s) = self.inner.precise {
let len = cmp::min(s.len(), 8);
write!(f, "#{}", &s[..len])?;
}
Ok(())
}
SourceIdInner {
kind: Kind::Registry,
ref url,
..
}
| SourceIdInner {
kind: Kind::LocalRegistry,
ref url,
..
} => write!(f, "registry `{}`", url_display(url)),
SourceIdInner {
kind: Kind::Directory,
ref url,
..
} => write!(f, "dir {}", url_display(url)),
Kind::Path => write!(f, "{}", url_display(&self.inner.url)),
Kind::Registry => write!(f, "registry `{}`", url_display(&self.inner.url)),
Kind::LocalRegistry => write!(f, "registry `{}`", url_display(&self.inner.url)),
Kind::Directory => write!(f, "dir {}", url_display(&self.inner.url)),
}
}
}
Expand Down Expand Up @@ -479,7 +458,7 @@ impl Ord for SourceIdInner {
ord => return ord,
}
match (&self.kind, &other.kind) {
(&Kind::Git(ref ref1), &Kind::Git(ref ref2)) => {
(Kind::Git(ref1), Kind::Git(ref2)) => {
(ref1, &self.canonical_url).cmp(&(ref2, &other.canonical_url))
}
_ => self.kind.cmp(&other.kind),
Expand All @@ -493,12 +472,8 @@ impl Ord for SourceIdInner {
impl Hash for SourceId {
fn hash<S: hash::Hasher>(&self, into: &mut S) {
self.inner.kind.hash(into);
match *self.inner {
SourceIdInner {
kind: Kind::Git(..),
ref canonical_url,
..
} => canonical_url.as_str().hash(into),
match self.inner.kind {
Kind::Git(_) => self.inner.canonical_url.as_str().hash(into),
_ => self.inner.url.as_str().hash(into),
}
}
Expand Down

0 comments on commit 1795a03

Please sign in to comment.