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 <flokli@flokli.de>
Reviewed-by: edef <edef@edef.eu>
Tested-by: besadii
This commit is contained in:
Florian Klink 2025-06-05 22:30:21 +03:00 committed by clbot
parent b90da345ab
commit 3c23b323d5

View file

@ -77,25 +77,23 @@ impl CAHash {
/// Formats a [CAHash] in the Nix default hash format, which is the format /// Formats a [CAHash] in the Nix default hash format, which is the format
/// that's used in NARInfos for example. /// that's used in NARInfos for example.
pub fn to_nix_nixbase32_string(&self) -> String { pub fn to_nix_nixbase32_string(&self) -> String {
format!( let (algo, hash) = match self {
"{}:{}", CAHash::Flat(h) => match h {
match self.mode() { NixHash::Md5(h) => ("fixed:md5", &h[..]),
HashMode::Flat => match self.hash().as_ref() { NixHash::Sha1(h) => ("fixed:sha1", &h[..]),
NixHash::Md5(_) => "fixed:md5", NixHash::Sha256(h) => ("fixed:sha256", &h[..]),
NixHash::Sha1(_) => "fixed:sha1", NixHash::Sha512(h) => ("fixed:sha512", &h[..]),
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",
}, },
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 /// This takes a serde_json::Map and turns it into this structure. This is necessary to do such