about summary refs log tree commit diff
path: root/tvix/nix-compat/src
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-10-14T20·33+0100
committerclbot <clbot@tvl.fyi>2023-10-15T02·21+0000
commitc3446da1c7a6dab55c07dae755ef5249deb8649e (patch)
tree60ce1cef805c192a10e596f142f0fc6ea63d8f5d /tvix/nix-compat/src
parentcb1a14334a7722e868058a6dda2176ae2f1bd52b (diff)
refactor(nix-compat/nixhash): add Result type alias r/6812
Change-Id: Id0248047e9642d38afc106629957a2e7608f8c78
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9727
Reviewed-by: Connor Brewster <cbrewster@hey.com>
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/nix-compat/src')
-rw-r--r--tvix/nix-compat/src/nixhash/mod.rs15
1 files changed, 9 insertions, 6 deletions
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<V> = std::result::Result<V, Error>;
+
 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<Self, Self::Error> {
+    fn try_from(value: (HashAlgo, &[u8])) -> Result<Self> {
         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<NixHash, Error> {
+pub fn from_algo_and_digest(algo: HashAlgo, digest: &[u8]) -> Result<NixHash> {
     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<NixHash, Error> {
+pub fn from_str(s: &str, algo_str: Option<&str>) -> Result<NixHash> {
     // if algo_str is some, parse or bail out
     let algo: Option<HashAlgo> = 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<NixHash, Error> {
 }
 
 /// Parses a Nix hash string ($algo:$digest) to a NixHash.
-pub fn from_nix_str(s: &str) -> Result<NixHash, Error> {
+pub fn from_nix_str(s: &str) -> Result<NixHash> {
     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<NixHash, Error> {
 /// 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<NixHash, Error> {
+pub fn from_sri_str(s: &str) -> Result<NixHash> {
     // 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<NixHash, Error> {
 /// 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<NixHash, Error> {
+fn decode_digest(s: &[u8], algo: HashAlgo) -> Result<NixHash> {
     // for the chosen hash algo, calculate the expected (decoded) digest length
     // (as bytes)
     let digest = if s.len() == HEXLOWER.encode_len(algo.digest_length()) {