use std::{
fmt::{self, Display, Formatter},
path::{Path, PathBuf},
str::FromStr,
};
use thiserror::Error;
const NIX_STORE: &str = "/nix/store";
const NIX_BASE32: &str = "0123456789abcdfghijklmnpqrsvwxyz";
const SHORT_HASH_LENGTH: usize = 8;
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct StorePath {
path: PathBuf,
hash: String,
}
impl StorePath {
pub(crate) fn short_hash(&self) -> &str {
&self.hash[..SHORT_HASH_LENGTH]
}
}
impl AsRef<Path> for StorePath {
fn as_ref(&self) -> &Path {
&self.path
}
}
impl FromStr for StorePath {
type Err = StorePathError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
let path = PathBuf::from(value);
let name = path
.file_name()
.and_then(|name| name.to_str())
.ok_or_else(|| StorePathError(value.to_owned()))?;
let (hash, store_name) = name
.split_once('-')
.ok_or_else(|| StorePathError(value.to_owned()))?;
if path.parent() != Some(Path::new(NIX_STORE))
|| store_name.is_empty()
|| hash.len() != 32
|| !hash.chars().all(|character| NIX_BASE32.contains(character))
{
return Err(StorePathError(value.to_owned()));
}
let hash = hash.to_owned();
Ok(Self { path, hash })
}
}
impl Display for StorePath {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
self.path.display().fmt(formatter)
}
}
#[derive(Debug, Error)]
#[error("invalid Nix store path `{0}`")]
pub(crate) struct StorePathError(
String,
);
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::StorePath;
#[test]
fn store_paths_validate_identity_and_have_distinct_presentations() {
let path = "/nix/store/00000000000000000000000000000000-nixos-system-www";
let store_path = path.parse::<StorePath>().expect("store path should parse");
let same = path.parse::<StorePath>().expect("store path should parse");
let different_name = "/nix/store/00000000000000000000000000000000-different"
.parse::<StorePath>()
.expect("store path should parse");
assert_eq!(store_path, same);
assert_ne!(store_path, different_name);
assert_eq!(store_path.to_string(), path);
assert_eq!(store_path.short_hash(), "00000000");
assert_eq!(store_path.as_ref(), PathBuf::from(path));
assert!("relative-store-path".parse::<StorePath>().is_err());
assert!(
"/tmp/00000000000000000000000000000000-system"
.parse::<StorePath>()
.is_err()
);
assert!(
"/nix/store/not-a-nix-hash-name"
.parse::<StorePath>()
.is_err()
);
}
}