cpp_demangle/
error.rs

1//! Custom `Error` and `Result` types for the `cpp_demangle` crate.
2
3use core::fmt;
4#[cfg(feature = "std")]
5use std::error;
6
7/// Errors that can occur while demangling a symbol.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub enum Error {
10    /// The mangled symbol ends abruptly.
11    UnexpectedEnd,
12
13    /// The mangled symbol is not well-formed.
14    UnexpectedText,
15
16    /// Found a back reference that is out-of-bounds of the substitution
17    /// table.
18    BadBackReference,
19
20    /// Found a reference to a template arg that is either out-of-bounds, or in
21    /// a context without template args.
22    BadTemplateArgReference,
23
24    /// Found a reference to a template arg from within the arg itself (or from
25    /// within an earlier arg).
26    ForwardTemplateArgReference,
27
28    /// Found a reference to a function arg that is either out-of-bounds, or in
29    /// a context without function args.
30    BadFunctionArgReference,
31
32    /// Found a reference to a leaf name in a context where there is no current
33    /// leaf name.
34    BadLeafNameReference,
35
36    /// An overflow or underflow would occur when parsing an integer in a
37    /// mangled symbol.
38    Overflow,
39
40    /// Encountered too much recursion when demangling symbol.
41    TooMuchRecursion,
42}
43
44#[test]
45fn size_of_error() {
46    assert_eq!(
47        core::mem::size_of::<Error>(),
48        1,
49        "We should keep the size of our Error type in check"
50    );
51}
52
53impl fmt::Display for Error {
54    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55        match *self {
56            Error::UnexpectedEnd => write!(f, "mangled symbol ends abruptly"),
57            Error::UnexpectedText => write!(f, "mangled symbol is not well-formed"),
58            Error::BadBackReference => write!(
59                f,
60                "back reference that is out-of-bounds of the substitution table"
61            ),
62            Error::BadTemplateArgReference => write!(
63                f,
64                "reference to a template arg that is either out-of-bounds, or in a context \
65                 without template args"
66            ),
67            Error::ForwardTemplateArgReference => write!(
68                f,
69                "reference to a template arg from itself or a later template arg"
70            ),
71            Error::BadFunctionArgReference => write!(
72                f,
73                "reference to a function arg that is either out-of-bounds, or in a context \
74                 without function args"
75            ),
76            Error::BadLeafNameReference => write!(
77                f,
78                "reference to a leaf name in a context where there is no current leaf name"
79            ),
80            Error::Overflow => write!(
81                f,
82                "an overflow or underflow would occur when parsing an integer in a mangled \
83                 symbol"
84            ),
85            Error::TooMuchRecursion => {
86                write!(f, "encountered too much recursion when demangling symbol")
87            }
88        }
89    }
90}
91
92#[cfg(feature = "std")]
93impl error::Error for Error {
94    fn description(&self) -> &str {
95        match *self {
96            Error::UnexpectedEnd => "mangled symbol ends abruptly",
97            Error::UnexpectedText => "mangled symbol is not well-formed",
98            Error::BadBackReference => {
99                "back reference that is out-of-bounds of the substitution table"
100            }
101            Error::BadTemplateArgReference => {
102                "reference to a template arg that is either out-of-bounds, or in a context \
103                 without template args"
104            }
105            Error::ForwardTemplateArgReference => {
106                "reference to a template arg from itself or a later template arg"
107            }
108            Error::BadFunctionArgReference => {
109                "reference to a function arg that is either out-of-bounds, or in a context \
110                 without function args"
111            }
112            Error::BadLeafNameReference => {
113                "reference to a leaf name in a context where there is no current leaf name"
114            }
115            Error::Overflow => {
116                "an overflow or underflow would occur when parsing an integer in a mangled symbol"
117            }
118            Error::TooMuchRecursion => "encountered too much recursion when demangling symbol",
119        }
120    }
121}
122
123/// A demangling result of `T` or a `cpp_demangle::error::Error`.
124pub type Result<T> = ::core::result::Result<T, Error>;