| 1 |
|
| 2 |
|
| 3 |
use core::fmt; |
| 4 |
#[cfg(feature = "std")] |
| 5 |
use std::error; |
| 6 |
|
| 7 |
|
| 8 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 9 |
pub enum Error { |
| 10 |
|
| 11 |
UnexpectedEnd, |
| 12 |
|
| 13 |
|
| 14 |
UnexpectedText, |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
BadBackReference, |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
BadTemplateArgReference, |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
ForwardTemplateArgReference, |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
BadFunctionArgReference, |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
BadLeafNameReference, |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
Overflow, |
| 39 |
|
| 40 |
|
| 41 |
TooMuchRecursion, |
| 42 |
} |
| 43 |
|
| 44 |
#[test] |
| 45 |
fn 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 |
|
| 53 |
impl 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")] |
| 93 |
impl 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 |
|
| 124 |
pub type Result<T> = ::core::result::Result<T, Error>; |