From 3c23b323d59d0387be11dd3ffda0939791ddbd39 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Thu, 5 Jun 2025 22:30:21 +0300 Subject: [PATCH] refactor(nix-compat/nixhash/ca_hash): construct algo and digest separately This is more readable. Change-Id: I632afc53c3e4b3c07fd913355a02e0fd575f4e02 Reviewed-on: https://cl.snix.dev/c/snix/+/30565 Autosubmit: Florian Klink Reviewed-by: edef Tested-by: besadii --- snix/nix-compat/src/nixhash/ca_hash.rs | 34 ++++++++++++-------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/snix/nix-compat/src/nixhash/ca_hash.rs b/snix/nix-compat/src/nixhash/ca_hash.rs index e3004874b..0563c5fd9 100644 --- a/snix/nix-compat/src/nixhash/ca_hash.rs +++ b/snix/nix-compat/src/nixhash/ca_hash.rs @@ -77,25 +77,23 @@ impl CAHash { /// Formats a [CAHash] in the Nix default hash format, which is the format /// that's used in NARInfos for example. pub fn to_nix_nixbase32_string(&self) -> String { - format!( - "{}:{}", - match self.mode() { - HashMode::Flat => match self.hash().as_ref() { - NixHash::Md5(_) => "fixed:md5", - NixHash::Sha1(_) => "fixed:sha1", - NixHash::Sha256(_) => "fixed:sha256", - NixHash::Sha512(_) => "fixed:sha512", - }, - HashMode::Nar => match self.hash().as_ref() { - NixHash::Md5(_) => "fixed:r:md5", - NixHash::Sha1(_) => "fixed:r:sha1", - NixHash::Sha256(_) => "fixed:r:sha256", - NixHash::Sha512(_) => "fixed:r:sha512", - }, - HashMode::Text => "text:sha256", + let (algo, hash) = match self { + CAHash::Flat(h) => match h { + NixHash::Md5(h) => ("fixed:md5", &h[..]), + NixHash::Sha1(h) => ("fixed:sha1", &h[..]), + NixHash::Sha256(h) => ("fixed:sha256", &h[..]), + NixHash::Sha512(h) => ("fixed:sha512", &h[..]), }, - nixbase32::encode(self.hash().digest_as_bytes()) - ) + CAHash::Nar(h) => match h { + NixHash::Md5(h) => ("fixed:r:md5", &h[..]), + NixHash::Sha1(h) => ("fixed:r:sha1", &h[..]), + NixHash::Sha256(h) => ("fixed:r:sha256", &h[..]), + NixHash::Sha512(h) => ("fixed:r:sha512", &h[..]), + }, + CAHash::Text(h) => ("text:sha256", &h[..]), + }; + + format!("{}:{}", algo, nixbase32::encode(hash)) } /// This takes a serde_json::Map and turns it into this structure. This is necessary to do such