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};

/// A type for errors returned by Chimera functions.
#[derive(Debug, Error, PartialEq, Eq)]
pub enum Error {
    /// A parameter passed to this function was invalid.
    #[error("A parameter passed to this function was invalid.")]
    Invalid,

    /// A memory allocation failed.
    #[error("A memory allocation failed.")]
    NoMem,

    /// The engine was terminated by callback.
    ///
    /// This return value indicates that the target buffer was partially scanned,
    /// but that the callback function requested that scanning cease after a match
    /// was located.
    #[error("The engine was terminated by callback.")]
    ScanTerminated,

    /// The pattern compiler failed, and the `ch_compile_error_t` should be inspected for more detail.
    #[error("The pattern compiler failed with more detail, {0}.")]
    CompileError(CompileError),

    /// The pattern compiler failed.
    #[error("he pattern compiler failed.")]
    CompilerError,

    /// The given database was built for a different version of the Chimera matcher.
    #[error("he pattern compiler failed.")]
    DbVersionError,

    /// The given database was built for a different platform (i.e., CPU type).
    #[error("The given database was built for a different platform (i.e., CPU type).")]
    DbPlatformError,

    /// The given database was built for a different mode of operation.
    ///
    /// This error is returned when streaming calls are used with a non-streaming database and vice versa.
    #[error("The given database was built for a different mode of operation.")]
    DbModeError,

    /// A parameter passed to this function was not correctly aligned.
    #[error("A parameter passed to this function was not correctly aligned.")]
    BadAlign,

    /// The memory allocator did not correctly return memory suitably aligned for
    /// the largest representable data type on this platform.
    #[error("The memory allocator did not correctly return memory suitably aligned.")]
    BadAlloc,

    /// The scratch region was already in use.
    ///
    /// This error is returned when Chimera is able to detect that the scratch
    /// region given is already in use by another Chimera API call.
    ///
    /// A separate scratch region, allocated with @ref ch_alloc_scratch() or @ref
    /// ch_clone_scratch(), is required for every concurrent caller of the Chimera
    /// API.
    ///
    /// For example, this error might be returned when @ref ch_scan() has been
    /// called inside a callback delivered by a currently-executing @ref ch_scan()
    /// call using the same scratch region.
    ///
    /// Note: Not all concurrent uses of scratch regions may be detected. This error
    /// is intended as a best-effort debugging tool, not a guarantee.
    #[error("The scratch region was already in use.")]
    ScratchInUse,

    /// Unexpected internal error from Hyperscan.
    ///
    /// This error indicates that there was unexpected matching behaviors from Hyperscan.
    /// This could be related to invalid usage of scratch space or invalid memory operations by users.
    #[error("Unexpected internal error from Hyperscan.")]
    UnknownError,

    /// Returned when pcre_exec (called for some expressions internally from `ch_scan`) failed due to a fatal error.
    #[cfg(feature = "v5_4")]
    #[error("Failed due to a fatal error")]
    FailInternal,

    /// Unexpected internal error from Hyperscan.
    ///
    /// This error indicates that there was unexpected matching behaviors from Hyperscan.
    /// This could be related to invalid usage of scratch space or invalid memory operations by users.
    #[error("Unexpected internal error from Hyperscan.")]
    UnknownHSError,

    /// Unknown error code
    #[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_COMPILER_ERROR => HsError::CompileError,
            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())
        }
    }
}