/rust/registry/src/index.crates.io-6f17d22bba15001f/sawp-0.12.1/src/probe.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use crate::error::{Error, ErrorKind}; |
2 | | use crate::parser::{Direction, Parse}; |
3 | | use crate::protocol::Protocol; |
4 | | |
5 | | /// Result of probing the underlying bytes. |
6 | | #[derive(Debug, PartialEq)] |
7 | | pub enum Status { |
8 | | /// Data matches this protocol |
9 | | Recognized, |
10 | | /// Data does not match this protocol |
11 | | Unrecognized, |
12 | | /// More data is needed to make a decision |
13 | | Incomplete, |
14 | | } |
15 | | |
16 | | pub trait Probe<'a>: Protocol<'a> + Parse<'a> { |
17 | | /// Probes the input to recognize if the underlying bytes likely match this |
18 | | /// protocol. |
19 | | /// |
20 | | /// Returns a probe status. Probe again once more data is available when the |
21 | | /// status is `Status::Incomplete`. |
22 | 0 | fn probe(&self, input: &'a [u8], direction: Direction) -> Status { |
23 | 0 | match self.parse(input, direction) { |
24 | 0 | Ok((_, _)) => Status::Recognized, |
25 | | Err(Error { |
26 | | kind: ErrorKind::Incomplete(_), |
27 | 0 | }) => Status::Incomplete, |
28 | 0 | Err(_) => Status::Unrecognized, |
29 | | } |
30 | 0 | } |
31 | | } |