From 14c5781389838f05f3f0063e8c7f04b1eeceaf2e Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Thu, 30 Mar 2023 01:11:00 +0200 Subject: refactor(tvix/nix-compat): move HashAlgo to separate file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … 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 Autosubmit: flokli --- tvix/nix-compat/src/lib.rs | 1 + tvix/nix-compat/src/nixhash.rs | 40 ++---------------------------------- tvix/nix-compat/src/nixhash_algos.rs | 39 +++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 38 deletions(-) create mode 100644 tvix/nix-compat/src/nixhash_algos.rs diff --git a/tvix/nix-compat/src/lib.rs b/tvix/nix-compat/src/lib.rs index 5aea5e242f..52b3ea65b9 100644 --- a/tvix/nix-compat/src/lib.rs +++ b/tvix/nix-compat/src/lib.rs @@ -4,6 +4,7 @@ pub mod derivation; pub mod nar; pub mod nixbase32; pub mod nixhash; +mod nixhash_algos; mod nixhash_with_mode; pub mod store_path; diff --git a/tvix/nix-compat/src/nixhash.rs b/tvix/nix-compat/src/nixhash.rs index 69ed98ff96..0c7637c60a 100644 --- a/tvix/nix-compat/src/nixhash.rs +++ b/tvix/nix-compat/src/nixhash.rs @@ -1,10 +1,8 @@ +use crate::nixbase32; use data_encoding::{BASE64, BASE64_NOPAD, HEXLOWER}; -use serde::{Deserialize, Serialize}; -use std::fmt::Display; use thiserror::Error; -use crate::nixbase32; - +pub use crate::nixhash_algos::HashAlgo; pub use crate::nixhash_with_mode::NixHashWithMode; /// Nix allows specifying hashes in various encodings, and magically just @@ -29,40 +27,6 @@ impl NixHash { } } -/// 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 { - 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())), - } - } -} - /// Errors related to NixHash construction. #[derive(Debug, Error)] pub enum Error { 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 { + 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())), + } + } +} -- cgit 1.4.1