From c3446da1c7a6dab55c07dae755ef5249deb8649e Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Sat, 14 Oct 2023 21:33:56 +0100 Subject: refactor(nix-compat/nixhash): add Result type alias Change-Id: Id0248047e9642d38afc106629957a2e7608f8c78 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9727 Reviewed-by: Connor Brewster Tested-by: BuildkiteCI Autosubmit: flokli --- tvix/nix-compat/src/nixhash/mod.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'tvix/nix-compat') diff --git a/tvix/nix-compat/src/nixhash/mod.rs b/tvix/nix-compat/src/nixhash/mod.rs index 8cc918b04322..3b2810b8b0bf 100644 --- a/tvix/nix-compat/src/nixhash/mod.rs +++ b/tvix/nix-compat/src/nixhash/mod.rs @@ -17,6 +17,9 @@ pub enum NixHash { Sha512(Box<[u8; 64]>), } +/// convenience Result type for all nixhash parsing Results. +pub type Result = std::result::Result; + impl NixHash { /// returns the algo as [HashAlgo]. pub fn algo(&self) -> HashAlgo { @@ -55,7 +58,7 @@ impl TryFrom<(HashAlgo, &[u8])> for NixHash { /// Constructs a new [NixHash] by specifying [HashAlgo] and digest. /// It can fail if the passed digest length doesn't match what's expected for /// the passed algo. - fn try_from(value: (HashAlgo, &[u8])) -> Result { + fn try_from(value: (HashAlgo, &[u8])) -> Result { let (algo, digest) = value; from_algo_and_digest(algo, digest) } @@ -64,7 +67,7 @@ impl TryFrom<(HashAlgo, &[u8])> for NixHash { /// Constructs a new [NixHash] by specifying [HashAlgo] and digest. // It can fail if the passed digest length doesn't match what's expected for // the passed algo. -pub fn from_algo_and_digest(algo: HashAlgo, digest: &[u8]) -> Result { +pub fn from_algo_and_digest(algo: HashAlgo, digest: &[u8]) -> Result { if digest.len() != algo.digest_length() { return Err(Error::InvalidEncodedDigestLength(digest.len(), algo)); } @@ -117,7 +120,7 @@ pub enum Error { /// The hash is communicated out-of-band, but might also be in-band (in the /// case of a nix hash string or SRI), in which it needs to be consistent with the /// one communicated out-of-band. -pub fn from_str(s: &str, algo_str: Option<&str>) -> Result { +pub fn from_str(s: &str, algo_str: Option<&str>) -> Result { // if algo_str is some, parse or bail out let algo: Option = if let Some(algo_str) = algo_str { Some(algo_str.try_into()?) @@ -167,7 +170,7 @@ pub fn from_str(s: &str, algo_str: Option<&str>) -> Result { } /// Parses a Nix hash string ($algo:$digest) to a NixHash. -pub fn from_nix_str(s: &str) -> Result { +pub fn from_nix_str(s: &str) -> Result { if let Some(rest) = s.strip_prefix("sha1:") { decode_digest(rest.as_bytes(), HashAlgo::Sha1) } else if let Some(rest) = s.strip_prefix("sha256:") { @@ -186,7 +189,7 @@ pub fn from_nix_str(s: &str) -> Result { /// only supports sha256 and sha512 from the spec, and supports sha1 and md5 /// additionally. /// It also accepts SRI strings where the base64 has an with invalid padding. -pub fn from_sri_str(s: &str) -> Result { +pub fn from_sri_str(s: &str) -> Result { // try to find the first occurence of "-" let idx = s.as_bytes().iter().position(|&e| e == b'-'); @@ -229,7 +232,7 @@ pub fn from_sri_str(s: &str) -> Result { /// Decode a plain digest depending on the hash algo specified externally. /// hexlower, nixbase32 and base64 encodings are supported - the encoding is /// inferred from the input length. -fn decode_digest(s: &[u8], algo: HashAlgo) -> Result { +fn decode_digest(s: &[u8], algo: HashAlgo) -> Result { // for the chosen hash algo, calculate the expected (decoded) digest length // (as bytes) let digest = if s.len() == HEXLOWER.encode_len(algo.digest_length()) { -- cgit 1.4.1