cpp_demangle/
error.rs

1//! Custom `Error` and `Result` types for the `cpp_demangle` crate.
2
3#[cfg(feature = "std")]
4use std::error;
5use std::fmt;
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    use std::mem;
47    assert_eq!(
48        mem::size_of::<Error>(),
49        1,
50        "We should keep the size of our Error type in check"
51    );
52}
53
54impl fmt::Display for Error {
55    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56        match *self {
57            Error::UnexpectedEnd => write!(f, "mangled symbol ends abruptly"),
58            Error::UnexpectedText => write!(f, "mangled symbol is not well-formed"),
59            Error::BadBackReference => write!(
60                f,
61                "back reference that is out-of-bounds of the substitution table"
62            ),
63            Error::BadTemplateArgReference => write!(
64                f,
65                "reference to a template arg that is either out-of-bounds, or in a context \
66                 without template args"
67            ),
68            Error::ForwardTemplateArgReference => write!(
69                f,
70                "reference to a template arg from itself or a later template arg"
71            ),
72            Error::BadFunctionArgReference => write!(
73                f,
74                "reference to a function arg that is either out-of-bounds, or in a context \
75                 without function args"
76            ),
77            Error::BadLeafNameReference => write!(
78                f,
79                "reference to a leaf name in a context where there is no current leaf name"
80            ),
81            Error::Overflow => write!(
82                f,
83                "an overflow or underflow would occur when parsing an integer in a mangled \
84                 symbol"
85            ),
86            Error::TooMuchRecursion => {
87                write!(f, "encountered too much recursion when demangling symbol")
88            }
89        }
90    }
91}
92
93#[cfg(feature = "std")]
94impl error::Error for Error {
95    fn description(&self) -> &str {
96        match *self {
97            Error::UnexpectedEnd => "mangled symbol ends abruptly",
98            Error::UnexpectedText => "mangled symbol is not well-formed",
99            Error::BadBackReference => {
100                "back reference that is out-of-bounds of the substitution table"
101            }
102            Error::BadTemplateArgReference => {
103                "reference to a template arg that is either out-of-bounds, or in a context \
104                 without template args"
105            }
106            Error::ForwardTemplateArgReference => {
107                "reference to a template arg from itself or a later template arg"
108            }
109            Error::BadFunctionArgReference => {
110                "reference to a function arg that is either out-of-bounds, or in a context \
111                 without function args"
112            }
113            Error::BadLeafNameReference => {
114                "reference to a leaf name in a context where there is no current leaf name"
115            }
116            Error::Overflow => {
117                "an overflow or underflow would occur when parsing an integer in a mangled symbol"
118            }
119            Error::TooMuchRecursion => "encountered too much recursion when demangling symbol",
120        }
121    }
122}
123
124/// A demangling result of `T` or a `cpp_demangle::error::Error`.
125pub type Result<T> = ::std::result::Result<T, Error>;