From 165bb0e9b435b9fd31374f00a8c5f86807b22a89 Mon Sep 17 00:00:00 2001 From: Evan Haas Date: Fri, 24 May 2024 09:17:03 -0700 Subject: [PATCH] Handle missing optional matches The Index trait for Captures panics if there is no match at a given index. Closes #193 --- src/operator/parse.rs | 7 +++++-- tests/structured_tests/missing_optional_match.toml | 7 +++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 tests/structured_tests/missing_optional_match.toml diff --git a/src/operator/parse.rs b/src/operator/parse.rs index 9f06c4d..4051078 100644 --- a/src/operator/parse.rs +++ b/src/operator/parse.rs @@ -34,9 +34,12 @@ impl Parse { None => Ok(None), Some(capture) => { let mut values: Vec = Vec::with_capacity(self.fields.len()); - for i in 0..self.fields.len() { + for item in capture.iter().skip(1) { // the first capture is the entire string - values.push(data::Value::from_string(&capture[i + 1])); + match item { + None => values.push(data::Value::None), + Some(match_) => values.push(data::Value::from_string(match_.as_str())), + }; } Ok(Some(values)) } diff --git a/tests/structured_tests/missing_optional_match.toml b/tests/structured_tests/missing_optional_match.toml new file mode 100644 index 0000000..23d8418 --- /dev/null +++ b/tests/structured_tests/missing_optional_match.toml @@ -0,0 +1,7 @@ +query = """* |parse regex "(?P.+)(?P\\?.+)?"""" +input = """/track/?verbose=1&ip=1&_=1716389413340""" +output = """ +[query=None] [url=/track/?verbose=1&ip=1&_=1716389413340] +""" +error = """ +"""