feat(tvix/nix-compat/store_path): store position in InvalidName

Add the position in the string where the name is problematic.

Change-Id: If6fd8be6100b718f8d68568eafc77ebb3cfb82d0
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8979
Reviewed-by: raitobezarius <tvl@lahfa.xyz>
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
This commit is contained in:
Florian Klink 2023-07-20 14:37:04 +03:00 committed by flokli
parent a6580748aa
commit 7971d7d9ff

View file

@ -28,8 +28,8 @@ pub enum Error {
InvalidHashEncoding(Nixbase32DecodeError),
#[error("Invalid length")]
InvalidLength(),
#[error("Invalid name: {0:?}")]
InvalidName(Vec<u8>),
#[error("Invalid name: {:?}, character at position {} ('{}') is invalid", .0, .1, .0[1])]
InvalidName(Vec<u8>, usize),
#[error("Tried to parse an absolute path which was missing the store dir prefix.")]
MissingStoreDir(),
}
@ -139,7 +139,7 @@ impl StorePath {
/// Checks a given &[u8] to match the restrictions for store path names, and
/// returns the name as string if successful.
pub(crate) fn validate_name(s: &[u8]) -> Result<String, Error> {
for c in s {
for (i, c) in s.iter().enumerate() {
if c.is_ascii_alphanumeric()
|| *c == b'-'
|| *c == b'_'
@ -151,7 +151,7 @@ pub(crate) fn validate_name(s: &[u8]) -> Result<String, Error> {
continue;
}
return Err(Error::InvalidName(s.to_vec()));
return Err(Error::InvalidName(s.to_vec(), i));
}
Ok(String::from_utf8(s.to_vec()).unwrap())