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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use std::fmt;
use std::result::Result as StdResult;
use thiserror::Error;
use crate::{chimera::CompileError, ffi::chimera as ffi};
#[derive(Debug, Error, PartialEq, Eq)]
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,
#[error("The pattern compiler failed with more detail, {0}.")]
CompileError(CompileError),
#[error("he pattern compiler failed.")]
CompilerError,
#[error("he pattern compiler failed.")]
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("Unexpected internal error from Hyperscan.")]
UnknownError,
#[cfg(feature = "v5_4")]
#[error("Failed due to a fatal error")]
FailInternal,
#[error("Unexpected internal error from Hyperscan.")]
UnknownHSError,
#[error("Unknown error code: {0}")]
Code(ffi::ch_error_t),
}
impl From<ffi::ch_error_t> for Error {
fn from(err: ffi::ch_error_t) -> Self {
use Error::*;
match err {
ffi::CH_INVALID => Invalid,
ffi::CH_NOMEM => NoMem,
ffi::CH_SCAN_TERMINATED => ScanTerminated,
ffi::CH_DB_VERSION_ERROR => DbVersionError,
ffi::CH_DB_PLATFORM_ERROR => DbPlatformError,
ffi::CH_DB_MODE_ERROR => DbModeError,
ffi::CH_BAD_ALIGN => BadAlign,
ffi::CH_BAD_ALLOC => BadAlloc,
ffi::CH_SCRATCH_IN_USE => ScratchInUse,
#[cfg(feature = "v5_4")]
ffi::CH_FAIL_INTERNAL => FailInternal,
ffi::CH_UNKNOWN_HS_ERROR => UnknownHSError,
_ => Code(err),
}
}
}
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::ch_error_t {
type Output = ();
type Error = crate::Error;
fn ok(self) -> StdResult<Self::Output, Self::Error> {
if self == ffi::CH_SUCCESS as ffi::ch_error_t {
Ok(())
} else {
Err(Error::from(self).into())
}
}
}