From 91d5745c3dbb3eb118be969a80e0608794b0e59d Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Thu, 22 Feb 2024 16:07:00 +0700 Subject: refactor(nix-compat/store_path): simplify build_ca_path Move the the `fixed:out:[r:]{}:` generation to a helper function, use matches! for more clarity. Change-Id: I4e930c42aacbf5c7451d1f8c8c80ccb4c45389f0 Reviewed-on: https://cl.tvl.fyi/c/depot/+/11006 Tested-by: BuildkiteCI Reviewed-by: aspen Autosubmit: flokli --- tvix/nix-compat/src/store_path/utils.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) (limited to 'tvix/nix-compat/src') diff --git a/tvix/nix-compat/src/store_path/utils.rs b/tvix/nix-compat/src/store_path/utils.rs index 0b75ef50647c..2289594e3a10 100644 --- a/tvix/nix-compat/src/store_path/utils.rs +++ b/tvix/nix-compat/src/store_path/utils.rs @@ -62,10 +62,8 @@ pub fn build_ca_path<'a, S: AsRef, I: IntoIterator>( self_reference: bool, ) -> Result, BuildStorePathError> { // self references are only allowed for CAHash::Nar(NixHash::Sha256(_)). - if self_reference { - let CAHash::Nar(NixHash::Sha256(_)) = ca_hash else { - return Err(BuildStorePathError::InvalidReference()); - }; + if self_reference && matches!(ca_hash, CAHash::Nar(NixHash::Sha256(_))) { + return Err(BuildStorePathError::InvalidReference()); } let (ty, hash) = match &ca_hash { @@ -86,11 +84,7 @@ pub fn build_ca_path<'a, S: AsRef, I: IntoIterator>( ( "output:out".to_string(), - NixHash::Sha256( - Sha256::new_with_prefix(format!("fixed:out:r:{}:", hash.to_nix_hex_string())) - .finalize() - .into(), - ), + NixHash::Sha256(fixed_out_digest("fixed:out:r", hash)), ) } // CaHash::Flat is using something very similar, except the `r:` prefix. @@ -101,11 +95,7 @@ pub fn build_ca_path<'a, S: AsRef, I: IntoIterator>( ( "output:out".to_string(), - NixHash::Sha256( - Sha256::new_with_prefix(format!("fixed:out:{}:", hash.to_nix_hex_string())) - .finalize() - .into(), - ), + NixHash::Sha256(fixed_out_digest("fixed:out", hash)), ) } }; @@ -114,6 +104,14 @@ pub fn build_ca_path<'a, S: AsRef, I: IntoIterator>( .map_err(BuildStorePathError::InvalidStorePath) } +/// Helper function, used in [build_ca_path] for the non-sha256 [CAHash::Nar] +/// and [CAHash::Flat]. +fn fixed_out_digest(prefix: &str, hash: &NixHash) -> [u8; 32] { + Sha256::new_with_prefix(format!("{}:{}:", prefix, hash.to_nix_hex_string())) + .finalize() + .into() +} + /// For given NAR sha256 digest and name, return the new [StorePathRef] this /// would have, or an error, in case the name is invalid. pub fn build_nar_based_store_path<'a>( -- cgit 1.4.1