rustfmt pass
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
bb08011d6f
commit
b57b36f81a
1 changed files with 34 additions and 48 deletions
82
src/lib.rs
82
src/lib.rs
|
@ -7,18 +7,18 @@
|
|||
use std::collections::HashMap;
|
||||
use std::str::FromStr;
|
||||
|
||||
use jid::BareJid;
|
||||
use minidom::Element;
|
||||
use nom::{
|
||||
self,
|
||||
branch::alt,
|
||||
bytes::complete::{tag, take_until, take_until1, take_while1},
|
||||
character::complete::{multispace0, space0, space1},
|
||||
combinator::recognize,
|
||||
combinator::recognize,
|
||||
multi::{many0, many1},
|
||||
sequence::{delimited, tuple},
|
||||
IResult,
|
||||
};
|
||||
use jid::BareJid;
|
||||
use minidom::Element;
|
||||
|
||||
pub static DEFAULT_NS: &'static str = "jabber:client";
|
||||
|
||||
|
@ -108,52 +108,44 @@ fn parse_connect(i: &str) -> IResult<&str, Action> {
|
|||
}
|
||||
|
||||
fn parse_action_line(i: &str) -> IResult<&str, &str> {
|
||||
let (i, (_, line, _)) = tuple((
|
||||
many1(tag("\t")),
|
||||
take_until1("\n"),
|
||||
tag("\n"),
|
||||
))(i)?;
|
||||
Ok((i, line))
|
||||
let (i, (_, line, _)) = tuple((many1(tag("\t")), take_until1("\n"), tag("\n")))(i)?;
|
||||
Ok((i, line))
|
||||
}
|
||||
|
||||
fn parse_send_receive(tagname: &str) -> impl Fn(&str) -> IResult<&str, (String, Element)> + '_ {
|
||||
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();
|
||||
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, (String::from(name), elem)))
|
||||
}
|
||||
let elem: Element =
|
||||
Element::from_reader_with_prefixes(&lines.as_bytes()[..], String::from(DEFAULT_NS))
|
||||
.unwrap();
|
||||
Ok((i, (String::from(name), elem)))
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_send(i: &str) -> IResult<&str, Action> {
|
||||
let (i, (name, elem)) = parse_send_receive("sends:")(i)?;
|
||||
Ok((i, Action::Send(name, elem)))
|
||||
let (i, (name, elem)) = parse_send_receive("sends:")(i)?;
|
||||
Ok((i, Action::Send(name, elem)))
|
||||
}
|
||||
|
||||
fn parse_receive(i: &str) -> IResult<&str, Action> {
|
||||
let (i, (name, elem)) = parse_send_receive("receives:")(i)?;
|
||||
Ok((i, Action::Receive(name, elem)))
|
||||
let (i, (name, elem)) = parse_send_receive("receives:")(i)?;
|
||||
Ok((i, Action::Receive(name, elem)))
|
||||
}
|
||||
|
||||
fn parse_actions(i: &str) -> IResult<&str, Vec<Action>> {
|
||||
let (i, actions) = many1(
|
||||
delimited(
|
||||
allspaces,
|
||||
alt((parse_connect, parse_send, parse_receive)),
|
||||
allspaces,
|
||||
)
|
||||
)(i)?;
|
||||
let (i, actions) = many1(delimited(
|
||||
allspaces,
|
||||
alt((parse_connect, parse_send, parse_receive)),
|
||||
allspaces,
|
||||
))(i)?;
|
||||
Ok((i, actions))
|
||||
}
|
||||
|
||||
|
@ -244,7 +236,7 @@ mod tests {
|
|||
fn test_action_connect() {
|
||||
let buf1 = "louise connects\n";
|
||||
|
||||
let action = Action::Connect(String::from("louise"));
|
||||
let action = Action::Connect(String::from("louise"));
|
||||
assert_eq!(parse_connect(buf1).unwrap().1, action);
|
||||
}
|
||||
|
||||
|
@ -257,16 +249,13 @@ mod tests {
|
|||
/>
|
||||
|
||||
"#;
|
||||
let xml = b"<presence\n\t\ttype=\"unavailable\"\t\n/>";
|
||||
let xml = b"<presence\n\t\ttype=\"unavailable\"\t\n/>";
|
||||
|
||||
let send = Action::Send(
|
||||
String::from("rosa"),
|
||||
Element::from_reader_with_prefixes(
|
||||
&xml[..],
|
||||
String::from(DEFAULT_NS),
|
||||
).unwrap(),
|
||||
Element::from_reader_with_prefixes(&xml[..], String::from(DEFAULT_NS)).unwrap(),
|
||||
);
|
||||
assert_eq!(parse_send(buf).unwrap().1, send);
|
||||
assert_eq!(parse_send(buf).unwrap().1, send);
|
||||
|
||||
let buf2 = r#"rosa receives:
|
||||
|
||||
|
@ -277,11 +266,8 @@ mod tests {
|
|||
"#;
|
||||
let receive = Action::Receive(
|
||||
String::from("rosa"),
|
||||
Element::from_reader_with_prefixes(
|
||||
&xml[..],
|
||||
String::from(DEFAULT_NS),
|
||||
).unwrap(),
|
||||
Element::from_reader_with_prefixes(&xml[..], String::from(DEFAULT_NS)).unwrap(),
|
||||
);
|
||||
assert_eq!(parse_receive(buf2).unwrap().1, receive);
|
||||
assert_eq!(parse_receive(buf2).unwrap().1, receive);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue