1//! Custom `Error` and `Result` types for the `cpp_demangle` crate.
23use core::fmt;
4#[cfg(feature = "std")]
5use std::error;
67/// 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.
11UnexpectedEnd,
1213/// The mangled symbol is not well-formed.
14UnexpectedText,
1516/// Found a back reference that is out-of-bounds of the substitution
17 /// table.
18BadBackReference,
1920/// Found a reference to a template arg that is either out-of-bounds, or in
21 /// a context without template args.
22BadTemplateArgReference,
2324/// Found a reference to a template arg from within the arg itself (or from
25 /// within an earlier arg).
26ForwardTemplateArgReference,
2728/// Found a reference to a function arg that is either out-of-bounds, or in
29 /// a context without function args.
30BadFunctionArgReference,
3132/// Found a reference to a leaf name in a context where there is no current
33 /// leaf name.
34BadLeafNameReference,
3536/// An overflow or underflow would occur when parsing an integer in a
37 /// mangled symbol.
38Overflow,
3940/// Encountered too much recursion when demangling symbol.
41TooMuchRecursion,
42}
4344#[test]
45fn size_of_error() {
46assert_eq!(
47 core::mem::size_of::<Error>(),
481,
49"We should keep the size of our Error type in check"
50);
51}
5253impl fmt::Display for Error {
54fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55match *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 => {
86write!(f, "encountered too much recursion when demangling symbol")
87 }
88 }
89 }
90}
9192#[cfg(feature = "std")]
93impl error::Error for Error {
94fn description(&self) -> &str {
95match *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}
122123/// A demangling result of `T` or a `cpp_demangle::error::Error`.
124pub type Result<T> = ::core::result::Result<T, Error>;