Skip to content

Commit

Permalink
Handle missing optional matches
Browse files Browse the repository at this point in the history
The Index trait for Captures panics if there is no match at a given
index.

Closes #193
  • Loading branch information
ehaas authored and rcoh committed May 26, 2024
1 parent fca19ae commit 165bb0e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/operator/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ impl Parse {
None => Ok(None),
Some(capture) => {
let mut values: Vec<data::Value> = 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))
}
Expand Down
7 changes: 7 additions & 0 deletions tests/structured_tests/missing_optional_match.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query = """* |parse regex "(?P<url>.+)(?P<query>\\?.+)?""""
input = """/track/?verbose=1&ip=1&_=1716389413340"""
output = """
[query=None] [url=/track/?verbose=1&ip=1&_=1716389413340]
"""
error = """
"""

0 comments on commit 165bb0e

Please sign in to comment.