about summary refs log tree commit diff
path: root/tvix/nix-compat/src/nixhash_algos.rs
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-03-29T23·11+0200
committerclbot <clbot@tvl.fyi>2023-03-30T13·47+0000
commit14c5781389838f05f3f0063e8c7f04b1eeceaf2e (patch)
treeb38482d57d285dba8b3f4f8268b73adf30f3d1e6 /tvix/nix-compat/src/nixhash_algos.rs
parente53152866ed975fafb59634321d6228cc29b3d82 (diff)
refactor(tvix/nix-compat): move HashAlgo to separate file r/6056
… and keep the pub exports as is.

Change-Id: I2ad21660577553395f05b5ba71083626429b0dfc
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8363
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
Autosubmit: flokli <flokli@flokli.de>
Diffstat (limited to '')
-rw-r--r--tvix/nix-compat/src/nixhash_algos.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/tvix/nix-compat/src/nixhash_algos.rs b/tvix/nix-compat/src/nixhash_algos.rs
new file mode 100644
index 0000000000..1864b4ef97
--- /dev/null
+++ b/tvix/nix-compat/src/nixhash_algos.rs
@@ -0,0 +1,39 @@
+use std::fmt::Display;
+
+use serde::{Deserialize, Serialize};
+
+use crate::nixhash::Error;
+
+/// This are the hash algorithms supported by cppnix.
+#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
+pub enum HashAlgo {
+    Md5,
+    Sha1,
+    Sha256,
+    Sha512,
+}
+
+impl Display for HashAlgo {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        match &self {
+            HashAlgo::Md5 => write!(f, "md5"),
+            HashAlgo::Sha1 => write!(f, "sha1"),
+            HashAlgo::Sha256 => write!(f, "sha256"),
+            HashAlgo::Sha512 => write!(f, "sha512"),
+        }
+    }
+}
+
+impl TryFrom<&str> for HashAlgo {
+    type Error = Error;
+
+    fn try_from(algo_str: &str) -> Result<Self, Self::Error> {
+        match algo_str {
+            "md5" => Ok(Self::Md5),
+            "sha1" => Ok(Self::Sha1),
+            "sha256" => Ok(Self::Sha256),
+            "sha512" => Ok(Self::Sha512),
+            _ => Err(Error::InvalidAlgo(algo_str.to_string())),
+        }
+    }
+}