feat(tvix/nix-compat): add HashMode and CAHash::mode()

This allows peeking at the mode without matching on all enum kinds of
CAHash directly.

Change-Id: Icca147a876f7f4cff0bbaa067ac4dae40d05bee9
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11069
Reviewed-by: flokli <flokli@flokli.de>
Reviewed-by: Brian Olsen <me@griff.name>
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
This commit is contained in:
Florian Klink 2024-02-29 10:28:41 +02:00 committed by clbot
parent eff2cc4f68
commit 442eabe9d0
2 changed files with 17 additions and 0 deletions

View file

@ -22,6 +22,14 @@ pub enum CAHash {
Text([u8; 32]), // "text", only supports sha256 Text([u8; 32]), // "text", only supports sha256
} }
/// Representation for the supported hash modes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HashMode {
Flat,
Nar,
Text,
}
impl CAHash { impl CAHash {
pub fn hash(&self) -> Cow<NixHash> { pub fn hash(&self) -> Cow<NixHash> {
match *self { match *self {
@ -31,6 +39,14 @@ impl CAHash {
} }
} }
pub fn mode(&self) -> HashMode {
match self {
CAHash::Flat(_) => HashMode::Flat,
CAHash::Nar(_) => HashMode::Nar,
CAHash::Text(_) => HashMode::Text,
}
}
/// Constructs a [CAHash] from the textual representation, /// Constructs a [CAHash] from the textual representation,
/// which is one of the three: /// which is one of the three:
/// - `text:sha256:$nixbase32sha256digest` /// - `text:sha256:$nixbase32sha256digest`

View file

@ -9,6 +9,7 @@ mod ca_hash;
pub use algos::HashAlgo; pub use algos::HashAlgo;
pub use ca_hash::CAHash; pub use ca_hash::CAHash;
pub use ca_hash::HashMode as CAHashMode;
/// NixHash represents hashes known by Nix. /// NixHash represents hashes known by Nix.
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]