refactor(tvix): turn nullary enum variants into unit variants

Change-Id: Iad4f2cb4aa92b5bb29ead6050348a8cd3e7b8632
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9860
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
This commit is contained in:
edef 2023-10-27 12:31:12 +00:00
parent 520c5a191e
commit e525272019
6 changed files with 30 additions and 30 deletions

View file

@ -48,7 +48,7 @@ pub enum ValidateNodeError {
InvalidDigestLen(usize), InvalidDigestLen(usize),
/// Invalid name encountered /// Invalid name encountered
#[error("Invalid name")] #[error("Invalid name")]
InvalidName(), InvalidName,
/// Invalid symlink target /// Invalid symlink target
#[error("Invalid symlink target: {}", .0.as_bstr())] #[error("Invalid symlink target: {}", .0.as_bstr())]
InvalidSymlinkTarget(Vec<u8>), InvalidSymlinkTarget(Vec<u8>),
@ -63,7 +63,7 @@ fn validate_node_name(name: &[u8]) -> Result<(), ValidateNodeError> {
|| name.contains(&0x00) || name.contains(&0x00)
|| name.contains(&b'/') || name.contains(&b'/')
{ {
Err(ValidateNodeError::InvalidName())?; Err(ValidateNodeError::InvalidName)?;
} }
Ok(()) Ok(())
} }

View file

@ -165,7 +165,7 @@ fn validate_invalid_names() {
..Default::default() ..Default::default()
}; };
match d.validate().expect_err("must fail") { match d.validate().expect_err("must fail") {
ValidateDirectoryError::InvalidNode(n, ValidateNodeError::InvalidName()) => { ValidateDirectoryError::InvalidNode(n, ValidateNodeError::InvalidName) => {
assert_eq!(n, b"") assert_eq!(n, b"")
} }
_ => panic!("unexpected error"), _ => panic!("unexpected error"),
@ -182,7 +182,7 @@ fn validate_invalid_names() {
..Default::default() ..Default::default()
}; };
match d.validate().expect_err("must fail") { match d.validate().expect_err("must fail") {
ValidateDirectoryError::InvalidNode(n, ValidateNodeError::InvalidName()) => { ValidateDirectoryError::InvalidNode(n, ValidateNodeError::InvalidName) => {
assert_eq!(n, b".") assert_eq!(n, b".")
} }
_ => panic!("unexpected error"), _ => panic!("unexpected error"),
@ -200,7 +200,7 @@ fn validate_invalid_names() {
..Default::default() ..Default::default()
}; };
match d.validate().expect_err("must fail") { match d.validate().expect_err("must fail") {
ValidateDirectoryError::InvalidNode(n, ValidateNodeError::InvalidName()) => { ValidateDirectoryError::InvalidNode(n, ValidateNodeError::InvalidName) => {
assert_eq!(n, b"..") assert_eq!(n, b"..")
} }
_ => panic!("unexpected error"), _ => panic!("unexpected error"),
@ -216,7 +216,7 @@ fn validate_invalid_names() {
..Default::default() ..Default::default()
}; };
match d.validate().expect_err("must fail") { match d.validate().expect_err("must fail") {
ValidateDirectoryError::InvalidNode(n, ValidateNodeError::InvalidName()) => { ValidateDirectoryError::InvalidNode(n, ValidateNodeError::InvalidName) => {
assert_eq!(n, b"\x00") assert_eq!(n, b"\x00")
} }
_ => panic!("unexpected error"), _ => panic!("unexpected error"),
@ -232,7 +232,7 @@ fn validate_invalid_names() {
..Default::default() ..Default::default()
}; };
match d.validate().expect_err("must fail") { match d.validate().expect_err("must fail") {
ValidateDirectoryError::InvalidNode(n, ValidateNodeError::InvalidName()) => { ValidateDirectoryError::InvalidNode(n, ValidateNodeError::InvalidName) => {
assert_eq!(n, b"foo/bar") assert_eq!(n, b"foo/bar")
} }
_ => panic!("unexpected error"), _ => panic!("unexpected error"),

View file

@ -19,7 +19,7 @@ pub enum Nixbase32DecodeError {
#[error("character {0:x} not in alphabet")] #[error("character {0:x} not in alphabet")]
CharacterNotInAlphabet(u8), CharacterNotInAlphabet(u8),
#[error("nonzero carry")] #[error("nonzero carry")]
NonzeroCarry(), NonzeroCarry,
#[error("invalid length")] #[error("invalid length")]
InvalidLength, InvalidLength,
} }
@ -118,7 +118,7 @@ fn decode_inner(input: &[u8], output: &mut [u8]) -> Result<(), Nixbase32DecodeEr
// if we're at the end, but have a nonzero carry, the encoding is invalid. // if we're at the end, but have a nonzero carry, the encoding is invalid.
if carry != 0 { if carry != 0 {
return Err(Nixbase32DecodeError::NonzeroCarry()); return Err(Nixbase32DecodeError::NonzeroCarry);
} }
Ok(()) Ok(())

View file

@ -28,11 +28,11 @@ pub const STORE_DIR_WITH_SLASH: &str = "/nix/store/";
#[derive(Debug, PartialEq, Eq, thiserror::Error)] #[derive(Debug, PartialEq, Eq, thiserror::Error)]
pub enum Error { pub enum Error {
#[error("Dash is missing between hash and name")] #[error("Dash is missing between hash and name")]
MissingDash(), MissingDash,
#[error("Hash encoding is invalid: {0}")] #[error("Hash encoding is invalid: {0}")]
InvalidHashEncoding(Nixbase32DecodeError), InvalidHashEncoding(Nixbase32DecodeError),
#[error("Invalid length")] #[error("Invalid length")]
InvalidLength(), InvalidLength,
#[error( #[error(
"Invalid name: \"{}\", character at position {} is invalid", "Invalid name: \"{}\", character at position {} is invalid",
std::str::from_utf8(.0).unwrap_or(&BASE64.encode(.0)), std::str::from_utf8(.0).unwrap_or(&BASE64.encode(.0)),
@ -40,7 +40,7 @@ pub enum Error {
)] )]
InvalidName(Vec<u8>, u8), InvalidName(Vec<u8>, u8),
#[error("Tried to parse an absolute path which was missing the store dir prefix.")] #[error("Tried to parse an absolute path which was missing the store dir prefix.")]
MissingStoreDir(), MissingStoreDir,
} }
/// 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]).
@ -102,7 +102,7 @@ impl StorePath {
// - 1 dash // - 1 dash
// - 1 character for the name // - 1 character for the name
if s.len() < ENCODED_DIGEST_SIZE + 2 { if s.len() < ENCODED_DIGEST_SIZE + 2 {
Err(Error::InvalidLength())? Err(Error::InvalidLength)?
} }
let digest = match nixbase32::decode(&s[..ENCODED_DIGEST_SIZE]) { let digest = match nixbase32::decode(&s[..ENCODED_DIGEST_SIZE]) {
@ -111,7 +111,7 @@ impl StorePath {
}; };
if s[ENCODED_DIGEST_SIZE] != b'-' { if s[ENCODED_DIGEST_SIZE] != b'-' {
return Err(Error::MissingDash()); return Err(Error::MissingDash);
} }
Ok(StorePath { Ok(StorePath {
@ -126,7 +126,7 @@ impl StorePath {
pub fn from_absolute_path(s: &[u8]) -> Result<StorePath, Error> { pub fn from_absolute_path(s: &[u8]) -> Result<StorePath, Error> {
match s.strip_prefix(STORE_DIR_WITH_SLASH.as_bytes()) { match s.strip_prefix(STORE_DIR_WITH_SLASH.as_bytes()) {
Some(s_stripped) => Self::from_bytes(s_stripped), Some(s_stripped) => Self::from_bytes(s_stripped),
None => Err(Error::MissingStoreDir()), None => Err(Error::MissingStoreDir),
} }
} }
@ -134,7 +134,7 @@ impl StorePath {
pub fn from_name_and_digest(name: String, digest: &[u8]) -> Result<StorePath, Error> { pub fn from_name_and_digest(name: String, digest: &[u8]) -> Result<StorePath, Error> {
Ok(Self { Ok(Self {
name: validate_name(name.as_bytes())?.to_owned(), name: validate_name(name.as_bytes())?.to_owned(),
digest: digest.try_into().map_err(|_| Error::InvalidLength())?, digest: digest.try_into().map_err(|_| Error::InvalidLength)?,
}) })
} }
@ -144,7 +144,7 @@ impl StorePath {
pub fn from_absolute_path_full(s: &str) -> Result<(StorePath, PathBuf), Error> { pub fn from_absolute_path_full(s: &str) -> Result<(StorePath, PathBuf), Error> {
// strip [STORE_DIR_WITH_SLASH] from s // strip [STORE_DIR_WITH_SLASH] from s
match s.strip_prefix(STORE_DIR_WITH_SLASH) { match s.strip_prefix(STORE_DIR_WITH_SLASH) {
None => Err(Error::MissingStoreDir()), None => Err(Error::MissingStoreDir),
Some(rest) => { Some(rest) => {
// put rest in a PathBuf // put rest in a PathBuf
let mut p = PathBuf::new(); let mut p = PathBuf::new();
@ -161,7 +161,7 @@ impl StorePath {
let rest_buf: PathBuf = it.collect(); let rest_buf: PathBuf = it.collect();
Ok((store_path, rest_buf)) Ok((store_path, rest_buf))
} else { } else {
Err(Error::InvalidLength()) // Well, or missing "/"? Err(Error::InvalidLength) // Well, or missing "/"?
} }
} }
} }
@ -205,7 +205,7 @@ pub(crate) fn validate_name(s: &(impl AsRef<[u8]> + ?Sized)) -> Result<&str, Err
// Empty or excessively long names are not allowed. // Empty or excessively long names are not allowed.
if s.is_empty() || s.len() > 211 { if s.is_empty() || s.len() > 211 {
return Err(Error::InvalidLength()); return Err(Error::InvalidLength);
} }
if s[0] == b'.' { if s[0] == b'.' {
@ -341,7 +341,7 @@ mod tests {
#[test] #[test]
fn absolute_path_missing_prefix() { fn absolute_path_missing_prefix() {
assert_eq!( assert_eq!(
Error::MissingStoreDir(), Error::MissingStoreDir,
StorePath::from_absolute_path(b"foobar-123").expect_err("must fail") StorePath::from_absolute_path(b"foobar-123").expect_err("must fail")
); );
} }
@ -370,15 +370,15 @@ mod tests {
#[test] #[test]
fn from_absolute_path_errors() { fn from_absolute_path_errors() {
assert_eq!( assert_eq!(
Error::InvalidLength(), Error::InvalidLength,
StorePath::from_absolute_path_full("/nix/store/").expect_err("must fail") StorePath::from_absolute_path_full("/nix/store/").expect_err("must fail")
); );
assert_eq!( assert_eq!(
Error::InvalidLength(), Error::InvalidLength,
StorePath::from_absolute_path_full("/nix/store/foo").expect_err("must fail") StorePath::from_absolute_path_full("/nix/store/foo").expect_err("must fail")
); );
assert_eq!( assert_eq!(
Error::MissingStoreDir(), Error::MissingStoreDir,
StorePath::from_absolute_path_full( StorePath::from_absolute_path_full(
"00bgd045z0d4icpbc2yyz4gx48ak44la-net-tools-1.60_p20170221182432" "00bgd045z0d4icpbc2yyz4gx48ak44la-net-tools-1.60_p20170221182432"
) )

View file

@ -29,7 +29,7 @@ pub enum ValidatePathInfoError {
/// No node present /// No node present
#[error("No node present")] #[error("No node present")]
NoNodePresent(), NoNodePresent,
/// Node fails validation /// Node fails validation
#[error("Invalid root node: {:?}", .0.to_string())] #[error("Invalid root node: {:?}", .0.to_string())]
@ -159,7 +159,7 @@ impl PathInfo {
// Ensure there is a (root) node present, and it properly parses to a [store_path::StorePath]. // Ensure there is a (root) node present, and it properly parses to a [store_path::StorePath].
let root_nix_path = match &self.node { let root_nix_path = match &self.node {
None | Some(castorepb::Node { node: None }) => { None | Some(castorepb::Node { node: None }) => {
Err(ValidatePathInfoError::NoNodePresent())? Err(ValidatePathInfoError::NoNodePresent)?
} }
Some(castorepb::Node { node: Some(node) }) => { Some(castorepb::Node { node: Some(node) }) => {
node.validate() node.validate()

View file

@ -8,12 +8,12 @@ use tvix_castore::proto as castorepb;
#[test_case( #[test_case(
None, None,
Err(ValidatePathInfoError::NoNodePresent()) ; Err(ValidatePathInfoError::NoNodePresent) ;
"No node" "No node"
)] )]
#[test_case( #[test_case(
Some(castorepb::Node { node: None }), Some(castorepb::Node { node: None }),
Err(ValidatePathInfoError::NoNodePresent()); Err(ValidatePathInfoError::NoNodePresent);
"No node 2" "No node 2"
)] )]
fn validate_no_node( fn validate_no_node(
@ -54,7 +54,7 @@ fn validate_no_node(
}, },
Err(ValidatePathInfoError::InvalidNodeName( Err(ValidatePathInfoError::InvalidNodeName(
"invalid".into(), "invalid".into(),
store_path::Error::InvalidLength() store_path::Error::InvalidLength
)); ));
"invalid node name" "invalid node name"
)] )]
@ -99,7 +99,7 @@ fn validate_directory(
}, },
Err(ValidatePathInfoError::InvalidNodeName( Err(ValidatePathInfoError::InvalidNodeName(
"invalid".into(), "invalid".into(),
store_path::Error::InvalidLength() store_path::Error::InvalidLength
)); ));
"invalid node name" "invalid node name"
)] )]
@ -132,7 +132,7 @@ fn validate_file(
}, },
Err(ValidatePathInfoError::InvalidNodeName( Err(ValidatePathInfoError::InvalidNodeName(
"invalid".into(), "invalid".into(),
store_path::Error::InvalidLength() store_path::Error::InvalidLength
)); ));
"invalid node name" "invalid node name"
)] )]