about summary refs log tree commit diff
path: root/tvix/nix-compat/src/store_path/mod.rs
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-10-10T22·22+0200
committerflokli <flokli@flokli.de>2023-10-14T13·23+0000
commit2d2c4322d93308ddffe1647466abd91025af6a68 (patch)
tree37ead8574df6eb98d76ca95636b50bf8fe1945cb /tvix/nix-compat/src/store_path/mod.rs
parent5f8eb4eeaaad31aedc45efee3143e6b0bbc982a4 (diff)
feat(tvix/store/protos): add Deriver field to PathInfo r/6805
This uses the newly introduced StorePath message type to add a Deriver
field to the PathInfo message.

Support for validation is added to both the golang and rust
implementation. This includes extending unit tests.

Change-Id: Ifc3eb3263fa25b9eec260db354cd74234c40af7e
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9647
Reviewed-by: Connor Brewster <cbrewster@hey.com>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/nix-compat/src/store_path/mod.rs')
-rw-r--r--tvix/nix-compat/src/store_path/mod.rs16
1 files changed, 16 insertions, 0 deletions
diff --git a/tvix/nix-compat/src/store_path/mod.rs b/tvix/nix-compat/src/store_path/mod.rs
index bd856b376522..9253b2a8d741 100644
--- a/tvix/nix-compat/src/store_path/mod.rs
+++ b/tvix/nix-compat/src/store_path/mod.rs
@@ -116,6 +116,14 @@ impl StorePath {
         }
     }
 
+    /// Construct a [StorePath] from a name and digest.
+    pub fn from_name_and_digest(name: String, digest: &[u8]) -> Result<StorePath, Error> {
+        Ok(Self {
+            name: validate_name(name.as_bytes())?,
+            digest: digest.try_into().map_err(|_| Error::InvalidLength())?,
+        })
+    }
+
     /// Decompose a string into a [StorePath] and a [PathBuf] containing the
     /// rest of the path, or an error.
     #[cfg(target_family = "unix")]
@@ -179,6 +187,14 @@ pub(crate) fn validate_name(s: &[u8]) -> Result<String, Error> {
     Ok(String::from_utf8(s.to_vec()).unwrap())
 }
 
+/// Ensures the StorePath fulfils the requirements for store paths.
+/// Useful when populating the struct manually instead of parsing.
+pub fn validate(s: &StorePath) -> Result<(), Error> {
+    validate_name(s.name.as_bytes())?;
+
+    Ok(())
+}
+
 impl fmt::Display for StorePath {
     /// The string representation of a store path starts with a digest (20
     /// bytes), [crate::nixbase32]-encoded, followed by a `-`,