Skip to content

Commit

Permalink
fix: accept quoted parameters
Browse files Browse the repository at this point in the history
This are necessay when escaping semicolons, colons or commas.
  • Loading branch information
luca-iachini authored and hoodie committed Jun 10, 2024
1 parent 7de9acb commit 38d992c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/parser/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ fn test_multi_properties() {
params: vec![
Parameter {
key: "EMAIL".into(),
val: Some("\"email@example.com\"".into()),
val: Some("email@example.com".into()),
},
Parameter {
key: "CUTYPE".into(),
Expand All @@ -471,7 +471,7 @@ fn test_multi_properties() {
params: vec![
Parameter {
key: "EMAIL".into(),
val: Some("\"dmail@example.com\"".into()),
val: Some("dmail@example.com".into()),
},
Parameter {
key: "CUTYPE".into(),
Expand Down
23 changes: 19 additions & 4 deletions src/parser/parameters.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use nom::{
branch::alt,
bytes::complete::{tag, take_till1},
bytes::complete::{is_not, tag, take_till1},
character::complete::space0,
combinator::{eof, opt},
error::{convert_error, ContextError, ParseError, VerboseError},
multi::many0,
sequence::{preceded, separated_pair, tuple},
sequence::{delimited, preceded, separated_pair, tuple},
Finish, IResult, Parser,
};

Expand Down Expand Up @@ -141,7 +141,12 @@ fn pair_parameter<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
separated_pair(
valid_key_sequence_cow, //key
tag("="),
opt(alt((eof, take_till1(|x| x == ';' || x == ':')))).map(remove_empty_string),
opt(alt((
eof,
delimited(tag("\""), is_not("\""), tag("\"")),
take_till1(|x| x == ';' || x == ':'),
)))
.map(remove_empty_string),
),
)
.map(|(key, val)| Parameter {
Expand All @@ -161,7 +166,12 @@ fn base_parameter<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
),
opt(preceded(
tag("="),
alt((eof, take_till1(|x| x == ';' || x == ':'))).map(ParseString::from),
alt((
eof,
delimited(tag("\""), is_not("\""), tag("\"")),
take_till1(|x| x == ';' || x == ':'),
))
.map(ParseString::from),
))
.map(remove_empty_string_parsed),
))
Expand Down Expand Up @@ -195,6 +205,11 @@ pub fn parse_parameter_list() {
Parameter::new_ref("DATE", Some("20170218")),
]
);
assert_parser!(
parameters,
";TEXT=\"quoted text with \\;\"",
vec![Parameter::new_ref("TEXT", Some("quoted text with \\;")),]
);
}

pub fn parameters<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
Expand Down

0 comments on commit 38d992c

Please sign in to comment.