Skip to content

Commit

Permalink
reject if enum argument options is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
shanretoo committed May 17, 2024
1 parent b9eef3d commit 1634a5b
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/parse/text/simple_extensions/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@ impl From<ArgumentsItem> for simple_extensions::ArgumentsItem {
/// Parse errors for [simple_extensions::ArgumentsItem].
#[derive(Debug, Error, PartialEq)]
pub enum ArgumentsItemError {
/// Empty enumeration option.
#[error("empty enumeration option")]
EmptyEnumOption,
/// Empty enumeration options.
#[error("empty enumeration options")]
EmptyEnumOptions,

/// Empty enumeration option value.
#[error("empty enumeration option value")]
EmptyEnumOptionValue,

/// Empty optional field.
#[error("the optional field `{0}` is empty and should be removed")]
Expand All @@ -141,8 +145,12 @@ impl EnumArgument {
description: Option<String>,
options: Vec<String>,
) -> Result<EnumArgument, ArgumentsItemError> {
if options.is_empty() {
return Err(ArgumentsItemError::EmptyEnumOptions);
}

if options.iter().any(String::is_empty) {
return Err(ArgumentsItemError::EmptyEnumOption);
return Err(ArgumentsItemError::EmptyEnumOptionValue);
}

Ok(EnumArgument {
Expand Down Expand Up @@ -270,17 +278,23 @@ mod tests {
}

#[test]
fn parse_enum_argument_with_empty_option() -> Result<(), ArgumentsItemError> {
fn parse_enum_argument_with_invalid_options() -> Result<(), ArgumentsItemError> {
let enum_argument = simple_extensions::ArgumentsItem::Variant0 {
name: None,
description: Some("desc".to_string()),
options: vec!["".to_string()],
};
let is_err = enum_argument
.parse(&mut Context::default())
.err()
.map(|err| matches!(err, ArgumentsItemError::EmptyEnumOption));
assert_eq!(is_err, Some(true));
let err = enum_argument.parse(&mut Context::default()).err();
assert_eq!(err, Some(ArgumentsItemError::EmptyEnumOptionValue));

let enum_argument = simple_extensions::ArgumentsItem::Variant0 {
name: None,
description: Some("desc".to_string()),
options: vec![],
};
let err = enum_argument.parse(&mut Context::default()).err();
assert_eq!(err, Some(ArgumentsItemError::EmptyEnumOptions));

Ok(())
}

Expand Down

0 comments on commit 1634a5b

Please sign in to comment.