about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--tvix/nix-compat/src/nixhash/ca_hash.rs32
-rw-r--r--tvix/tools/narinfo2parquet/src/main.rs23
2 files changed, 27 insertions, 28 deletions
diff --git a/tvix/nix-compat/src/nixhash/ca_hash.rs b/tvix/nix-compat/src/nixhash/ca_hash.rs
index 2bf5f966cefe..423531696afe 100644
--- a/tvix/nix-compat/src/nixhash/ca_hash.rs
+++ b/tvix/nix-compat/src/nixhash/ca_hash.rs
@@ -47,6 +47,26 @@ impl CAHash {
         }
     }
 
+    /// Returns a colon-separated string consisting of mode, recursiveness and
+    /// hash algo. Used as a prefix in various string representations.
+    pub fn algo_str(&self) -> &'static str {
+        match self.mode() {
+            HashMode::Flat => match self.hash().as_ref() {
+                NixHash::Md5(_) => "fixed:md5",
+                NixHash::Sha1(_) => "fixed:sha1",
+                NixHash::Sha256(_) => "fixed:sha256",
+                NixHash::Sha512(_) => "fixed:sha512",
+            },
+            HashMode::Nar => match self.hash().as_ref() {
+                NixHash::Md5(_) => "fixed:r:md5",
+                NixHash::Sha1(_) => "fixed:r:sha1",
+                NixHash::Sha256(_) => "fixed:r:sha256",
+                NixHash::Sha512(_) => "fixed:r:sha512",
+            },
+            HashMode::Text => "text:sha256",
+        }
+    }
+
     /// Constructs a [CAHash] from the textual representation,
     /// which is one of the three:
     /// - `text:sha256:$nixbase32sha256digest`
@@ -76,13 +96,11 @@ impl CAHash {
     /// Formats a [CAHash] in the Nix default hash format, which is the format
     /// that's used in NARInfos for example.
     pub fn to_nix_nixbase32_string(&self) -> String {
-        match self {
-            CAHash::Flat(nh) => format!("fixed:{}", nh.to_nix_nixbase32_string()),
-            CAHash::Nar(nh) => format!("fixed:r:{}", nh.to_nix_nixbase32_string()),
-            CAHash::Text(digest) => {
-                format!("text:sha256:{}", nixbase32::encode(digest))
-            }
-        }
+        format!(
+            "{}:{}",
+            self.algo_str(),
+            nixbase32::encode(self.hash().digest_as_bytes())
+        )
     }
 
     /// This takes a serde_json::Map and turns it into this structure. This is necessary to do such
diff --git a/tvix/tools/narinfo2parquet/src/main.rs b/tvix/tools/narinfo2parquet/src/main.rs
index 3b793e02b133..bbfa5c8c31e0 100644
--- a/tvix/tools/narinfo2parquet/src/main.rs
+++ b/tvix/tools/narinfo2parquet/src/main.rs
@@ -12,7 +12,6 @@ use jemallocator::Jemalloc;
 use nix_compat::{
     narinfo::{self, NarInfo},
     nixbase32,
-    nixhash::{CAHash, NixHash},
 };
 use polars::{io::parquet::ParquetWriter, prelude::*};
 use std::{
@@ -177,26 +176,8 @@ impl FrameBuilder {
             }));
 
         if let Some(ca) = &entry.ca {
-            // decompose the CAHash into algo and hash parts
-            // TODO(edef): move this into CAHash
-            let (algo, hash) = match ca {
-                CAHash::Flat(h) => match h {
-                    NixHash::Md5(h) => ("fixed:md5", &h[..]),
-                    NixHash::Sha1(h) => ("fixed:sha1", &h[..]),
-                    NixHash::Sha256(h) => ("fixed:sha256", &h[..]),
-                    NixHash::Sha512(h) => ("fixed:sha512", &h[..]),
-                },
-                CAHash::Nar(h) => match h {
-                    NixHash::Md5(h) => ("fixed:r:md5", &h[..]),
-                    NixHash::Sha1(h) => ("fixed:r:sha1", &h[..]),
-                    NixHash::Sha256(h) => ("fixed:r:sha256", &h[..]),
-                    NixHash::Sha512(h) => ("fixed:r:sha512", &h[..]),
-                },
-                CAHash::Text(h) => ("text:sha256", &h[..]),
-            };
-
-            self.ca_algo.append_value(algo);
-            self.ca_hash.append_value(hash);
+            self.ca_algo.append_value(ca.algo_str());
+            self.ca_hash.append_value(ca.hash().digest_as_bytes());
         } else {
             self.ca_algo.append_null();
             self.ca_hash.append_null();