mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
option_parser: Trim whitespace from TupleList elements
Whitespace around `TupleList` elements can lead to errors while parsing the contained `Tuple` keys and values. In some cases, whitespace in input can lead to different parsing results for semantically identical keys or values, e.g. "id1" and "id1 ". We therefore trim whitespace from `TupleList` elements. Signed-off-by: Pascal Scholz <pascal.scholz@cyberus-technology.de> On-behalf-of: SAP pascal.scholz@sap.com
This commit is contained in:
committed by
Rob Bradford
parent
0a98a06ef7
commit
4c2328b723
@@ -530,7 +530,7 @@ impl<S: Parseable, T: TupleValue> Parseable for TupleList<S, T> {
|
||||
.ok_or_else(|| TupleError::UnbalancedOutsideBrackets(s.to_string()))?;
|
||||
let tuples_raw = split_commas(body).map_err(TupleError::SplitInsideBrackets)?;
|
||||
for tuple_raw in tuples_raw.iter() {
|
||||
list.push(Tuple::from_str(tuple_raw)?);
|
||||
list.push(Tuple::from_str(tuple_raw.trim())?);
|
||||
}
|
||||
|
||||
Ok(TupleList(list))
|
||||
@@ -868,6 +868,21 @@ mod unit_tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tuple_list_trim_whitespace() {
|
||||
let t = TupleList::<String, Vec<u64>>::from_str("[a@[1,2], b@[3,4] ,\tc@[5,6],\nd@[7,8]]")
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
t,
|
||||
TupleList(vec![
|
||||
Tuple("a".to_owned(), vec![1, 2]),
|
||||
Tuple("b".to_owned(), vec![3, 4]),
|
||||
Tuple("c".to_owned(), vec![5, 6]),
|
||||
Tuple("d".to_owned(), vec![7, 8]),
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tuple_missing_at_separator() {
|
||||
Tuple::<String, u64>::from_str("foo42").unwrap_err();
|
||||
|
||||
Reference in New Issue
Block a user