about summary refs log tree commit diff
path: root/tvix/nix-compat/src
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2024-02-29T08·28+0200
committerclbot <clbot@tvl.fyi>2024-03-03T13·17+0000
commit442eabe9d0c9e88e902d40a7748652bdca3d5628 (patch)
tree038db8a97359b0e70ebac569af29502af5e50b2b /tvix/nix-compat/src
parenteff2cc4f6844875f3684bfb17a134ccd0b5547a4 (diff)
feat(tvix/nix-compat): add HashMode and CAHash::mode() r/7634
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>
Diffstat (limited to 'tvix/nix-compat/src')
-rw-r--r--tvix/nix-compat/src/nixhash/ca_hash.rs16
-rw-r--r--tvix/nix-compat/src/nixhash/mod.rs1
2 files changed, 17 insertions, 0 deletions
diff --git a/tvix/nix-compat/src/nixhash/ca_hash.rs b/tvix/nix-compat/src/nixhash/ca_hash.rs
index 1c2f61dc6c65..2bf5f966cefe 100644
--- a/tvix/nix-compat/src/nixhash/ca_hash.rs
+++ b/tvix/nix-compat/src/nixhash/ca_hash.rs
@@ -22,6 +22,14 @@ pub enum CAHash {
     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 {
     pub fn hash(&self) -> Cow<NixHash> {
         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,
     /// which is one of the three:
     /// - `text:sha256:$nixbase32sha256digest`
diff --git a/tvix/nix-compat/src/nixhash/mod.rs b/tvix/nix-compat/src/nixhash/mod.rs
index a6f2b967996a..25b89d8f8eb0 100644
--- a/tvix/nix-compat/src/nixhash/mod.rs
+++ b/tvix/nix-compat/src/nixhash/mod.rs
@@ -9,6 +9,7 @@ mod ca_hash;
 
 pub use algos::HashAlgo;
 pub use ca_hash::CAHash;
+pub use ca_hash::HashMode as CAHashMode;
 
 /// NixHash represents hashes known by Nix.
 #[derive(Clone, Debug, Eq, PartialEq)]