diff options
Diffstat (limited to 'tvix/nix-compat/src/nixhash/algos.rs')
-rw-r--r-- | tvix/nix-compat/src/nixhash/algos.rs | 21 |
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. |