529 ) |
529 ) |
530 }, |
530 }, |
531 ))(input) |
531 ))(input) |
532 } |
532 } |
533 |
533 |
534 fn empty_message(input: &[u8]) -> HWResult<HWProtocolMessage> { |
534 pub fn malformed_message(input: &[u8]) -> HWResult<()> { |
535 let (i, _) = alt((end_of_message, eol))(input)?; |
535 let (i, _) = terminatedc(input, |i| take_until(&b"\n\n"[..])(i), end_of_message)?; |
536 Ok((i, Empty)) |
536 Ok((i, ())) |
537 } |
537 } |
538 |
538 |
539 fn malformed_message(input: &[u8]) -> HWResult<HWProtocolMessage> { |
539 pub fn message(input: &[u8]) -> HWResult<HWProtocolMessage> { |
540 let (i, _) = separated_listc(input, eol, a_line)?; |
540 precededc( |
541 Ok((i, Malformed)) |
541 input, |
542 } |
542 |i| take_while(|c| c == b'\n')(i), |
543 |
|
544 fn message(input: &[u8]) -> HWResult<HWProtocolMessage> { |
|
545 alt(( |
|
546 |i| { |
543 |i| { |
547 terminatedc( |
544 terminatedc( |
548 i, |
545 i, |
549 alt(( |
546 alt(( |
550 no_arg_message, |
547 no_arg_message, |
555 complex_message, |
552 complex_message, |
556 )), |
553 )), |
557 end_of_message, |
554 end_of_message, |
558 ) |
555 ) |
559 }, |
556 }, |
560 |i| terminatedc(i, malformed_message, end_of_message), |
557 ) |
561 empty_message, |
558 } |
562 ))(input) |
559 |
563 } |
560 fn extract_messages(input: &[u8]) -> HWResult<Vec<HWProtocolMessage>> { |
564 |
561 many0(message)(input) |
565 pub fn extract_messages<'a>(input: &'a [u8]) -> HWResult<Vec<HWProtocolMessage>> { |
|
566 many0(|i: &'a [u8]| complete!(i, message))(input) |
|
567 } |
562 } |
568 |
563 |
569 #[cfg(test)] |
564 #[cfg(test)] |
570 mod test { |
565 mod test { |
571 use super::{extract_messages, message}; |
566 use super::{extract_messages, message}; |
|
567 use crate::protocol::parser::HWProtocolError; |
572 use crate::protocol::{messages::HWProtocolMessage::*, test::gen_proto_msg}; |
568 use crate::protocol::{messages::HWProtocolMessage::*, test::gen_proto_msg}; |
573 use proptest::{proptest, proptest_helper}; |
569 use proptest::{proptest, proptest_helper}; |
574 |
570 |
575 #[cfg(test)] |
571 #[cfg(test)] |
576 proptest! { |
572 proptest! { |
594 message(b"QUIT\nbye-bye\n\n"), |
590 message(b"QUIT\nbye-bye\n\n"), |
595 Ok((&b""[..], Quit(Some("bye-bye".to_string())))) |
591 Ok((&b""[..], Quit(Some("bye-bye".to_string())))) |
596 ); |
592 ); |
597 assert_eq!(message(b"QUIT\n\n"), Ok((&b""[..], Quit(None)))); |
593 assert_eq!(message(b"QUIT\n\n"), Ok((&b""[..], Quit(None)))); |
598 assert_eq!( |
594 assert_eq!( |
599 message(b"CMD\nwatch demo\n\n"), |
595 message(b"CMD\nwatch 49471\n\n"), |
600 Ok((&b""[..], Watch("demo".to_string()))) |
596 Ok((&b""[..], Watch(49471))) |
601 ); |
597 ); |
602 assert_eq!( |
598 assert_eq!( |
603 message(b"BAN\nme\nbad\n77\n\n"), |
599 message(b"BAN\nme\nbad\n77\n\n"), |
604 Ok((&b""[..], Ban("me".to_string(), "bad".to_string(), 77))) |
600 Ok((&b""[..], Ban("me".to_string(), "bad".to_string(), 77))) |
605 ); |
601 ); |
615 message(b"CMD\nRND A B\n\n"), |
611 message(b"CMD\nRND A B\n\n"), |
616 Ok((&b""[..], Rnd(vec![String::from("A"), String::from("B")]))) |
612 Ok((&b""[..], Rnd(vec![String::from("A"), String::from("B")]))) |
617 ); |
613 ); |
618 |
614 |
619 assert_eq!( |
615 assert_eq!( |
620 extract_messages(b"QUIT\n1\n2\n\n"), |
616 message(b"QUIT\n1\n2\n\n"), |
621 Ok((&b""[..], vec![Malformed])) |
617 Err(nom::Err::Error(HWProtocolError::new())) |
622 ); |
618 ); |
623 |
619 |
624 assert_eq!( |
|
625 extract_messages(b"PING\n\nPING\n\nP"), |
|
626 Ok((&b"P"[..], vec![Ping, Ping])) |
|
627 ); |
|
628 assert_eq!( |
|
629 extract_messages(b"SING\n\nPING\n\n"), |
|
630 Ok((&b""[..], vec![Malformed, Ping])) |
|
631 ); |
|
632 assert_eq!( |
620 assert_eq!( |
633 extract_messages(b"\n\n\n\nPING\n\n"), |
621 extract_messages(b"\n\n\n\nPING\n\n"), |
634 Ok((&b""[..], vec![Empty, Empty, Ping])) |
622 Ok((&b""[..], vec![Ping])) |
635 ); |
623 ); |
636 assert_eq!( |
624 assert_eq!( |
637 extract_messages(b"\n\n\nPING\n\n"), |
625 extract_messages(b"\n\n\nPING\n\n"), |
638 Ok((&b""[..], vec![Empty, Empty, Ping])) |
626 Ok((&b""[..], vec![Ping])) |
639 ); |
627 ); |
640 } |
628 } |
641 } |
629 } |