diff options
author | Florian Klink <flokli@flokli.de> | 2024-07-27T10·04+0300 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2024-07-27T10·53+0000 |
commit | c8f92c6e9fea7c15b4fc0bce6f087d34b45d4710 (patch) | |
tree | 02222df3f0893dc64e8a97c4918cca25a5a2320b /tvix/nix-compat/src/nixhash/ca_hash.rs | |
parent | 5114504b675cf334dd8476a4a64eb77c5d405896 (diff) |
refactor(tvix/nix-compat): introduce CaHash::algo_str r/8414
This is the `{fixed,fixed:r,text}:{sha*,md5}` prefix used in various string representations. Factor that code out, and use it in the two places it can be used in. Change-Id: Ic9555fa9e1884198d435e55c7f630b8d3ba2a032 Reviewed-on: https://cl.tvl.fyi/c/depot/+/12041 Autosubmit: flokli <flokli@flokli.de> Tested-by: BuildkiteCI Reviewed-by: Brian Olsen <me@griff.name>
Diffstat (limited to 'tvix/nix-compat/src/nixhash/ca_hash.rs')
-rw-r--r-- | tvix/nix-compat/src/nixhash/ca_hash.rs | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/tvix/nix-compat/src/nixhash/ca_hash.rs b/tvix/nix-compat/src/nixhash/ca_hash.rs index 2bf5f966cefe..423531696afe 100644 --- a/tvix/nix-compat/src/nixhash/ca_hash.rs +++ b/tvix/nix-compat/src/nixhash/ca_hash.rs @@ -47,6 +47,26 @@ impl CAHash { } } + /// Returns a colon-separated string consisting of mode, recursiveness and + /// hash algo. Used as a prefix in various string representations. + pub fn algo_str(&self) -> &'static str { + match self.mode() { + HashMode::Flat => match self.hash().as_ref() { + NixHash::Md5(_) => "fixed:md5", + NixHash::Sha1(_) => "fixed:sha1", + NixHash::Sha256(_) => "fixed:sha256", + NixHash::Sha512(_) => "fixed:sha512", + }, + HashMode::Nar => match self.hash().as_ref() { + NixHash::Md5(_) => "fixed:r:md5", + NixHash::Sha1(_) => "fixed:r:sha1", + NixHash::Sha256(_) => "fixed:r:sha256", + NixHash::Sha512(_) => "fixed:r:sha512", + }, + HashMode::Text => "text:sha256", + } + } + /// Constructs a [CAHash] from the textual representation, /// which is one of the three: /// - `text:sha256:$nixbase32sha256digest` @@ -76,13 +96,11 @@ impl CAHash { /// Formats a [CAHash] in the Nix default hash format, which is the format /// that's used in NARInfos for example. pub fn to_nix_nixbase32_string(&self) -> String { - match self { - CAHash::Flat(nh) => format!("fixed:{}", nh.to_nix_nixbase32_string()), - CAHash::Nar(nh) => format!("fixed:r:{}", nh.to_nix_nixbase32_string()), - CAHash::Text(digest) => { - format!("text:sha256:{}", nixbase32::encode(digest)) - } - } + format!( + "{}:{}", + self.algo_str(), + nixbase32::encode(self.hash().digest_as_bytes()) + ) } /// This takes a serde_json::Map and turns it into this structure. This is necessary to do such |