1#[cfg(feature = "std")]
4use std::error;
5use std::fmt;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub enum Error {
10 UnexpectedEnd,
12
13 UnexpectedText,
15
16 BadBackReference,
19
20 BadTemplateArgReference,
23
24 ForwardTemplateArgReference,
27
28 BadFunctionArgReference,
31
32 BadLeafNameReference,
35
36 Overflow,
39
40 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
124pub type Result<T> = ::std::result::Result<T, Error>;