about summary refs log tree commit diff
path: root/tvix/nix-compat/src/nixhash/with_mode.rs
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-10-14T16·48+0100
committerclbot <clbot@tvl.fyi>2023-10-14T17·55+0000
commit4ae0f428bd207fdf3730d0f6ff73c7410ae9cd7b (patch)
tree341376899389f822e4bd40b3843aba8954fc77a3 /tvix/nix-compat/src/nixhash/with_mode.rs
parent786b0324a9df1a63606bae72011978fc415f6f07 (diff)
refactor(tvix/nix-compat): make NixHash an enum with fixed-len bytes r/6807
Less Vec<u8> passed around.

Change-Id: Ie153a6bfaa084d7490ffa38634efdf5f3c31a768
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9722
Reviewed-by: Connor Brewster <cbrewster@hey.com>
Autosubmit: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/nix-compat/src/nixhash/with_mode.rs')
-rw-r--r--tvix/nix-compat/src/nixhash/with_mode.rs43
1 files changed, 21 insertions, 22 deletions
diff --git a/tvix/nix-compat/src/nixhash/with_mode.rs b/tvix/nix-compat/src/nixhash/with_mode.rs
index caf14331426a..86c14dc25d9c 100644
--- a/tvix/nix-compat/src/nixhash/with_mode.rs
+++ b/tvix/nix-compat/src/nixhash/with_mode.rs
@@ -6,6 +6,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};
 use serde_json::{Map, Value};
 
 use super::algos::SUPPORTED_ALGOS;
+use super::from_algo_and_digest;
 
 pub enum NixHashMode {
     Flat,
@@ -103,40 +104,38 @@ impl NixHashWithMode {
             if let Some(s) = v.as_str() {
                 match s.strip_prefix("r:") {
                     Some(rest) => Ok(Some(Self::Recursive(
-                        (
+                        from_algo_and_digest(
                             HashAlgo::try_from(rest).map_err(|e| {
                                 serde::de::Error::invalid_value(
                                     Unexpected::Other(&e.to_string()),
                                     &format!("one of {}", SUPPORTED_ALGOS.join(",")).as_str(),
                                 )
                             })?,
-                            digest,
+                            &digest,
                         )
-                            .try_into()
-                            .map_err(|e: nixhash::Error| {
-                                serde::de::Error::invalid_value(
-                                    Unexpected::Other(&e.to_string()),
-                                    &"a digest with right length",
-                                )
-                            })?,
+                        .map_err(|e: nixhash::Error| {
+                            serde::de::Error::invalid_value(
+                                Unexpected::Other(&e.to_string()),
+                                &"a digest with right length",
+                            )
+                        })?,
                     ))),
                     None => Ok(Some(Self::Flat(
-                        (
+                        from_algo_and_digest(
                             HashAlgo::try_from(s).map_err(|e| {
                                 serde::de::Error::invalid_value(
                                     Unexpected::Other(&e.to_string()),
                                     &format!("one of {}", SUPPORTED_ALGOS.join(",")).as_str(),
                                 )
                             })?,
-                            digest,
+                            &digest,
                         )
-                            .try_into()
-                            .map_err(|e: nixhash::Error| {
-                                serde::de::Error::invalid_value(
-                                    Unexpected::Other(&e.to_string()),
-                                    &"a digest with right length",
-                                )
-                            })?,
+                        .map_err(|e: nixhash::Error| {
+                            serde::de::Error::invalid_value(
+                                Unexpected::Other(&e.to_string()),
+                                &"a digest with right length",
+                            )
+                        })?,
                     ))),
                 }
             } else {
@@ -162,12 +161,12 @@ impl Serialize for NixHashWithMode {
         let mut map = serializer.serialize_map(Some(2))?;
         match self {
             NixHashWithMode::Flat(h) => {
-                map.serialize_entry("hash", &nixbase32::encode(&h.digest))?;
-                map.serialize_entry("hashAlgo", &h.algo.to_string())?;
+                map.serialize_entry("hash", &nixbase32::encode(h.digest_as_bytes()))?;
+                map.serialize_entry("hashAlgo", &h.algo())?;
             }
             NixHashWithMode::Recursive(h) => {
-                map.serialize_entry("hash", &nixbase32::encode(&h.digest))?;
-                map.serialize_entry("hashAlgo", &format!("r:{}", &h.algo.to_string()))?;
+                map.serialize_entry("hash", &nixbase32::encode(h.digest_as_bytes()))?;
+                map.serialize_entry("hashAlgo", &format!("r:{}", &h.algo()))?;
             }
         };
         map.end()