use std::fmt;
use std::result::Result as StdResult;
use thiserror::Error;
use crate::{common::Error as HsError, ffi};
pub type Result<T> = StdResult<T, Error>;
#[derive(Debug, Error, PartialEq, Eq)]
pub enum Error {
#[error(transparent)]
Hyperscan(#[from] crate::common::Error),
#[cfg(feature = "chimera")]
#[error(transparent)]
Chimera(#[from] crate::chimera::Error),
#[error(transparent)]
Expr(#[from] crate::compile::ExprError),
#[error(transparent)]
Utf8(#[from] std::str::Utf8Error),
#[error(transparent)]
ParseInt(#[from] std::num::ParseIntError),
#[error(transparent)]
NulByte(#[from] std::ffi::NulError),
#[error("invalid pattern flag: {0}")]
InvalidFlag(char),
}
pub trait AsResult
where
Self: Sized,
{
type Output;
type Error: fmt::Debug;
fn ok(self) -> StdResult<Self::Output, Self::Error>;
fn map<U, F: FnOnce(Self::Output) -> U>(self, op: F) -> StdResult<U, Self::Error> {
self.ok().map(op)
}
fn and_then<U, F: FnOnce(Self::Output) -> StdResult<U, Self::Error>>(self, op: F) -> StdResult<U, Self::Error> {
self.ok().and_then(op)
}
fn expect(self, msg: &str) -> Self::Output {
self.ok().expect(msg)
}
}
impl AsResult for ffi::hs_error_t {
type Output = ();
type Error = Error;
fn ok(self) -> StdResult<Self::Output, Self::Error> {
if self == ffi::HS_SUCCESS as ffi::hs_error_t {
Ok(())
} else {
Err(HsError::from(self).into())
}
}
}