From 7b6b94c5ca2d34c4885d627aeaf52bd3039409f6 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Tue, 3 Jun 2025 22:28:26 +0300 Subject: [PATCH] refactor(nix-compat/nixhash): use a bit more map and ok_or_else This looks more readable like this. Change-Id: Iaa750fae66c7263612f169405eb7d38fb9541b04 Reviewed-on: https://cl.snix.dev/c/snix/+/30552 Autosubmit: Florian Klink Reviewed-by: Ilan Joselevich Tested-by: besadii --- snix/nix-compat/src/nixhash/mod.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/snix/nix-compat/src/nixhash/mod.rs b/snix/nix-compat/src/nixhash/mod.rs index 507242690..edb34e57b 100644 --- a/snix/nix-compat/src/nixhash/mod.rs +++ b/snix/nix-compat/src/nixhash/mod.rs @@ -210,11 +210,7 @@ pub enum Error { /// one communicated out-of-band. pub fn from_str(s: &str, algo_str: Option<&str>) -> NixHashResult { // if algo_str is some, parse or bail out - let algo: Option = if let Some(algo_str) = algo_str { - Some(algo_str.try_into()?) - } else { - None - }; + let algo: Option = algo_str.map(HashAlgo::try_from).transpose()?; // Peek at the beginning of the string to detect SRI hashes. if s.starts_with("sha1-") @@ -240,6 +236,7 @@ pub fn from_str(s: &str, algo_str: Option<&str>) -> NixHashResult { || s.starts_with("md5:") { let parsed_nixhash = from_nix_str(s)?; + // ensure the algo matches with what has been passed externally, if so. if let Some(algo) = algo { if algo != parsed_nixhash.algo() { @@ -250,11 +247,8 @@ pub fn from_str(s: &str, algo_str: Option<&str>) -> NixHashResult { } // Neither of these, assume a bare digest, so there MUST be an externally-passed algo. - match algo { - // Fail if there isn't. - None => Err(Error::MissingInlineHashAlgo(s.to_string())), - Some(algo) => decode_digest(s.as_bytes(), algo), - } + let algo = algo.ok_or_else(|| Error::MissingInlineHashAlgo(s.to_string()))?; + decode_digest(s.as_bytes(), algo) } /// Parses a Nix hash string ($algo:$digest) to a NixHash.