pe_sigscan/error.rs
1//! Error types returned by IDA-style pattern parsing.
2//!
3//! See [`crate::Pattern::from_ida`] for the producer; [`ParsePatternError`]
4//! is the only error this crate exposes.
5
6/// Error returned by [`crate::Pattern::from_ida`] when the input string
7/// contains an invalid token.
8///
9/// The error captures both the offending token's index and a categorised
10/// reason ([`ParseErrorKind`]) so callers can produce a precise diagnostic
11/// without re-parsing the input themselves.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub struct ParsePatternError {
14 /// Zero-based index of the offending whitespace-separated token.
15 pub token_index: usize,
16 /// What was wrong with the token.
17 pub kind: ParseErrorKind,
18}
19
20/// Categories of [`ParsePatternError`].
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub enum ParseErrorKind {
23 /// The pattern string contained no tokens.
24 Empty,
25 /// Token was not exactly two hex digits or `?` / `??`.
26 InvalidLength,
27 /// Token had two characters but at least one was a non-hex digit (and
28 /// it wasn't a `??` wildcard).
29 InvalidHexDigit,
30}
31
32impl core::fmt::Display for ParsePatternError {
33 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
34 match self.kind {
35 ParseErrorKind::Empty => write!(f, "pattern string contained no tokens"),
36 ParseErrorKind::InvalidLength => {
37 write!(
38 f,
39 "token #{} must be two hex digits or `?` / `??`",
40 self.token_index
41 )
42 }
43 ParseErrorKind::InvalidHexDigit => {
44 write!(f, "token #{} contains a non-hex digit", self.token_index)
45 }
46 }
47 }
48}
49
50#[cfg(feature = "std")]
51impl std::error::Error for ParsePatternError {}