-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: support for multi-properties #82
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,15 +131,26 @@ impl From<Component<'_>> for Other { | |
|
||
impl From<Component<'_>> for InnerComponent { | ||
fn from(component: Component) -> Self { | ||
Self { | ||
let mut from_component = Self { | ||
properties: component | ||
.properties | ||
.into_iter() | ||
.map(|p| (p.name.clone().into_owned().into(), p.into())) | ||
.iter() | ||
.filter(|p| !p.is_multi_property()) | ||
.map(|p| (p.name.clone().into_owned().into(), p.to_owned().into())) | ||
.collect(), | ||
components: component.components.into_iter().map(Other::from).collect(), | ||
multi_properties: Default::default(), | ||
}; | ||
|
||
for p in component | ||
.properties | ||
.into_iter() | ||
.filter(Property::is_multi_property) | ||
{ | ||
from_component.insert_multi(p); | ||
} | ||
|
||
from_component | ||
} | ||
} | ||
|
||
|
@@ -271,6 +282,18 @@ pub fn component<'a, E: ParseError<&'a str> + ContextError<&'a str>>( | |
)) | ||
} | ||
|
||
#[cfg(test)] | ||
pub fn inner_component<'a, E: ParseError<&'a str> + ContextError<&'a str>>( | ||
input: &'a str, | ||
) -> IResult<&'a str, InnerComponent, E> { | ||
match component::<(_, _)>(input) { | ||
Ok(result) => { | ||
return Ok((result.0, InnerComponent::from(result.1))); | ||
} | ||
Err(_e) => todo!(), | ||
}; | ||
} | ||
|
||
#[test] | ||
fn test_components() { | ||
assert_parser!(component, "BEGIN:FOO\nEND:FOO", Component::new_empty("FOO")); | ||
|
@@ -424,6 +447,60 @@ END:VEVENT | |
); | ||
} | ||
|
||
#[test] | ||
fn test_multi_properties() { | ||
let multi_properties = From::from([( | ||
"ATTENDEE".into(), | ||
vec![ | ||
Property { | ||
name: "ATTENDEE".into(), | ||
val: "mailto:[email protected]".into(), | ||
params: vec![ | ||
Parameter { | ||
key: "EMAIL".into(), | ||
val: Some("\"[email protected]\"".into()), | ||
}, | ||
Parameter { | ||
key: "CUTYPE".into(), | ||
val: Some("INDIVIDUAL".into()), | ||
}, | ||
], | ||
} | ||
.into(), | ||
Property { | ||
name: "ATTENDEE".into(), | ||
val: "mailto:[email protected]".into(), | ||
params: vec![ | ||
Parameter { | ||
key: "EMAIL".into(), | ||
val: Some("\"[email protected]\"".into()), | ||
}, | ||
Parameter { | ||
key: "CUTYPE".into(), | ||
val: Some("INDIVIDUAL".into()), | ||
}, | ||
], | ||
} | ||
.into(), | ||
], | ||
)]); | ||
|
||
assert_parser!( | ||
inner_component, | ||
r#" | ||
BEGIN:VEVENT | ||
ATTENDEE;CUTYPE=INDIVIDUAL;EMAIL="[email protected]":mailto:[email protected] | ||
ATTENDEE;CUTYPE=INDIVIDUAL;EMAIL="[email protected]":mailto:[email protected] | ||
END:VEVENT | ||
"#, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for simplicity you could also just test parsing and reserializing this same event There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, interesting Idea. I will try this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nvm, don't have to, I tried, it's a bit of a waste of time ;) |
||
InnerComponent { | ||
properties: Default::default(), | ||
multi_properties, | ||
components: vec![] | ||
} | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_faulty_component() { | ||
use nom::error::{ErrorKind::*, VerboseErrorKind::*}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't you want to go with a multimap here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still can, it does add another dependencies and I thought this approach might be more simple. It also keeps
multi_properties
more similar toproperties
in type. Happy to change it to multimap if you prefer.