about summary refs log tree commit diff
path: root/tvix/store/src/nixpath.rs
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-01-02T11·32+0100
committerflokli <flokli@flokli.de>2023-01-03T13·03+0000
commit8e2b1de3d5e687eab1cc36117b12d68553588562 (patch)
treea3c8bb4b03a58c8ba0b2a2cdf77ae694c263d822 /tvix/store/src/nixpath.rs
parent2df642231cfa2ea59869a5112201e3f38cfeb271 (diff)
feat(tvix/store/nixpath): DIGEST_SIZE public, use more consts r/5572
Rename PATH_HASH_SIZE to DIGEST_SIZE.

It's a digest, not hash (we don't necessarily have the internal hash
state anymore), and the fact it's about (Nix)Paths is already visible
from the module name.

Also expose ENCODED_DIGEST_SIZE, so we don't need to do the calculation
inside from_string() method, and it becomes more clear this is a
constant.

Change-Id: I0e7577dd7a6e503039037b986313b214e995d826
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7725
Reviewed-by: tazjin <tazjin@tvl.su>
Reviewed-by: jrhahn <mail.jhahn@gmail.com>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/store/src/nixpath.rs')
-rw-r--r--tvix/store/src/nixpath.rs30
1 files changed, 18 insertions, 12 deletions
diff --git a/tvix/store/src/nixpath.rs b/tvix/store/src/nixpath.rs
index b619126868d5..ed0d39864de4 100644
--- a/tvix/store/src/nixpath.rs
+++ b/tvix/store/src/nixpath.rs
@@ -3,7 +3,10 @@ use data_encoding::DecodeError;
 use std::fmt;
 use thiserror::Error;
 
-const PATH_HASH_SIZE: usize = 20;
+pub const DIGEST_SIZE: usize = 20;
+// lazy_static doesn't allow us to call NIXBASE32.encode_len(), so we ran it
+// manually and have an assert in the tests.
+pub const ENCODED_DIGEST_SIZE: usize = 32;
 
 /// Errors that can occur during the validation of name characters.
 #[derive(Debug, PartialEq, Eq, Error)]
@@ -18,39 +21,36 @@ pub enum ParseNixPathError {
 
 #[derive(Debug, PartialEq, Eq)]
 pub struct NixPath {
-    pub digest: [u8; PATH_HASH_SIZE],
+    pub digest: [u8; DIGEST_SIZE],
     pub name: String,
 }
 
 impl NixPath {
     pub fn from_string(s: &str) -> Result<NixPath, ParseNixPathError> {
-        let encoded_path_hash_size = NIXBASE32.encode_len(PATH_HASH_SIZE);
-        let name_offset = encoded_path_hash_size + 1;
-
         // the whole string needs to be at least:
         //
         // - 32 characters (encoded hash)
         // - 1 dash
         // - 1 character for the name
-        if s.len() < name_offset + 1 {
+        if s.len() < ENCODED_DIGEST_SIZE + 2 {
             return Err(ParseNixPathError::InvalidName("".to_string()));
         }
 
-        let digest = match NIXBASE32.decode(s[..encoded_path_hash_size].as_bytes()) {
+        let digest = match NIXBASE32.decode(s[..ENCODED_DIGEST_SIZE].as_bytes()) {
             Ok(decoded) => decoded,
             Err(decoder_error) => {
                 return Err(ParseNixPathError::InvalidHashEncoding(decoder_error))
             }
         };
 
-        if s.as_bytes()[encoded_path_hash_size] != b'-' {
+        if s.as_bytes()[ENCODED_DIGEST_SIZE] != b'-' {
             return Err(ParseNixPathError::MissingDash());
         }
 
-        NixPath::validate_characters(&s[name_offset..])?;
+        NixPath::validate_characters(&s[ENCODED_DIGEST_SIZE + 2..])?;
 
         Ok(NixPath {
-            name: s[name_offset..].to_string(),
+            name: s[ENCODED_DIGEST_SIZE + 1..].to_string(),
             digest: digest.try_into().expect("size is known"),
         })
     }
@@ -88,18 +88,24 @@ impl fmt::Display for NixPath {
 
 #[cfg(test)]
 mod tests {
-    use crate::nixpath::PATH_HASH_SIZE;
+    use crate::nixbase32::NIXBASE32;
+    use crate::nixpath::{DIGEST_SIZE, ENCODED_DIGEST_SIZE};
 
     use super::NixPath;
 
     #[test]
+    fn encoded_digest_size() {
+        assert_eq!(ENCODED_DIGEST_SIZE, NIXBASE32.encode_len(DIGEST_SIZE));
+    }
+
+    #[test]
     fn happy_path() {
         let example_nix_path_str =
             "00bgd045z0d4icpbc2yyz4gx48ak44la-net-tools-1.60_p20170221182432";
         let nixpath =
             NixPath::from_string(&example_nix_path_str).expect("Error parsing example string");
 
-        let expected_digest: [u8; PATH_HASH_SIZE] = [
+        let expected_digest: [u8; DIGEST_SIZE] = [
             0x8a, 0x12, 0x32, 0x15, 0x22, 0xfd, 0x91, 0xef, 0xbd, 0x60, 0xeb, 0xb2, 0x48, 0x1a,
             0xf8, 0x85, 0x80, 0xf6, 0x16, 0x00,
         ];