uv_configuration/
hash.rs

1#[derive(Debug, Copy, Clone)]
2pub enum HashCheckingMode {
3    /// Hashes should be validated against a pre-defined list of hashes. Every requirement must
4    /// itself be hashable (e.g., Git dependencies are forbidden) _and_ have a hash in the lockfile.
5    Require,
6    /// Hashes should be validated, if present, but ignored if absent.
7    Verify,
8}
9
10impl HashCheckingMode {
11    /// Return the [`HashCheckingMode`] from the command-line arguments, if any.
12    ///
13    /// By default, the hash checking mode is [`HashCheckingMode::Verify`]. If `--require-hashes` is
14    /// passed, the hash checking mode is [`HashCheckingMode::Require`]. If `--no-verify-hashes` is
15    /// passed, then no hash checking is performed.
16    pub fn from_args(require_hashes: Option<bool>, verify_hashes: Option<bool>) -> Option<Self> {
17        if require_hashes == Some(true) {
18            // Given `--require-hashes`, always require hashes, regardless of any other flags.
19            Some(Self::Require)
20        } else if verify_hashes == Some(true) {
21            // Given `--verify-hashes`, always verify hashes, regardless of any other flags.
22            Some(Self::Verify)
23        } else if verify_hashes == Some(false) {
24            // Given `--no-verify-hashes` (without `--require-hashes`), do not verify hashes.
25            None
26        } else if require_hashes == Some(false) {
27            // Given `--no-require-hashes` (without `--verify-hashes`), do not require hashes.
28            None
29        } else {
30            // By default, verify hashes.
31            Some(Self::Verify)
32        }
33    }
34
35    /// Returns `true` if the hash checking mode is `Require`.
36    pub fn is_require(&self) -> bool {
37        matches!(self, Self::Require)
38    }
39}
40
41impl std::fmt::Display for HashCheckingMode {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        match self {
44            Self::Require => write!(f, "--require-hashes"),
45            Self::Verify => write!(f, "--verify-hashes"),
46        }
47    }
48}