1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/// Pre-built parsers for convenience.
pub mod parser;

/// Generic parser trait.
/// Only `Output` and `parse_unprotected` need to be defined to apply the trait.
pub trait Parser<'parser> {
    type Input;
    type Output;
    /// Parse `data` given `offset`.
    /// May leave `offset` in an incorrect state on failure.
    fn parse(&self, data: &'parser [Self::Input]) -> ParseResult<Self::Output>;
}

/// A wrapper around `Result` with `ParseError` as the error.
pub type ParseResult<T> = Result<(T, usize), ParseError>;

/// A container for all the different possible errors when parsing.
#[derive(Debug)]
pub enum ParseError {
    InvalidData,
    NotEnoughData,
    Other(&'static str),
    GenericError(Box<dyn std::error::Error>),
}

/// Includes all of the necessary traits for working with nyst.
pub mod prelude {
    pub use super::{ParseError, ParseResult, Parser};
}