Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
cb7382a941
commit
4c326b4ca5
1 changed files with 40 additions and 18 deletions
58
src/lib.rs
58
src/lib.rs
|
@ -116,28 +116,40 @@ fn parse_action_line(i: &str) -> IResult<&str, &str> {
|
|||
Ok((i, line))
|
||||
}
|
||||
|
||||
fn parse_send(i: &str) -> IResult<&str, Action> {
|
||||
let (i , (name, _, _, _, lines)) = tuple((
|
||||
take_while1(is_not_space),
|
||||
space1,
|
||||
tag("sends:"),
|
||||
take_while1(|c| c == ' ' || c == '\r' || c == '\n'), // Spaces but \t
|
||||
recognize(many1(parse_action_line)),
|
||||
))(i)?;
|
||||
let lines = lines.trim();
|
||||
fn parse_send_receive(tagname: &str) -> impl Fn(&str) -> IResult<&str, Action> + '_ {
|
||||
move |i: &str| {
|
||||
let (i , (name, _, _, _, lines)) = tuple((
|
||||
take_while1(is_not_space),
|
||||
space1,
|
||||
tag(tagname),
|
||||
take_while1(|c| c == ' ' || c == '\r' || c == '\n'), // Spaces but \t
|
||||
recognize(many1(parse_action_line)),
|
||||
))(i)?;
|
||||
let lines = lines.trim();
|
||||
|
||||
let elem: Element =
|
||||
Element::from_reader_with_prefixes(
|
||||
&lines.as_bytes()[..],
|
||||
String::from(DEFAULT_NS),
|
||||
).unwrap();
|
||||
Ok((i, Action::Send(String::from(name), elem)))
|
||||
let elem: Element =
|
||||
Element::from_reader_with_prefixes(
|
||||
&lines.as_bytes()[..],
|
||||
String::from(DEFAULT_NS),
|
||||
).unwrap();
|
||||
Ok((i, Action::Send(String::from(name), elem)))
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_send(i: &str) -> IResult<&str, Action> {
|
||||
let (i, action) = parse_send_receive("sends:")(i)?;
|
||||
Ok((i, action))
|
||||
}
|
||||
|
||||
fn parse_receive(i: &str) -> IResult<&str, Action> {
|
||||
let (i, action) = parse_send_receive("receives::")(i)?;
|
||||
Ok((i, action))
|
||||
}
|
||||
|
||||
fn parse_action(i: &str) -> IResult<&str, Action> {
|
||||
delimited(
|
||||
allspaces,
|
||||
parse_connect,
|
||||
alt((parse_connect, parse_send, parse_receive)),
|
||||
allspaces,
|
||||
)(i)
|
||||
}
|
||||
|
@ -291,7 +303,7 @@ rosa connects
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_action_send() {
|
||||
fn test_action_send_receive() {
|
||||
let buf = r#"rosa sends:
|
||||
|
||||
<presence
|
||||
|
@ -308,6 +320,16 @@ rosa connects
|
|||
String::from(DEFAULT_NS),
|
||||
).unwrap(),
|
||||
);
|
||||
assert_eq!(parse_send(buf).unwrap().1, action);
|
||||
|
||||
assert_eq!(parse_send_receive("sends:")(buf).unwrap().1, action);
|
||||
|
||||
let buf2 = r#"rosa receives:
|
||||
|
||||
<presence
|
||||
type="unavailable"
|
||||
/>
|
||||
|
||||
"#;
|
||||
assert_eq!(parse_send_receive("receives:")(buf2).unwrap().1, action);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue