@@ -81,6 +81,9 @@ impl SourceId {
81
81
///
82
82
/// The canonical url will be calculated, but the precise field will not
83
83
fn new ( kind : SourceKind , url : Url , name : Option < & str > ) -> CargoResult < SourceId > {
84
+ if kind == SourceKind :: SparseRegistry {
85
+ assert ! ( url. as_str( ) . starts_with( "sparse+" ) ) ;
86
+ }
84
87
let source_id = SourceId :: wrap ( SourceIdInner {
85
88
kind,
86
89
canonical_url : CanonicalUrl :: new ( & url) ?,
@@ -152,7 +155,7 @@ impl SourceId {
152
155
. with_precise ( Some ( "locked" . to_string ( ) ) ) )
153
156
}
154
157
"sparse" => {
155
- let url = url . into_url ( ) ?;
158
+ let url = string . into_url ( ) ?;
156
159
Ok ( SourceId :: new ( SourceKind :: SparseRegistry , url, None ) ?
157
160
. with_precise ( Some ( "locked" . to_string ( ) ) ) )
158
161
}
@@ -721,6 +724,7 @@ impl<'a> fmt::Display for SourceIdAsUrl<'a> {
721
724
ref url,
722
725
..
723
726
} => {
727
+ // Sparse registry URL already includes the `sparse+` prefix
724
728
write ! ( f, "{url}" )
725
729
}
726
730
SourceIdInner {
@@ -864,4 +868,14 @@ mod tests {
864
868
assert_eq ! ( gen_hash( source_id) , 17459999773908528552 ) ;
865
869
assert_eq ! ( crate :: util:: hex:: short_hash( & source_id) , "6568fe2c2fab5bfe" ) ;
866
870
}
871
+
872
+ #[ test]
873
+ fn serde_roundtrip ( ) {
874
+ let url = "sparse+https://my-crates.io/" . into_url ( ) . unwrap ( ) ;
875
+ let source_id = SourceId :: for_registry ( & url) . unwrap ( ) ;
876
+ let formatted = format ! ( "{}" , source_id. as_url( ) ) ;
877
+ let deserialized = SourceId :: from_url ( & formatted) . unwrap ( ) ;
878
+ assert_eq ! ( formatted, "sparse+https://my-crates.io/" ) ;
879
+ assert_eq ! ( source_id, deserialized) ;
880
+ }
867
881
}
0 commit comments