diff options
Diffstat (limited to 'rust/vendor/sawp/src/probe.rs')
-rw-r--r-- | rust/vendor/sawp/src/probe.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/rust/vendor/sawp/src/probe.rs b/rust/vendor/sawp/src/probe.rs new file mode 100644 index 0000000..3aa976d --- /dev/null +++ b/rust/vendor/sawp/src/probe.rs @@ -0,0 +1,31 @@ +use crate::error::{Error, ErrorKind}; +use crate::parser::{Direction, Parse}; +use crate::protocol::Protocol; + +/// Result of probing the underlying bytes. +#[derive(Debug, PartialEq)] +pub enum Status { + /// Data matches this protocol + Recognized, + /// Data does not match this protocol + Unrecognized, + /// More data is needed to make a decision + Incomplete, +} + +pub trait Probe<'a>: Protocol<'a> + Parse<'a> { + /// Probes the input to recognize if the underlying bytes likely match this + /// protocol. + /// + /// Returns a probe status. Probe again once more data is available when the + /// status is `Status::Incomplete`. + fn probe(&self, input: &'a [u8], direction: Direction) -> Status { + match self.parse(input, direction) { + Ok((_, _)) => Status::Recognized, + Err(Error { + kind: ErrorKind::Incomplete(_), + }) => Status::Incomplete, + Err(_) => Status::Unrecognized, + } + } +} |