diff options
author | Florian Klink <flokli@flokli.de> | 2023-12-09T10·48+0200 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2023-12-09T12·58+0000 |
commit | 0907420600ba0f9270065dd83e81fb8f56dfda71 (patch) | |
tree | 4bacffb7e7b9b1352f9def180f4a990f395610ac /tvix/nix-compat/src | |
parent | 5855959d879a6f57ff683f752fdc4d0c47427382 (diff) |
refactor(nix-compat/store_path): from_absolute_path to StorePathRef r/7132
The only non-test usage was only checking for the error case, and we can still convert this to an owned StorePath by calling to_owned() on StorePathRef. Change-Id: I9f67a759e580c9c429c96896bcdd295392aa5a2a Reviewed-on: https://cl.tvl.fyi/c/depot/+/10225 Reviewed-by: tazjin <tazjin@tvl.su> Autosubmit: flokli <flokli@flokli.de> Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/nix-compat/src')
-rw-r--r-- | tvix/nix-compat/src/derivation/output.rs | 4 | ||||
-rw-r--r-- | tvix/nix-compat/src/derivation/validate.rs | 6 | ||||
-rw-r--r-- | tvix/nix-compat/src/store_path/mod.rs | 29 |
3 files changed, 20 insertions, 19 deletions
diff --git a/tvix/nix-compat/src/derivation/output.rs b/tvix/nix-compat/src/derivation/output.rs index c13f94859dc1..b7fa1ac379b0 100644 --- a/tvix/nix-compat/src/derivation/output.rs +++ b/tvix/nix-compat/src/derivation/output.rs @@ -1,6 +1,6 @@ use crate::derivation::OutputError; use crate::nixhash::CAHash; -use crate::store_path::StorePath; +use crate::store_path::StorePathRef; use serde::{Deserialize, Serialize}; use serde_json::Map; @@ -51,7 +51,7 @@ impl Output { } if validate_output_paths { - if let Err(e) = StorePath::from_absolute_path(self.path.as_bytes()) { + if let Err(e) = StorePathRef::from_absolute_path(self.path.as_bytes()) { return Err(OutputError::InvalidOutputPath(self.path.to_string(), e)); } } diff --git a/tvix/nix-compat/src/derivation/validate.rs b/tvix/nix-compat/src/derivation/validate.rs index 37ddf9017138..b88df1f621cd 100644 --- a/tvix/nix-compat/src/derivation/validate.rs +++ b/tvix/nix-compat/src/derivation/validate.rs @@ -1,5 +1,5 @@ use crate::derivation::{Derivation, DerivationError}; -use crate::store_path::{self, StorePath}; +use crate::store_path::{self, StorePathRef}; impl Derivation { /// validate ensures a Derivation struct is properly populated, @@ -53,7 +53,7 @@ impl Derivation { // Validate all input_derivations for (input_derivation_path, output_names) in &self.input_derivations { // Validate input_derivation_path - if let Err(e) = StorePath::from_absolute_path(input_derivation_path.as_bytes()) { + if let Err(e) = StorePathRef::from_absolute_path(input_derivation_path.as_bytes()) { return Err(DerivationError::InvalidInputDerivationPath( input_derivation_path.to_string(), e, @@ -96,7 +96,7 @@ impl Derivation { // Validate all input_sources for input_source in self.input_sources.iter() { - if let Err(e) = StorePath::from_absolute_path(input_source.as_bytes()) { + if let Err(e) = StorePathRef::from_absolute_path(input_source.as_bytes()) { return Err(DerivationError::InvalidInputSourcesPath( input_source.to_string(), e, diff --git a/tvix/nix-compat/src/store_path/mod.rs b/tvix/nix-compat/src/store_path/mod.rs index 350e65d83fb0..9dfbb01630d7 100644 --- a/tvix/nix-compat/src/store_path/mod.rs +++ b/tvix/nix-compat/src/store_path/mod.rs @@ -97,16 +97,6 @@ impl StorePath { Ok(StorePathRef::from_bytes(s)?.to_owned()) } - /// Construct a [StorePath] from an absolute store path string. - /// This is equivalent to calling [StorePath::from_bytes], but stripping the - /// [STORE_DIR_WITH_SLASH] prefix before. - pub fn from_absolute_path(s: &[u8]) -> Result<StorePath, Error> { - match s.strip_prefix(STORE_DIR_WITH_SLASH.as_bytes()) { - Some(s_stripped) => Self::from_bytes(s_stripped), - None => Err(Error::MissingStoreDir), - } - } - /// Decompose a string into a [StorePath] and a [PathBuf] containing the /// rest of the path, or an error. #[cfg(target_family = "unix")] @@ -187,6 +177,16 @@ impl<'a> StorePathRef<'a> { }) } + /// Construct a [StorePathRef] from an absolute store path string. + /// This is equivalent to calling [StorePathRef::from_bytes], but stripping + /// the [STORE_DIR_WITH_SLASH] prefix before. + pub fn from_absolute_path(s: &'a [u8]) -> Result<Self, Error> { + match s.strip_prefix(STORE_DIR_WITH_SLASH.as_bytes()) { + Some(s_stripped) => Self::from_bytes(s_stripped), + None => Err(Error::MissingStoreDir), + } + } + /// Construct a [StorePathRef] by passing the `$digest-$name` string /// that comes after [STORE_DIR_WITH_SLASH]. pub fn from_bytes(s: &'a [u8]) -> Result<Self, Error> { @@ -284,7 +284,7 @@ impl fmt::Display for StorePathRef<'_> { mod tests { use std::path::PathBuf; - use crate::store_path::DIGEST_SIZE; + use crate::store_path::{StorePathRef, DIGEST_SIZE}; use hex_literal::hex; use test_case::test_case; @@ -360,10 +360,11 @@ mod tests { let nixpath_expected = StorePath::from_bytes(example_nix_path_str.as_bytes()).expect("must parse"); - let nixpath_actual = StorePath::from_absolute_path( + let nixpath_actual = StorePathRef::from_absolute_path( "/nix/store/00bgd045z0d4icpbc2yyz4gx48ak44la-net-tools-1.60_p20170221182432".as_bytes(), ) - .expect("must parse"); + .expect("must parse") + .to_owned(); assert_eq!(nixpath_expected, nixpath_actual); @@ -377,7 +378,7 @@ mod tests { fn absolute_path_missing_prefix() { assert_eq!( Error::MissingStoreDir, - StorePath::from_absolute_path(b"foobar-123").expect_err("must fail") + StorePathRef::from_absolute_path(b"foobar-123").expect_err("must fail") ); } |