use core::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum FromSliceError {
#[non_exhaustive]
InvalidLength {
expected: usize,
got: usize,
},
}
impl fmt::Display for FromSliceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidLength { expected, got } => {
write!(f, "slice length mismatch: expected {expected}, got {got}")
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for FromSliceError {}
#[cfg(any(feature = "encoding-bech32", feature = "encoding-bech32m"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Bech32Error {
InvalidHrp,
ConversionFailed,
OperationFailed,
UnexpectedHrp,
#[non_exhaustive]
InvalidLength {
expected: usize,
got: usize,
},
}
#[cfg(any(feature = "encoding-bech32", feature = "encoding-bech32m"))]
impl fmt::Display for Bech32Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidHrp => f.write_str("invalid Human-Readable Part (HRP)"),
Self::ConversionFailed => f.write_str("bit conversion failed"),
Self::OperationFailed => f.write_str("bech32 operation failed"),
Self::UnexpectedHrp => f.write_str("unexpected HRP"),
Self::InvalidLength { expected, got } => {
write!(f, "decoded length mismatch: expected {expected}, got {got}")
}
}
}
}
#[cfg(all(
feature = "std",
any(feature = "encoding-bech32", feature = "encoding-bech32m")
))]
impl std::error::Error for Bech32Error {}
#[cfg(feature = "encoding-base64")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Base64Error {
InvalidBase64,
#[non_exhaustive]
InvalidLength {
expected: usize,
got: usize,
},
}
#[cfg(feature = "encoding-base64")]
impl fmt::Display for Base64Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidBase64 => f.write_str("invalid base64 string"),
Self::InvalidLength { expected, got } => {
write!(f, "decoded length mismatch: expected {expected}, got {got}")
}
}
}
}
#[cfg(all(feature = "std", feature = "encoding-base64"))]
impl std::error::Error for Base64Error {}
#[cfg(feature = "encoding-hex")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum HexError {
InvalidHex,
#[non_exhaustive]
InvalidLength {
expected: usize,
got: usize,
},
}
#[cfg(feature = "encoding-hex")]
impl fmt::Display for HexError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidHex => f.write_str("invalid hex string"),
Self::InvalidLength { expected, got } => {
write!(f, "decoded length mismatch: expected {expected}, got {got}")
}
}
}
}
#[cfg(all(feature = "std", feature = "encoding-hex"))]
impl std::error::Error for HexError {}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum DecodingError {
#[cfg(feature = "encoding-bech32")]
InvalidBech32(Bech32Error),
#[cfg(feature = "encoding-base64")]
InvalidBase64(Base64Error),
#[cfg(feature = "encoding-hex")]
InvalidHex(HexError),
InvalidEncoding,
}
impl fmt::Display for DecodingError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
#[cfg(feature = "encoding-bech32")]
Self::InvalidBech32(_) => f.write_str("invalid bech32 string"),
#[cfg(feature = "encoding-base64")]
Self::InvalidBase64(_) => f.write_str("invalid base64 string"),
#[cfg(feature = "encoding-hex")]
Self::InvalidHex(_) => f.write_str("invalid hex string"),
Self::InvalidEncoding => f.write_str("invalid encoding"),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for DecodingError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
#[cfg(feature = "encoding-bech32")]
Self::InvalidBech32(inner) => Some(inner),
#[cfg(feature = "encoding-base64")]
Self::InvalidBase64(inner) => Some(inner),
#[cfg(feature = "encoding-hex")]
Self::InvalidHex(inner) => Some(inner),
Self::InvalidEncoding => None,
}
}
}