docs(tvix/nix-compat): update docstrings

Make it cleaner that StorePath only does encode the first path component
after the STORE_DIR prefix. Also, move some of the comments around a
bit, so it makes more sense what's using what.

Change-Id: Ibb57373a13526e30c58ad561ca50e1336b091d94
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8566
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
This commit is contained in:
Florian Klink 2023-05-14 13:29:01 +03:00 committed by clbot
parent 14a8ea9eab
commit 1a2190cd3b
2 changed files with 18 additions and 11 deletions

1
tvix/Cargo.lock generated
View file

@ -2708,7 +2708,6 @@ dependencies = [
"prost", "prost",
"prost-build", "prost-build",
"rayon", "rayon",
"serde_json",
"sha2 0.10.6", "sha2 0.10.6",
"sled", "sled",
"tempfile", "tempfile",

View file

@ -44,14 +44,15 @@ impl From<NameError> for Error {
/// Represents a path in the Nix store (a direct child of [STORE_DIR]). /// Represents a path in the Nix store (a direct child of [STORE_DIR]).
/// ///
/// It starts with a digest (20 bytes), [crate::nixbase32]-encoded, /// It consists of a digest (20 bytes), and a name, which is a string.
/// followed by a `-`, and ends with a `name`, which is a string, consisting only of ASCCI /// The name may only contain ASCII alphanumeric, or one of the following
/// alphanumeric characters, or one of the following characters: `-`, `_`, `.`, /// characters: `-`, `_`, `.`, `+`, `?`, `=`.
/// `+`, `?`, `=`.
///
/// The name is usually used to describe the pname and version of a package. /// The name is usually used to describe the pname and version of a package.
/// Derivations paths can also be represented as store paths, they end /// Derivation paths can also be represented as store paths, their names just
/// with .drv. /// end with the `.drv` prefix.
///
/// A [StorePath] does not encode any additional subpath "inside" the store
/// path.
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct StorePath { pub struct StorePath {
pub digest: [u8; DIGEST_SIZE], pub digest: [u8; DIGEST_SIZE],
@ -59,6 +60,8 @@ pub struct StorePath {
} }
impl StorePath { impl StorePath {
/// Construct a [StorePath] by passing the `$digest-$name` string
/// that comes after [STORE_DIR_WITH_SLASH].
pub fn from_string(s: &str) -> Result<StorePath, Error> { pub fn from_string(s: &str) -> Result<StorePath, Error> {
// the whole string needs to be at least: // the whole string needs to be at least:
// //
@ -87,7 +90,8 @@ impl StorePath {
} }
/// Construct a [StorePath] from an absolute store path string. /// Construct a [StorePath] from an absolute store path string.
/// That is a string starting with the store prefix (/nix/store) /// This is equivalent to calling [StorePath::from_string], but stripping
/// the [STORE_DIR_WITH_SLASH] prefix before.
pub fn from_absolute_path(s: &str) -> Result<StorePath, Error> { pub fn from_absolute_path(s: &str) -> Result<StorePath, Error> {
match s.strip_prefix(STORE_DIR_WITH_SLASH) { match s.strip_prefix(STORE_DIR_WITH_SLASH) {
Some(s_stripped) => Self::from_string(s_stripped), Some(s_stripped) => Self::from_string(s_stripped),
@ -96,9 +100,10 @@ impl StorePath {
} }
/// Converts the [StorePath] to an absolute store path string. /// Converts the [StorePath] to an absolute store path string.
/// That is a string starting with the store prefix (/nix/store) /// That is just the string representation, prefixed with the store prefix
/// ([STORE_DIR_WITH_SLASH]),
pub fn to_absolute_path(&self) -> String { pub fn to_absolute_path(&self) -> String {
format!("{}/{}", STORE_DIR, self) format!("{}{}", STORE_DIR_WITH_SLASH, self)
} }
/// Checks a given &str to match the restrictions for store path names. /// Checks a given &str to match the restrictions for store path names.
@ -123,6 +128,9 @@ impl StorePath {
} }
impl fmt::Display for StorePath { impl fmt::Display for StorePath {
/// The string representation of a store path starts with a digest (20
/// bytes), [crate::nixbase32]-encoded, followed by a `-`,
/// and ends with the name.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}-{}", nixbase32::encode(&self.digest), self.name) write!(f, "{}-{}", nixbase32::encode(&self.digest), self.name)
} }