1#[cfg(not(feature = "std"))]
4use alloc::format;
5
6pub type Result<T> = core::result::Result<T, Error>;
8
9#[derive(Debug, thiserror::Error)]
18#[non_exhaustive]
19pub enum Error {
20 #[error("unknown base code: {code}")]
25 UnknownBase {
26 code: char,
28 },
29
30 #[error("invalid base string")]
35 InvalidBaseString,
36
37 #[error("base-x decoding failed")]
41 BaseXDecode,
42
43 #[error("base256emoji decoding failed")]
48 Base256EmojiDecode,
49
50 #[error("data-encoding decoding failed: {message}")]
55 DataEncodingDecode {
56 #[cfg(feature = "std")]
58 message: std::string::String,
59 #[cfg(not(feature = "std"))]
61 message: alloc::string::String,
62 },
63
64 #[error("empty input string")]
69 EmptyInput,
70}
71
72impl PartialEq for Error {
74 fn eq(&self, other: &Self) -> bool {
75 match (self, other) {
76 (Error::UnknownBase { code: c1 }, Error::UnknownBase { code: c2 }) => c1 == c2,
77 (Error::InvalidBaseString, Error::InvalidBaseString) => true,
78 (Error::EmptyInput, Error::EmptyInput) => true,
79 (Error::BaseXDecode, Error::BaseXDecode) => true,
80 (Error::Base256EmojiDecode, Error::Base256EmojiDecode) => true,
81 (
82 Error::DataEncodingDecode { message: m1 },
83 Error::DataEncodingDecode { message: m2 },
84 ) => m1 == m2,
85 _ => false,
86 }
87 }
88}
89
90impl Eq for Error {}
92
93impl Clone for Error {
95 fn clone(&self) -> Self {
96 match self {
97 Error::UnknownBase { code } => Error::UnknownBase { code: *code },
98 Error::InvalidBaseString => Error::InvalidBaseString,
99 Error::BaseXDecode => Error::BaseXDecode,
100 Error::Base256EmojiDecode => Error::Base256EmojiDecode,
101 Error::DataEncodingDecode { message } => Error::DataEncodingDecode {
102 message: message.clone(),
103 },
104 Error::EmptyInput => Error::EmptyInput,
105 }
106 }
107}
108
109impl From<base_x::DecodeError> for Error {
111 fn from(_: base_x::DecodeError) -> Self {
112 Error::BaseXDecode
113 }
114}
115
116impl From<data_encoding::DecodeError> for Error {
117 fn from(err: data_encoding::DecodeError) -> Self {
118 #[cfg(feature = "std")]
119 let message = std::format!("{}", err);
120 #[cfg(not(feature = "std"))]
121 let message = format!("{}", err);
122
123 Error::DataEncodingDecode { message }
124 }
125}