about summary refs log tree commit diff
path: root/tvix/nix-compat/src/store_path/mod.rs
diff options
context:
space:
mode:
authorJohn Ericson <John.Ericson@Obsidian.Systems>2023-03-31T14·20-0400
committerJohn Ericson <git@johnericson.me>2023-04-09T15·12+0000
commit26c68f8e892633bde4aeebbfc0e4ae7ee571687d (patch)
treee6ee1bcf805ceb83b974f6d1c6a8e7d0b30a9b1f /tvix/nix-compat/src/store_path/mod.rs
parentb4670bfbd16dd80fb52e61e79b4aa6e1d0453570 (diff)
refactor(nix-compat): Properly encapsulate store path construction r/6088
Before there was code scattered about (e.g. text hashing module and
derivation output computation) constructing store paths from low level
building blocks --- there was some duplication and it was easy to make
nonsense store paths.

Now, we have roughly the same "safe-ish" ways of constructing them as
C++ Nix, and only those are exposed:

- Make text hashed content-addressed store paths

- Make other content-addressed store paths

- Make input-addressed fixed output hashes

Change-Id: I122a3ee0802b4f45ae386306b95b698991be89c8
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8411
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/nix-compat/src/store_path/mod.rs')
-rw-r--r--tvix/nix-compat/src/store_path/mod.rs30
1 files changed, 20 insertions, 10 deletions
diff --git a/tvix/nix-compat/src/store_path/mod.rs b/tvix/nix-compat/src/store_path/mod.rs
index 0e004ccd7a..7fe425d8ed 100644
--- a/tvix/nix-compat/src/store_path/mod.rs
+++ b/tvix/nix-compat/src/store_path/mod.rs
@@ -4,10 +4,7 @@ use thiserror::Error;
 
 mod utils;
 
-pub use utils::{
-    build_store_path_from_fingerprint, build_store_path_from_references, compress_hash,
-    hash_placeholder,
-};
+pub use utils::*;
 
 pub const DIGEST_SIZE: usize = 20;
 // lazy_static doesn't allow us to call NIXBASE32.encode_len(), so we ran it
@@ -19,19 +16,32 @@ pub const ENCODED_DIGEST_SIZE: usize = 32;
 pub const STORE_DIR: &str = "/nix/store";
 pub const STORE_DIR_WITH_SLASH: &str = "/nix/store/";
 
-/// Errors that can occur during the validation of name characters.
+/// Errors that can occur when parsing a literal store path
 #[derive(Debug, PartialEq, Eq, Error)]
 pub enum Error {
     #[error("Dash is missing between hash and name")]
     MissingDash(),
     #[error("Hash encoding is invalid: {0}")]
     InvalidHashEncoding(Nixbase32DecodeError),
-    #[error("Invalid name: {0}")]
-    InvalidName(String),
+    #[error("{0}")]
+    InvalidName(NameError),
     #[error("Tried to parse an absolute path which was missing the store dir prefix.")]
     MissingStoreDir(),
 }
 
+/// Errors that can occur during the validation of name characters.
+#[derive(Debug, PartialEq, Eq, Error)]
+pub enum NameError {
+    #[error("Invalid name: {0}")]
+    InvalidName(String),
+}
+
+impl From<NameError> for Error {
+    fn from(e: NameError) -> Self {
+        Self::InvalidName(e)
+    }
+}
+
 /// Represents a path in the Nix store (a direct child of [STORE_DIR]).
 ///
 /// It starts with a digest (20 bytes), [crate::nixbase32]-encoded,
@@ -56,7 +66,7 @@ impl StorePath {
         // - 1 dash
         // - 1 character for the name
         if s.len() < ENCODED_DIGEST_SIZE + 2 {
-            return Err(Error::InvalidName("".to_string()));
+            Err(NameError::InvalidName("".to_string()))?;
         }
 
         let digest = match nixbase32::decode(s[..ENCODED_DIGEST_SIZE].as_bytes()) {
@@ -92,7 +102,7 @@ impl StorePath {
     }
 
     /// Checks a given &str to match the restrictions for store path names.
-    pub fn validate_name(s: &str) -> Result<(), Error> {
+    pub fn validate_name(s: &str) -> Result<(), NameError> {
         for c in s.chars() {
             if c.is_ascii_alphanumeric()
                 || c == '-'
@@ -105,7 +115,7 @@ impl StorePath {
                 continue;
             }
 
-            return Err(Error::InvalidName(s.to_string()));
+            return Err(NameError::InvalidName(s.to_string()));
         }
 
         Ok(())