about summary refs log tree commit diff
path: root/tvix/nix-compat/src/nixhash/algos.rs
diff options
context:
space:
mode:
authorBrian Olsen <brian@maven-group.org>2024-03-01T20·51+0100
committerBrian Olsen <brian@maven-group.org>2024-03-03T13·07+0000
commiteff2cc4f6844875f3684bfb17a134ccd0b5547a4 (patch)
tree4dd4863d6b9ba914a0096f6d15d87b0b027f1b39 /tvix/nix-compat/src/nixhash/algos.rs
parent260c2938d4ef038993fbd0c84a7ec220ef3f78f2 (diff)
fix(tvix/nix-compat): Make CAHash deserialize more formats r/7633
Currently CAHash only deserializes the hash in hex code while
the serializer outputs a nixbase32 hash. This means that you can't currently
deserialize what has been serialized.

This change makes deserialize support any digest format (so hex, nixbase32
and base64) as well as flattens the deserialize code and error handling.

It also implements serde methods of HashAlgo directly using Display and TryFrom
implementations because otherwise these would get serialized as eg. Sha256 instead
of sha256 which also broke CAHash serialize/deserialize.

Change-Id: I1941a72eaec741e4956292adaaf0115b97f260ba
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11082
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/nix-compat/src/nixhash/algos.rs')
-rw-r--r--tvix/nix-compat/src/nixhash/algos.rs21
1 files changed, 20 insertions, 1 deletions
diff --git a/tvix/nix-compat/src/nixhash/algos.rs b/tvix/nix-compat/src/nixhash/algos.rs
index f189eba09db6..ac8915314c83 100644
--- a/tvix/nix-compat/src/nixhash/algos.rs
+++ b/tvix/nix-compat/src/nixhash/algos.rs
@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
 use crate::nixhash::Error;
 
 /// This are the hash algorithms supported by cppnix.
-#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
 pub enum HashAlgo {
     Md5,
     Sha1,
@@ -36,6 +36,25 @@ impl Display for HashAlgo {
     }
 }
 
+impl Serialize for HashAlgo {
+    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+    where
+        S: serde::Serializer,
+    {
+        serializer.collect_str(&self)
+    }
+}
+
+impl<'de> Deserialize<'de> for HashAlgo {
+    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+    where
+        D: serde::Deserializer<'de>,
+    {
+        let s: &str = Deserialize::deserialize(deserializer)?;
+        HashAlgo::try_from(s).map_err(serde::de::Error::custom)
+    }
+}
+
 /// TODO(Raito): this could be automated via macros, I suppose.
 /// But this may be more expensive than just doing it by hand
 /// and ensuring that is kept in sync.