@@ -8,6 +8,8 @@ use std::fmt;
8
8
use std:: path:: Path ;
9
9
use std:: str:: FromStr ;
10
10
11
+ use crate :: metadata:: MergedMetadata ;
12
+
11
13
impl EncodableResolve {
12
14
pub fn load_lock_file ( path : & Path ) -> Result < EncodableResolve , Error > {
13
15
let config = & std:: fs:: read_to_string ( path)
@@ -27,8 +29,19 @@ impl EncodableResolve {
27
29
28
30
pub fn get_hashes_by_package_id (
29
31
& self ,
32
+ metadata : & MergedMetadata ,
30
33
hashes : & mut HashMap < PackageId , String > ,
31
34
) -> Result < ( ) , Error > {
35
+ let mut package_id_by_source = HashMap :: new ( ) ;
36
+ for p in & metadata. packages {
37
+ let Some ( ref source) = p. source else {
38
+ // local crate
39
+ continue ;
40
+ } ;
41
+ let key = ( p. name . as_str ( ) , source. repr . as_str ( ) , p. version . to_string ( ) ) ;
42
+ package_id_by_source. insert ( key, & p. id ) ;
43
+ }
44
+
32
45
for EncodableDependency {
33
46
name,
34
47
version,
@@ -37,13 +50,17 @@ impl EncodableResolve {
37
50
..
38
51
} in self . package . iter ( )
39
52
{
40
- if let ( Some ( source) , Some ( checksum) ) = ( source, checksum) {
41
- let package_id = PackageId {
42
- repr : format ! ( "{} {} ({})" , name, version, source) ,
43
- } ;
44
- if checksum != "<none>" {
45
- hashes. insert ( package_id, checksum. clone ( ) ) ;
46
- }
53
+ let Some ( ( source, checksum) ) = Option :: zip ( source. as_ref ( ) , checksum. as_ref ( ) ) else {
54
+ continue ;
55
+ } ;
56
+ if checksum == "<none>" {
57
+ continue ;
58
+ }
59
+
60
+ if let Some ( package_id) =
61
+ package_id_by_source. get ( & ( name. as_str ( ) , source. as_str ( ) , version. clone ( ) ) )
62
+ {
63
+ hashes. insert ( ( * package_id) . clone ( ) , checksum. clone ( ) ) ;
47
64
}
48
65
}
49
66
@@ -66,88 +83,6 @@ impl EncodableResolve {
66
83
}
67
84
}
68
85
69
- #[ test]
70
- fn test_no_legacy_checksums ( ) {
71
- let config = r#"
72
- [[package]]
73
- name = "aho-corasick"
74
- version = "0.7.6"
75
- source = "registry+https://github.com/rust-lang/crates.io-index"
76
- dependencies = [
77
- "memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
78
- ]
79
- "# ;
80
- let resolve = EncodableResolve :: load_lock_string ( Path :: new ( "dummy" ) , config) . unwrap ( ) ;
81
- let mut hashes = HashMap :: new ( ) ;
82
- resolve. get_hashes_by_package_id ( & mut hashes) . unwrap ( ) ;
83
- assert_eq ! ( hashes, HashMap :: new( ) ) ;
84
- }
85
-
86
- #[ test]
87
- fn test_some_legacy_checksums ( ) {
88
- let config = r#"
89
- [[package]]
90
- name = "aho-corasick"
91
- version = "0.7.6"
92
- source = "registry+https://github.com/rust-lang/crates.io-index"
93
- dependencies = [
94
- "memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
95
- ]
96
-
97
- [metadata]
98
- "checksum structopt 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "16c2cdbf9cc375f15d1b4141bc48aeef444806655cd0e904207edc8d68d86ed7"
99
- "checksum structopt-derive 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "53010261a84b37689f9ed7d395165029f9cc7abb9f56bbfe86bee2597ed25107"
100
-
101
- "# ;
102
- let resolve = EncodableResolve :: load_lock_string ( Path :: new ( "dummy" ) , config) . unwrap ( ) ;
103
- let mut hashes = HashMap :: new ( ) ;
104
- resolve. get_hashes_by_package_id ( & mut hashes) . unwrap ( ) ;
105
- assert_eq ! (
106
- hashes,
107
- [ (
108
- PackageId { repr: "structopt 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" . to_string( ) } ,
109
- "16c2cdbf9cc375f15d1b4141bc48aeef444806655cd0e904207edc8d68d86ed7"
110
- ) ,
111
- (
112
- PackageId { repr: "structopt-derive 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" . to_string( ) } ,
113
- "53010261a84b37689f9ed7d395165029f9cc7abb9f56bbfe86bee2597ed25107"
114
- ) ]
115
- . iter( )
116
- . map( |( package_id, hash) | ( package_id. clone( ) , hash. to_string( ) ) )
117
- . collect:: <HashMap <_, _>>( )
118
- ) ;
119
- }
120
-
121
- #[ test]
122
- fn test_some_inline_checksums ( ) {
123
- let config = r#"
124
- [[package]]
125
- name = "aho-corasick"
126
- version = "0.7.6"
127
- source = "registry+https://github.com/rust-lang/crates.io-index"
128
- dependencies = [
129
- "memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
130
- ]
131
- checksum = "16c2cdbf9cc375f15d1b4141bc48aeef444806655cd0e904207edc8d68d86ed7"
132
- "# ;
133
- let resolve = EncodableResolve :: load_lock_string ( Path :: new ( "dummy" ) , config) . unwrap ( ) ;
134
- let mut hashes = HashMap :: new ( ) ;
135
- resolve. get_hashes_by_package_id ( & mut hashes) . unwrap ( ) ;
136
- assert_eq ! (
137
- hashes,
138
- [ (
139
- PackageId {
140
- repr: "aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)"
141
- . to_string( )
142
- } ,
143
- "16c2cdbf9cc375f15d1b4141bc48aeef444806655cd0e904207edc8d68d86ed7"
144
- ) ]
145
- . iter( )
146
- . map( |( package_id, hash) | ( package_id. clone( ) , hash. to_string( ) ) )
147
- . collect:: <HashMap <_, _>>( )
148
- ) ;
149
- }
150
-
151
86
//
152
87
// The code below was copied/adjusted from Cargo.
153
88
//
0 commit comments