New Action::Disconnect

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-01-10 14:25:06 +01:00
parent 41849edbb9
commit 0aec3cea6e
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -35,6 +35,7 @@ pub enum Action {
Connect(AccountName),
Send(AccountName, Element),
Receive(AccountName, Element),
Disconnect(AccountName),
}
#[derive(Debug, Clone, PartialEq)]
@ -107,6 +108,12 @@ fn parse_connect(i: &str) -> IResult<&str, Action> {
Ok((i, Action::Connect(String::from(name))))
}
fn parse_disconnect(i: &str) -> IResult<&str, Action> {
let (i, (name, _, _)) = tuple((take_while1(is_not_space), space1, tag("disconnects\n")))(i)?;
Ok((i, Action::Disconnect(String::from(name))))
}
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))
@ -143,7 +150,7 @@ fn parse_receive(i: &str) -> IResult<&str, Action> {
fn parse_actions(i: &str) -> IResult<&str, Vec<Action>> {
let (i, actions) = many1(delimited(
allspaces,
alt((parse_connect, parse_send, parse_receive)),
alt((parse_connect, parse_disconnect, parse_send, parse_receive)),
allspaces,
))(i)?;
Ok((i, actions))
@ -240,6 +247,14 @@ mod tests {
assert_eq!(parse_connect(buf1).unwrap().1, action);
}
#[test]
fn test_action_disconnect() {
let buf1 = "louise disconnects\n";
let action = Action::Disconnect(String::from("louise"));
assert_eq!(parse_disconnect(buf1).unwrap().1, action);
}
#[test]
fn test_action_send_receive() {
let buf = r#"rosa sends: