@@ -188,17 +188,7 @@ impl SourceId {
188188 match kind {
189189 "git" => {
190190 let mut url = url. into_url ( ) ?;
191- let mut reference = GitReference :: DefaultBranch ;
192- for ( k, v) in url. query_pairs ( ) {
193- match & k[ ..] {
194- // Map older 'ref' to branch.
195- "branch" | "ref" => reference = GitReference :: Branch ( v. into_owned ( ) ) ,
196-
197- "rev" => reference = GitReference :: Rev ( v. into_owned ( ) ) ,
198- "tag" => reference = GitReference :: Tag ( v. into_owned ( ) ) ,
199- _ => { }
200- }
201- }
191+ let reference = GitReference :: from_query ( url. query_pairs ( ) ) ;
202192 let precise = url. fragment ( ) . map ( |s| s. to_owned ( ) ) ;
203193 url. set_fragment ( None ) ;
204194 url. set_query ( None ) ;
@@ -752,6 +742,20 @@ impl PartialEq for SourceIdInner {
752742 }
753743}
754744
745+ impl SourceKind {
746+ pub ( crate ) fn protocol ( & self ) -> Option < & str > {
747+ match self {
748+ SourceKind :: Path => Some ( "path" ) ,
749+ SourceKind :: Git ( _) => Some ( "git" ) ,
750+ SourceKind :: Registry => Some ( "registry" ) ,
751+ // Sparse registry URL already includes the `sparse+` prefix
752+ SourceKind :: SparseRegistry => None ,
753+ SourceKind :: LocalRegistry => Some ( "local-registry" ) ,
754+ SourceKind :: Directory => Some ( "directory" ) ,
755+ }
756+ }
757+ }
758+
755759/// Forwards to `Ord`
756760impl PartialOrd for SourceKind {
757761 fn partial_cmp ( & self , other : & SourceKind ) -> Option < Ordering > {
@@ -848,57 +852,46 @@ pub struct SourceIdAsUrl<'a> {
848852
849853impl < ' a > fmt:: Display for SourceIdAsUrl < ' a > {
850854 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}" )
855+ if let Some ( protocol) = self . inner . kind . protocol ( ) {
856+ write ! ( f, "{protocol}+" ) ?;
857+ }
858+ write ! ( f, "{}" , self . inner. url) ?;
859+ if let SourceIdInner {
860+ kind : SourceKind :: Git ( ref reference) ,
861+ ref precise,
862+ ..
863+ } = * self . inner
864+ {
865+ if let Some ( pretty) = reference. pretty_ref ( self . encoded ) {
866+ write ! ( f, "?{}" , pretty) ?;
878867 }
879- SourceIdInner {
880- kind : SourceKind :: SparseRegistry ,
881- ref url,
882- ..
883- } => {
884- // Sparse registry URL already includes the `sparse+` prefix
885- write ! ( f, "{url}" )
868+ if let Some ( precise) = precise. as_ref ( ) {
869+ write ! ( f, "#{}" , precise) ?;
886870 }
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) ,
897871 }
872+ Ok ( ( ) )
898873 }
899874}
900875
901876impl GitReference {
877+ pub fn from_query (
878+ query_pairs : impl Iterator < Item = ( impl AsRef < str > , impl AsRef < str > ) > ,
879+ ) -> Self {
880+ let mut reference = GitReference :: DefaultBranch ;
881+ for ( k, v) in query_pairs {
882+ let v = v. as_ref ( ) ;
883+ match k. as_ref ( ) {
884+ // Map older 'ref' to branch.
885+ "branch" | "ref" => reference = GitReference :: Branch ( v. to_owned ( ) ) ,
886+
887+ "rev" => reference = GitReference :: Rev ( v. to_owned ( ) ) ,
888+ "tag" => reference = GitReference :: Tag ( v. to_owned ( ) ) ,
889+ _ => { }
890+ }
891+ }
892+ reference
893+ }
894+
902895 /// Returns a `Display`able view of this git reference, or None if using
903896 /// the head of the default branch
904897 pub fn pretty_ref ( & self , url_encoded : bool ) -> Option < PrettyRef < ' _ > > {
0 commit comments