#[cfg(not(feature = "std"))]
use alloc::format;
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("unknown base code: {code}")]
UnknownBase {
code: char,
},
#[error("invalid base string")]
InvalidBaseString,
#[error("base-x decoding failed")]
BaseXDecode,
#[error("base256emoji decoding failed")]
Base256EmojiDecode,
#[error("data-encoding decoding failed: {message}")]
DataEncodingDecode {
#[cfg(feature = "std")]
message: std::string::String,
#[cfg(not(feature = "std"))]
message: alloc::string::String,
},
#[error("empty input string")]
EmptyInput,
}
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Error::UnknownBase { code: c1 }, Error::UnknownBase { code: c2 }) => c1 == c2,
(Error::InvalidBaseString, Error::InvalidBaseString) => true,
(Error::EmptyInput, Error::EmptyInput) => true,
(Error::BaseXDecode, Error::BaseXDecode) => true,
(Error::Base256EmojiDecode, Error::Base256EmojiDecode) => true,
(
Error::DataEncodingDecode { message: m1 },
Error::DataEncodingDecode { message: m2 },
) => m1 == m2,
_ => false,
}
}
}
impl Eq for Error {}
impl Clone for Error {
fn clone(&self) -> Self {
match self {
Error::UnknownBase { code } => Error::UnknownBase { code: *code },
Error::InvalidBaseString => Error::InvalidBaseString,
Error::BaseXDecode => Error::BaseXDecode,
Error::Base256EmojiDecode => Error::Base256EmojiDecode,
Error::DataEncodingDecode { message } => Error::DataEncodingDecode {
message: message.clone(),
},
Error::EmptyInput => Error::EmptyInput,
}
}
}
impl From<base_x::DecodeError> for Error {
fn from(_: base_x::DecodeError) -> Self {
Error::BaseXDecode
}
}
impl From<data_encoding::DecodeError> for Error {
fn from(err: data_encoding::DecodeError) -> Self {
#[cfg(feature = "std")]
let message = std::format!("{}", err);
#[cfg(not(feature = "std"))]
let message = format!("{}", err);
Error::DataEncodingDecode { message }
}
}