refactor(nix-compat/nixhash): drop NixHashResult type alias

This one is overkill. `NixHashResult<NixHash>` takes exactly as many
characters as `Result<NixHash, Error>`, so removing the type alias
actually removes the total amount of code.

The only external reference to it is somewhere that should probably live
in nixhash::ca_hash.

Change-Id: I0c4a149294d33129a67cb1b699cc8a645c7c18e1
Reviewed-on: https://cl.snix.dev/c/snix/+/30562
Autosubmit: Florian Klink <flokli@flokli.de>
Tested-by: besadii
Reviewed-by: edef <edef@edef.eu>
This commit is contained in:
Florian Klink 2025-06-04 22:36:13 +03:00 committed by clbot
parent 4a63d85b06
commit 16136380ec
2 changed files with 6 additions and 8 deletions

View file

@ -79,10 +79,11 @@ pub fn parse_streaming(i: &[u8]) -> (Result<Derivation, Error<&[u8]>>, &[u8]) {
/// Consume a string containing the algo, and optionally a `r:` /// Consume a string containing the algo, and optionally a `r:`
/// prefix, and a digest (bytes), return a [CAHash::Nar] or [CAHash::Flat]. /// prefix, and a digest (bytes), return a [CAHash::Nar] or [CAHash::Flat].
// TODO: This maybe should belong in ca_hash.rs
fn from_algo_and_mode_and_digest<B: AsRef<[u8]>>( fn from_algo_and_mode_and_digest<B: AsRef<[u8]>>(
algo_and_mode: &str, algo_and_mode: &str,
digest: B, digest: B,
) -> crate::nixhash::NixHashResult<CAHash> { ) -> Result<CAHash, nixhash::Error> {
Ok(match algo_and_mode.strip_prefix("r:") { Ok(match algo_and_mode.strip_prefix("r:") {
Some(algo) => nixhash::CAHash::Nar(NixHash::from_algo_and_digest( Some(algo) => nixhash::CAHash::Nar(NixHash::from_algo_and_digest(
algo.try_into()?, algo.try_into()?,

View file

@ -85,9 +85,6 @@ impl Display for NixHash {
} }
} }
/// convenience Result type for all nixhash parsing Results.
pub type NixHashResult<V> = std::result::Result<V, Error>;
impl NixHash { impl NixHash {
/// returns the algo as [HashAlgo]. /// returns the algo as [HashAlgo].
pub fn algo(&self) -> HashAlgo { pub fn algo(&self) -> HashAlgo {
@ -112,7 +109,7 @@ impl NixHash {
/// Constructs a new [NixHash] by specifying [HashAlgo] and digest. /// Constructs a new [NixHash] by specifying [HashAlgo] and digest.
/// It can fail if the passed digest length doesn't match what's expected for /// It can fail if the passed digest length doesn't match what's expected for
/// the passed algo. /// the passed algo.
pub fn from_algo_and_digest(algo: HashAlgo, digest: &[u8]) -> NixHashResult<NixHash> { pub fn from_algo_and_digest(algo: HashAlgo, digest: &[u8]) -> Result<NixHash, Error> {
if digest.len() != algo.digest_length() { if digest.len() != algo.digest_length() {
return Err(Error::InvalidDigestLength(digest.len(), algo)); return Err(Error::InvalidDigestLength(digest.len(), algo));
} }
@ -153,7 +150,7 @@ impl NixHash {
/// Parses a Nix SRI string to a NixHash. /// Parses a Nix SRI string to a NixHash.
/// (See caveats in [Self] on the deviations from the SRI spec) /// (See caveats in [Self] on the deviations from the SRI spec)
pub fn from_sri(s: &str) -> NixHashResult<NixHash> { pub fn from_sri(s: &str) -> Result<NixHash, Error> {
// split at the first occurence of "-" // split at the first occurence of "-"
let (algo_str, digest_str) = s.split_once('-').ok_or(Error::InvalidSRI)?; let (algo_str, digest_str) = s.split_once('-').ok_or(Error::InvalidSRI)?;
@ -225,7 +222,7 @@ impl NixHash {
/// the "digest only" format is used. /// the "digest only" format is used.
/// In other cases, consistency of an optionally externally configured algo /// In other cases, consistency of an optionally externally configured algo
/// with the one parsed is ensured. /// with the one parsed is ensured.
pub fn from_str(s: &str, want_algo: Option<HashAlgo>) -> NixHashResult<NixHash> { pub fn from_str(s: &str, want_algo: Option<HashAlgo>) -> Result<NixHash, Error> {
// Check for SRI hashes. // Check for SRI hashes.
if let Ok(parsed_nixhash) = Self::from_sri(s) { if let Ok(parsed_nixhash) = Self::from_sri(s) {
// ensure the algo matches with what has been passed externally, if so. // ensure the algo matches with what has been passed externally, if so.
@ -291,7 +288,7 @@ pub enum Error {
/// Decode a plain digest depending on the hash algo specified externally. /// Decode a plain digest depending on the hash algo specified externally.
/// hexlower, nixbase32 and base64 encodings are supported - the encoding is /// hexlower, nixbase32 and base64 encodings are supported - the encoding is
/// inferred from the input length. /// inferred from the input length.
fn decode_digest(s: &[u8], algo: HashAlgo) -> NixHashResult<NixHash> { fn decode_digest(s: &[u8], algo: HashAlgo) -> Result<NixHash, Error> {
// for the chosen hash algo, calculate the expected (decoded) digest length // for the chosen hash algo, calculate the expected (decoded) digest length
// (as bytes) // (as bytes)
let digest = if s.len() == HEXLOWER.encode_len(algo.digest_length()) { let digest = if s.len() == HEXLOWER.encode_len(algo.digest_length()) {