feat(tvix/nix-compat): Extract to_plain_hex_string

Towards https://b.tvl.fyi/issues/264

Change-Id: Ibde971bfb6baa97b5c678d84ce1941189bc59f6f
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10969
Reviewed-by: flokli <flokli@flokli.de>
Autosubmit: Peter Kolloch <info@eigenvalue.net>
Tested-by: BuildkiteCI
This commit is contained in:
Peter Kolloch 2024-02-20 00:17:40 +07:00 committed by clbot
parent df73d5f242
commit 46d89f899f

View file

@ -61,11 +61,7 @@ impl NixHash {
/// Formats a [NixHash] in the Nix default hash format, /// Formats a [NixHash] in the Nix default hash format,
/// which is the algo, followed by a colon, then the lower hex encoded digest. /// which is the algo, followed by a colon, then the lower hex encoded digest.
pub fn to_nix_hex_string(&self) -> String { pub fn to_nix_hex_string(&self) -> String {
format!( format!("{}:{}", self.algo(), self.to_plain_hex_string())
"{}:{}",
self.algo(),
HEXLOWER.encode(self.digest_as_bytes())
)
} }
/// Formats a [NixHash] in the format that's used inside CAHash, /// Formats a [NixHash] in the format that's used inside CAHash,
@ -77,6 +73,11 @@ impl NixHash {
nixbase32::encode(self.digest_as_bytes()) nixbase32::encode(self.digest_as_bytes())
) )
} }
/// Returns the digest as a hex string -- without any algorithm prefix.
pub fn to_plain_hex_string(&self) -> String {
HEXLOWER.encode(self.digest_as_bytes())
}
} }
impl TryFrom<(HashAlgo, &[u8])> for NixHash { impl TryFrom<(HashAlgo, &[u8])> for NixHash {