refactor(nix-compat/nixhash/ca_hash): inline algo_str

This is only used in to_nix_nixbase32_string, and in one place in
narinfo2parquet (though it might be sliced wrongly there too).

This is a partial revert of cl/12041, at least for the narinfo2parquet
parts.

Change-Id: Ic8c57373f7ab79043a491267e8484fa8399cea04
Reviewed-on: https://cl.snix.dev/c/snix/+/30564
Tested-by: besadii
Autosubmit: Florian Klink <flokli@flokli.de>
Reviewed-by: edef <edef@edef.eu>
This commit is contained in:
Florian Klink 2025-06-04 23:05:42 +03:00 committed by clbot
parent 63036b3c5e
commit b90da345ab
2 changed files with 36 additions and 23 deletions

View file

@ -12,6 +12,7 @@ use jemallocator::Jemalloc;
use nix_compat::{
narinfo::{self, NarInfo},
nixbase32,
nixhash::{CAHash, NixHash},
};
use polars::{io::parquet::ParquetWriter, prelude::*};
use std::{
@ -192,8 +193,26 @@ impl FrameBuilder {
}));
if let Some(ca) = &entry.ca {
self.ca_algo.append_value(ca.algo_str());
self.ca_hash.append_value(ca.hash().digest_as_bytes());
// decompose the CAHash into algo and hash parts
// TODO(edef): split this up differently?
let (algo, hash) = match ca {
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[..]),
},
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[..]),
};
self.ca_algo.append_value(algo);
self.ca_hash.append_value(hash);
} else {
self.ca_algo.append_null();
self.ca_hash.append_null();

View file

@ -47,26 +47,6 @@ impl CAHash {
}
}
/// Returns a colon-separated string consisting of mode, recursiveness and
/// hash algo. Used as a prefix in various string representations.
pub fn algo_str(&self) -> &'static str {
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",
}
}
/// Constructs a [CAHash] from the textual representation,
/// which is one of the three:
/// - `text:sha256:$nixbase32sha256digest`
@ -99,7 +79,21 @@ impl CAHash {
pub fn to_nix_nixbase32_string(&self) -> String {
format!(
"{}:{}",
self.algo_str(),
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",
},
nixbase32::encode(self.hash().digest_as_bytes())
)
}