From 142c72e070e32710cb691c0c3b79babad595cb62 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Thu, 14 Mar 2024 14:22:16 +0200 Subject: refactor(nix-compat/store_path): add from_name_and_digest_fixed Allow constructing a StorePath with a fixed-size digest. Change-Id: Id7d0b0152f6c55660a8973a02c84afa9188ce3ba Reviewed-on: https://cl.tvl.fyi/c/depot/+/11144 Autosubmit: flokli Reviewed-by: John Ericson Tested-by: BuildkiteCI --- tvix/nix-compat/src/store_path/mod.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'tvix/nix-compat/src/store_path/mod.rs') diff --git a/tvix/nix-compat/src/store_path/mod.rs b/tvix/nix-compat/src/store_path/mod.rs index c744f1a46a..3638ce6e87 100644 --- a/tvix/nix-compat/src/store_path/mod.rs +++ b/tvix/nix-compat/src/store_path/mod.rs @@ -215,10 +215,21 @@ impl<'a> StorePathRef<'a> { } /// Construct a [StorePathRef] from a name and digest. + /// The name is validated, and the digest checked for size. pub fn from_name_and_digest(name: &'a str, digest: &[u8]) -> Result { + let digest_fixed = digest.try_into().map_err(|_| Error::InvalidLength)?; + Self::from_name_and_digest_fixed(name, digest_fixed) + } + + /// Construct a [StorePathRef] from a name and digest of correct length. + /// The name is validated. + pub fn from_name_and_digest_fixed( + name: &'a str, + digest: [u8; DIGEST_SIZE], + ) -> Result { Ok(Self { name: validate_name(name.as_bytes())?, - digest: digest.try_into().map_err(|_| Error::InvalidLength)?, + digest, }) } -- cgit 1.4.1