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 (Self::UnknownBase { code: c1 }, Self::UnknownBase { code: c2 }) => c1 == c2,
77 (
78 Self::DataEncodingDecode { message: m1 },
79 Self::DataEncodingDecode { message: m2 },
80 ) => m1 == m2,
81 (Self::InvalidBaseString, Self::InvalidBaseString)
82 | (Self::EmptyInput, Self::EmptyInput)
83 | (Self::BaseXDecode, Self::BaseXDecode)
84 | (Self::Base256EmojiDecode, Self::Base256EmojiDecode) => true,
85 _ => false,
86 }
87 }
88}
89
90impl Eq for Error {}
92
93impl Clone for Error {
95 fn clone(&self) -> Self {
96 match self {
97 Self::UnknownBase { code } => Self::UnknownBase { code: *code },
98 Self::InvalidBaseString => Self::InvalidBaseString,
99 Self::BaseXDecode => Self::BaseXDecode,
100 Self::Base256EmojiDecode => Self::Base256EmojiDecode,
101 Self::DataEncodingDecode { message } => Self::DataEncodingDecode {
102 message: message.clone(),
103 },
104 Self::EmptyInput => Self::EmptyInput,
105 }
106 }
107}
108
109impl From<base_x::DecodeError> for Error {
111 fn from(_: base_x::DecodeError) -> Self {
112 Self::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 Self::DataEncodingDecode { message }
124 }
125}