@@ -4,49 +4,59 @@ use crates_index::IndexConfig;
4
4
use hex:: ToHex ;
5
5
6
6
pub ( crate ) enum CrateIndexLookup {
7
- Git ( crates_index:: GitIndex ) ,
8
- Http ( crates_index:: SparseIndex ) ,
7
+ Git {
8
+ index : crates_index:: GitIndex ,
9
+ index_config : IndexConfig ,
10
+ } ,
11
+ Http {
12
+ index : crates_index:: SparseIndex ,
13
+ index_config : IndexConfig ,
14
+ } ,
9
15
}
10
16
11
17
impl CrateIndexLookup {
12
- pub ( crate ) fn get_source_info ( & self , pkg : & cargo_lock:: Package ) -> Result < Option < SourceInfo > > {
13
- let index_config = self
18
+ pub ( crate ) fn get_source_info ( & self , pkg : & cargo_lock:: Package ) -> Result < SourceInfo > {
19
+ let url = self
14
20
. index_config ( )
15
- . context ( "Failed to get crate index config" ) ?;
16
- let crate_ = match self {
17
- // The crates we care about should all be in the cache already,
18
- // because `cargo metadata` ran which should have fetched them.
19
- Self :: Http ( index) => {
20
- Some ( index. crate_from_cache ( pkg. name . as_str ( ) ) . with_context ( || {
21
- format ! ( "Failed to get crate from cache: {:?}\n {:?}" , index, pkg)
22
- } ) ?)
23
- }
24
- Self :: Git ( index) => index. crate_ ( pkg. name . as_str ( ) ) ,
25
- } ;
26
- let source_info = crate_. and_then ( |crate_idx| {
27
- crate_idx
28
- . versions ( )
29
- . iter ( )
30
- . find ( |v| v. version ( ) == pkg. version . to_string ( ) )
31
- . and_then ( |v| {
32
- v. download_url ( & index_config) . map ( |url| {
33
- let sha256 = pkg
34
- . checksum
35
- . as_ref ( )
36
- . and_then ( |sum| sum. as_sha256 ( ) . map ( |sum| sum. encode_hex :: < String > ( ) ) )
37
- . unwrap_or_else ( || v. checksum ( ) . encode_hex :: < String > ( ) ) ;
38
- SourceInfo { url, sha256 }
21
+ . download_url ( pkg. name . as_str ( ) , & pkg. version . to_string ( ) )
22
+ . context ( "no url for crate" ) ?;
23
+ let sha256 = pkg
24
+ . checksum
25
+ . as_ref ( )
26
+ . and_then ( |sum| sum. as_sha256 ( ) . map ( |sum| sum. encode_hex :: < String > ( ) ) )
27
+ . unwrap_or_else ( || {
28
+ let crate_ = match self {
29
+ // The crates we care about should all be in the cache already,
30
+ // because `cargo metadata` ran which should have fetched them.
31
+ Self :: Http { index, .. } => Some (
32
+ index
33
+ . crate_from_cache ( pkg. name . as_str ( ) )
34
+ . with_context ( || {
35
+ format ! ( "Failed to get crate from cache: {:?}\n {:?}" , index, pkg)
36
+ } )
37
+ . unwrap ( ) ,
38
+ ) ,
39
+ Self :: Git { index, .. } => index. crate_ ( pkg. name . as_str ( ) ) ,
40
+ } ;
41
+ crate_
42
+ . and_then ( |crate_idx| {
43
+ crate_idx
44
+ . versions ( )
45
+ . iter ( )
46
+ . find ( |v| v. version ( ) == pkg. version . to_string ( ) )
47
+ . map ( |v| v. checksum ( ) . encode_hex :: < String > ( ) )
39
48
} )
40
- } )
41
- } ) ;
42
- Ok ( source_info)
49
+ . unwrap ( )
50
+ } ) ;
51
+
52
+ Ok ( SourceInfo { url, sha256 } )
43
53
}
44
54
45
55
#[ allow( clippy:: result_large_err) ]
46
- fn index_config ( & self ) -> Result < IndexConfig , crates_index :: Error > {
56
+ fn index_config ( & self ) -> & IndexConfig {
47
57
match self {
48
- Self :: Git ( index ) => index . index_config ( ) ,
49
- Self :: Http ( index ) => index . index_config ( ) ,
58
+ Self :: Git { index_config , .. } => index_config,
59
+ Self :: Http { index_config , .. } => index_config,
50
60
}
51
61
}
52
62
}
@@ -73,9 +83,14 @@ mod test {
73
83
. unwrap ( ) ,
74
84
) ;
75
85
76
- let index = CrateIndexLookup :: Http (
77
- crates_index:: SparseIndex :: from_url ( "sparse+https://index.crates.io/" ) . unwrap ( ) ,
78
- ) ;
86
+ let index =
87
+ crates_index:: SparseIndex :: from_url ( "sparse+https://index.crates.io/" ) . unwrap ( ) ;
88
+ let index_config = index. index_config ( ) . unwrap ( ) ;
89
+ let index = CrateIndexLookup :: Http {
90
+ index,
91
+ index_config,
92
+ } ;
93
+
79
94
let source_info = index
80
95
. get_source_info ( & cargo_lock:: Package {
81
96
name : "lazy_static" . parse ( ) . unwrap ( ) ,
@@ -85,7 +100,6 @@ mod test {
85
100
dependencies : Vec :: new ( ) ,
86
101
replace : None ,
87
102
} )
88
- . unwrap ( )
89
103
. unwrap ( ) ;
90
104
assert_eq ! (
91
105
source_info. url,
@@ -100,9 +114,14 @@ mod test {
100
114
let _e = EnvVarResetter :: set ( "CARGO_HOME" ,
101
115
runfiles:: rlocation!( runfiles, "rules_rust/crate_universe/test_data/crate_indexes/rewritten_lazy_static/cargo_home" ) . unwrap ( ) ) ;
102
116
103
- let index = CrateIndexLookup :: Http (
104
- crates_index:: SparseIndex :: from_url ( "sparse+https://index.crates.io/" ) . unwrap ( ) ,
105
- ) ;
117
+ let index =
118
+ crates_index:: SparseIndex :: from_url ( "sparse+https://index.crates.io/" ) . unwrap ( ) ;
119
+ let index_config = index. index_config ( ) . unwrap ( ) ;
120
+ let index = CrateIndexLookup :: Http {
121
+ index,
122
+ index_config,
123
+ } ;
124
+
106
125
let source_info = index
107
126
. get_source_info ( & cargo_lock:: Package {
108
127
name : "lazy_static" . parse ( ) . unwrap ( ) ,
@@ -112,7 +131,6 @@ mod test {
112
131
dependencies : Vec :: new ( ) ,
113
132
replace : None ,
114
133
} )
115
- . unwrap ( )
116
134
. unwrap ( ) ;
117
135
assert_eq ! (
118
136
source_info. url,
0 commit comments