From 442eabe9d0c9e88e902d40a7748652bdca3d5628 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Thu, 29 Feb 2024 10:28:41 +0200 Subject: feat(tvix/nix-compat): add HashMode and CAHash::mode() This allows peeking at the mode without matching on all enum kinds of CAHash directly. Change-Id: Icca147a876f7f4cff0bbaa067ac4dae40d05bee9 Reviewed-on: https://cl.tvl.fyi/c/depot/+/11069 Reviewed-by: flokli Reviewed-by: Brian Olsen Tested-by: BuildkiteCI Autosubmit: flokli --- tvix/nix-compat/src/nixhash/ca_hash.rs | 16 ++++++++++++++++ tvix/nix-compat/src/nixhash/mod.rs | 1 + 2 files changed, 17 insertions(+) (limited to 'tvix/nix-compat') diff --git a/tvix/nix-compat/src/nixhash/ca_hash.rs b/tvix/nix-compat/src/nixhash/ca_hash.rs index 1c2f61dc6c..2bf5f966ce 100644 --- a/tvix/nix-compat/src/nixhash/ca_hash.rs +++ b/tvix/nix-compat/src/nixhash/ca_hash.rs @@ -22,6 +22,14 @@ pub enum CAHash { Text([u8; 32]), // "text", only supports sha256 } +/// Representation for the supported hash modes. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum HashMode { + Flat, + Nar, + Text, +} + impl CAHash { pub fn hash(&self) -> Cow { match *self { @@ -31,6 +39,14 @@ impl CAHash { } } + pub fn mode(&self) -> HashMode { + match self { + CAHash::Flat(_) => HashMode::Flat, + CAHash::Nar(_) => HashMode::Nar, + CAHash::Text(_) => HashMode::Text, + } + } + /// Constructs a [CAHash] from the textual representation, /// which is one of the three: /// - `text:sha256:$nixbase32sha256digest` diff --git a/tvix/nix-compat/src/nixhash/mod.rs b/tvix/nix-compat/src/nixhash/mod.rs index a6f2b96799..25b89d8f8e 100644 --- a/tvix/nix-compat/src/nixhash/mod.rs +++ b/tvix/nix-compat/src/nixhash/mod.rs @@ -9,6 +9,7 @@ mod ca_hash; pub use algos::HashAlgo; pub use ca_hash::CAHash; +pub use ca_hash::HashMode as CAHashMode; /// NixHash represents hashes known by Nix. #[derive(Clone, Debug, Eq, PartialEq)] -- cgit 1.4.1