use std::fmt;
use thiserror::Error;
#[cfg(feature = "compile")]
use crate::compile::Error as CompileError;
use crate::ffi;
#[derive(Debug, Error, PartialEq)]
pub enum Error {
#[error("A parameter passed to this function was invalid.")]
Invalid,
#[error("A memory allocation failed.")]
NoMem,
#[error("The engine was terminated by callback.")]
ScanTerminated,
#[cfg(feature = "compile")]
#[error("The pattern compiler failed with more detail, {0}.")]
CompileError(CompileError),
#[error("The given database was built for a different version of Hyperscan.")]
DbVersionError,
#[error("The given database was built for a different platform (i.e., CPU type).")]
DbPlatformError,
#[error("The given database was built for a different mode of operation.")]
DbModeError,
#[error("A parameter passed to this function was not correctly aligned.")]
BadAlign,
#[error("The memory allocator did not correctly return memory suitably aligned.")]
BadAlloc,
#[error("The scratch region was already in use.")]
ScratchInUse,
#[error("Unsupported CPU architecture.")]
ArchError,
#[error("Provided buffer was too small.")]
InsufficientSpace,
#[cfg(feature = "v5")]
#[error("Unexpected internal error.")]
UnknownError,
#[error("Unknown error code: {0}")]
Code(ffi::hs_error_t),
}
impl From<ffi::hs_error_t> for Error {
fn from(err: ffi::hs_error_t) -> Self {
use Error::*;
match err {
ffi::HS_INVALID => Invalid,
ffi::HS_NOMEM => NoMem,
ffi::HS_SCAN_TERMINATED => ScanTerminated,
ffi::HS_DB_VERSION_ERROR => DbVersionError,
ffi::HS_DB_PLATFORM_ERROR => DbPlatformError,
ffi::HS_DB_MODE_ERROR => DbModeError,
ffi::HS_BAD_ALIGN => BadAlign,
ffi::HS_BAD_ALLOC => BadAlloc,
ffi::HS_SCRATCH_IN_USE => ScratchInUse,
ffi::HS_ARCH_ERROR => ArchError,
ffi::HS_INSUFFICIENT_SPACE => InsufficientSpace,
#[cfg(feature = "v5")]
ffi::HS_UNKNOWN_ERROR => UnknownError,
_ => Code(err),
}
}
}
pub trait AsResult
where
Self: Sized,
{
type Output;
type Error: fmt::Debug;
fn ok(self) -> Result<Self::Output, Self::Error>;
fn map<U, F: FnOnce(Self::Output) -> U>(self, op: F) -> Result<U, Self::Error> {
self.ok().map(op)
}
fn and_then<U, F: FnOnce(Self::Output) -> Result<U, Self::Error>>(self, op: F) -> Result<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 = anyhow::Error;
fn ok(self) -> Result<Self::Output, Self::Error> {
if self == ffi::HS_SUCCESS as ffi::hs_error_t {
Ok(())
} else {
Err(Error::from(self).into())
}
}
}