Skip to content

Commit

Permalink
Add Error and ParseResult types (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten authored Dec 11, 2023
1 parent 3c3f494 commit 20a5b0a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ rust-version = "1.72.0"

[dependencies]
nom = "7.1"
thiserror = "1.0.50"
49 changes: 49 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use nom::{
error::{ErrorKind, FromExternalError, ParseError},
IResult,
};

/// The error type for AsciiDoc parsing operations.
#[non_exhaustive]
#[derive(Clone, Debug, thiserror::Error, PartialEq, Eq)]
pub enum Error {
/// AsciiDoc data was incomplete.
#[error("Incomplete data, missing: {0:?}")]
Incomplete(nom::Needed),

/// Error from nom parsing framework.
#[error("nom error: {0:?}")]
NomError(ErrorKind),
}

impl<'a> ParseError<&'a str> for Error {
fn from_error_kind(_input: &'a str, kind: ErrorKind) -> Self {
Error::NomError(kind)
}

fn append(_input: &'a str, kind: ErrorKind, _other: Self) -> Self {
Error::NomError(kind)
}
}

impl From<nom::Err<Error>> for Error {
fn from(e: nom::Err<Error>) -> Self {
match e {
nom::Err::Incomplete(n) => Self::Incomplete(n),
nom::Err::Error(e) | nom::Err::Failure(e) => e,
}
}
}

impl<I, E> FromExternalError<I, E> for Error {
fn from_external_error(_input: I, kind: ErrorKind, _e: E) -> Error {
Error::NomError(kind)
}
}

/// Holds the result of AsciiDoc parsing functions.
///
/// Note that this type is also a [`Result`], so the usual functions (`map`,
/// `unwrap`, etc.) are available.
#[allow(dead_code)] // TEMPORARY
pub type ParseResult<'a, T, E = Error> = IResult<&'a str, T, E>;
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#![deny(warnings)]
#![doc = include_str!("../README.md")]

mod error;
pub use error::{Error, ParseResult};

pub(crate) mod primitives;
pub mod strings;

Expand Down

0 comments on commit 20a5b0a

Please sign in to comment.