option_parser: Reject empty keys in Tuple

We reject empty unquoted `Tuple` keys in inputs such as "@42", as this
is malformed input.

Signed-off-by: Pascal Scholz <pascal.scholz@cyberus-technology.de>
On-behalf-of: SAP pascal.scholz@sap.com
This commit is contained in:
Pascal Scholz
2026-07-10 17:08:10 +02:00
committed by Rob Bradford
parent 4c2328b723
commit d054a38600

View File

@@ -476,6 +476,8 @@ pub enum TupleError {
InvalidIntegerList(#[source] IntegerListParseError),
#[error("Invalid integer")]
InvalidInteger(#[source] ParseIntError),
#[error("Empty key in tuple: {0}")]
EmptyKey(String),
}
/// A tuple consisting of a `key@value` pair parsed from a string.
@@ -503,7 +505,11 @@ impl<S: Parseable, T: TupleValue> Parseable for Tuple<S, T> {
if last_idx != 0 {
return Err(TupleError::InvalidValue((*tuple).to_string()));
}
first_val = Some(&tuple[last_idx..idx]);
first_val = if tuple[last_idx..idx].is_empty() {
return Err(TupleError::EmptyKey((*tuple).to_string()));
} else {
Some(&tuple[last_idx..idx])
};
last_idx = idx + 1;
}
_ => {}
@@ -888,6 +894,17 @@ mod unit_tests {
Tuple::<String, u64>::from_str("foo42").unwrap_err();
}
#[test]
fn test_tuple_missing_key() {
let expected_value = "@42";
let e = Tuple::<String, u64>::from_str("@42").unwrap_err();
assert!(
matches!(e, TupleError::EmptyKey(ref s) if s == expected_value),
"Expected \"{:?}\"; got \"{e:?}\"",
TupleError::EmptyKey(expected_value.to_string()),
);
}
#[test]
fn test_split_commas_unbalanced_bracket() {
split_commas("[a,b").unwrap_err();