1
+ use crate :: BuiltDist ;
1
2
use uv_pypi_types:: { HashAlgorithm , HashDigest } ;
2
3
3
4
#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
4
5
pub enum HashPolicy < ' a > {
5
6
/// No hash policy is specified.
6
7
None ,
7
8
/// Hashes should be generated (specifically, a SHA-256 hash), but not validated.
8
- Generate ,
9
+ Generate ( HashGeneration ) ,
9
10
/// Hashes should be validated against a pre-defined list of hashes. If necessary, hashes should
10
11
/// be generated so as to ensure that the archive is valid.
11
12
Validate ( & ' a [ HashDigest ] ) ,
@@ -17,21 +18,28 @@ impl HashPolicy<'_> {
17
18
matches ! ( self , Self :: None )
18
19
}
19
20
20
- /// Returns `true` if the hash policy is `Generate`.
21
- pub fn is_generate ( & self ) -> bool {
22
- matches ! ( self , Self :: Generate )
23
- }
24
-
25
21
/// Returns `true` if the hash policy is `Validate`.
26
22
pub fn is_validate ( & self ) -> bool {
27
23
matches ! ( self , Self :: Validate ( _) )
28
24
}
29
25
26
+ /// Returns `true` if the hash policy indicates that hashes should be generated.
27
+ pub fn is_generate ( & self , dist : & BuiltDist ) -> bool {
28
+ match self {
29
+ HashPolicy :: Generate ( HashGeneration :: Url ) => dist. file ( ) . is_none ( ) ,
30
+ HashPolicy :: Generate ( HashGeneration :: All ) => {
31
+ dist. file ( ) . map_or ( true , |file| file. hashes . is_empty ( ) )
32
+ }
33
+ HashPolicy :: Validate ( _) => false ,
34
+ HashPolicy :: None => false ,
35
+ }
36
+ }
37
+
30
38
/// Return the algorithms used in the hash policy.
31
39
pub fn algorithms ( & self ) -> Vec < HashAlgorithm > {
32
40
match self {
33
41
Self :: None => vec ! [ ] ,
34
- Self :: Generate => vec ! [ HashAlgorithm :: Sha256 ] ,
42
+ Self :: Generate ( _ ) => vec ! [ HashAlgorithm :: Sha256 ] ,
35
43
Self :: Validate ( hashes) => {
36
44
let mut algorithms = hashes. iter ( ) . map ( HashDigest :: algorithm) . collect :: < Vec < _ > > ( ) ;
37
45
algorithms. sort ( ) ;
@@ -45,12 +53,22 @@ impl HashPolicy<'_> {
45
53
pub fn digests ( & self ) -> & [ HashDigest ] {
46
54
match self {
47
55
Self :: None => & [ ] ,
48
- Self :: Generate => & [ ] ,
56
+ Self :: Generate ( _ ) => & [ ] ,
49
57
Self :: Validate ( hashes) => hashes,
50
58
}
51
59
}
52
60
}
53
61
62
+ /// The context in which hashes should be generated.
63
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
64
+ pub enum HashGeneration {
65
+ /// Generate hashes for direct URL distributions.
66
+ Url ,
67
+ /// Generate hashes for direct URL distributions, along with any distributions that are hosted
68
+ /// on a registry that does _not_ provide hashes.
69
+ All ,
70
+ }
71
+
54
72
pub trait Hashed {
55
73
/// Return the [`HashDigest`]s for the archive.
56
74
fn hashes ( & self ) -> & [ HashDigest ] ;
@@ -59,7 +77,7 @@ pub trait Hashed {
59
77
fn satisfies ( & self , hashes : HashPolicy ) -> bool {
60
78
match hashes {
61
79
HashPolicy :: None => true ,
62
- HashPolicy :: Generate => self
80
+ HashPolicy :: Generate ( _ ) => self
63
81
. hashes ( )
64
82
. iter ( )
65
83
. any ( |hash| hash. algorithm == HashAlgorithm :: Sha256 ) ,
@@ -71,7 +89,7 @@ pub trait Hashed {
71
89
fn has_digests ( & self , hashes : HashPolicy ) -> bool {
72
90
match hashes {
73
91
HashPolicy :: None => true ,
74
- HashPolicy :: Generate => self
92
+ HashPolicy :: Generate ( _ ) => self
75
93
. hashes ( )
76
94
. iter ( )
77
95
. any ( |hash| hash. algorithm == HashAlgorithm :: Sha256 ) ,
0 commit comments