#[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) {
(Self::UnknownBase { code: c1 }, Self::UnknownBase { code: c2 }) => c1 == c2,
(
Self::DataEncodingDecode { message: m1 },
Self::DataEncodingDecode { message: m2 },
) => m1 == m2,
(Self::InvalidBaseString, Self::InvalidBaseString)
| (Self::EmptyInput, Self::EmptyInput)
| (Self::BaseXDecode, Self::BaseXDecode)
| (Self::Base256EmojiDecode, Self::Base256EmojiDecode) => true,
_ => false,
}
}
}
impl Eq for Error {}
impl Clone for Error {
fn clone(&self) -> Self {
match self {
Self::UnknownBase { code } => Self::UnknownBase { code: *code },
Self::InvalidBaseString => Self::InvalidBaseString,
Self::BaseXDecode => Self::BaseXDecode,
Self::Base256EmojiDecode => Self::Base256EmojiDecode,
Self::DataEncodingDecode { message } => Self::DataEncodingDecode {
message: message.clone(),
},
Self::EmptyInput => Self::EmptyInput,
}
}
}
impl From<base_x::DecodeError> for Error {
fn from(_: base_x::DecodeError) -> Self {
Self::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);
Self::DataEncodingDecode { message }
}
}