use crate::re2;
use displaydoc::Display;
use thiserror::Error;
use std::os::raw::c_uint;
#[derive(
Debug,
Display,
Error,
Copy,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
num_enum::IntoPrimitive,
num_enum::FromPrimitive,
)]
#[repr(u32)]
pub enum RE2ErrorCode {
Internal = re2::RE2_ErrorCode_ErrorInternal,
BadEscape = re2::RE2_ErrorCode_ErrorBadEscape,
BadCharClass = re2::RE2_ErrorCode_ErrorBadCharClass,
BadCharRange = re2::RE2_ErrorCode_ErrorBadCharRange,
MissingBracket = re2::RE2_ErrorCode_ErrorMissingBracket,
MissingParen = re2::RE2_ErrorCode_ErrorMissingParen,
UnexpectedParen = re2::RE2_ErrorCode_ErrorUnexpectedParen,
TrailingBackslash = re2::RE2_ErrorCode_ErrorTrailingBackslash,
RepeatArgument = re2::RE2_ErrorCode_ErrorRepeatArgument,
RepeatSize = re2::RE2_ErrorCode_ErrorRepeatSize,
RepeatOp = re2::RE2_ErrorCode_ErrorRepeatOp,
BadPerlOp = re2::RE2_ErrorCode_ErrorBadPerlOp,
BadUTF8 = re2::RE2_ErrorCode_ErrorBadUTF8,
BadNamedCapture = re2::RE2_ErrorCode_ErrorBadNamedCapture,
PatternTooLarge = re2::RE2_ErrorCode_ErrorPatternTooLarge,
#[num_enum(default)]
UnknownError = 300,
}
impl RE2ErrorCode {
pub(crate) fn from_native(x: re2::RE2_ErrorCode) -> Result<(), Self> {
static_assertions::const_assert_eq!(0, re2::RE2_ErrorCode_NoError);
if x == 0 {
Ok(())
} else {
let s: Self = (x as c_uint).into();
Err(s)
}
}
}
#[derive(Debug, Display, Error, PartialEq, Eq, Hash)]
pub struct CompileError {
pub message: String,
pub arg: String,
#[source]
pub code: RE2ErrorCode,
}
#[derive(Debug, Display, Error, PartialEq, Eq, Hash)]
pub struct RewriteError {
pub message: String,
}
#[derive(Debug, Display, Error)]
pub enum SetError {
Runtime(#[from] SetErrorInfo),
Pattern(#[from] SetPatternError),
SetCompile(#[from] SetCompileError),
}
#[derive(
Debug,
Display,
Error,
Copy,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
num_enum::IntoPrimitive,
num_enum::FromPrimitive,
)]
#[repr(u32)]
pub enum SetErrorKind {
NotCompiled = re2::RE2_Set_ErrorKind_kNotCompiled,
OutOfMemory = re2::RE2_Set_ErrorKind_kOutOfMemory,
Inconsistent = re2::RE2_Set_ErrorKind_kInconsistent,
#[num_enum(default)]
UnknownError = 300,
}
impl SetErrorKind {
pub(crate) fn from_native(x: re2::RE2_Set_ErrorKind) -> Result<(), Self> {
static_assertions::const_assert_eq!(0, re2::RE2_Set_ErrorKind_kNoError);
if x == 0 {
Ok(())
} else {
let s: Self = (x as c_uint).into();
Err(s)
}
}
}
#[derive(Debug, Display, Error, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct SetErrorInfo {
#[from]
pub kind: SetErrorKind,
}
impl SetErrorInfo {
pub(crate) fn from_native(x: re2::RE2_Set_ErrorInfo) -> Result<(), Self> {
let re2::RE2_Set_ErrorInfo { kind } = x;
SetErrorKind::from_native(kind)?;
Ok(())
}
}
#[derive(Debug, Display, Error, PartialEq, Eq, Hash)]
pub struct SetPatternError {
pub message: String,
}
#[derive(Debug, Display, Error, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SetCompileError {
OutOfMemory,
}
#[derive(Debug, Display, Error)]
pub enum RE2Error {
Runtime(#[from] RE2ErrorCode),
Compile(#[from] CompileError),
Rewrite(#[from] RewriteError),
Set(#[from] SetError),
}