use core::fmt::{Display, Formatter, Result as FmtResult};
pub type Result<T, E = Error> = core::result::Result<T, E>;
#[non_exhaustive]
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
#[cfg(feature = "age")]
AgeFormatError(crate::keys::age::DecError),
#[cfg(feature = "bip39")]
Bip39Error(crate::keys::bip39::Error),
BufferSize {
name: &'static str,
needs: usize,
has: usize,
},
CipherError { alg: &'static str },
SignatureError { alg: &'static str },
ConvertError { from: &'static str, to: &'static str },
PrivateKeyError,
InvalidArgumentError { alg: &'static str, expected: &'static str },
#[cfg(feature = "slip10")]
Slip10Error(crate::keys::slip10::SegmentHardeningError),
SystemError {
call: &'static str,
raw_os_error: Option<i32>,
},
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
#[cfg(feature = "age")]
Error::AgeFormatError(inner) => write!(f, "failed to decode/decrypt age format: {inner:?}"),
#[cfg(feature = "bip39")]
Error::Bip39Error(inner) => write!(f, "bip39 error: {inner:?}"),
Error::BufferSize { name, needs, has } => {
write!(f, "{} buffer needs {} bytes, but it only has {}", name, needs, has)
}
Error::CipherError { alg } => write!(f, "error in algorithm {}", alg),
Error::SignatureError { alg } => write!(f, "error in signature algorithm {}", alg),
Error::ConvertError { from, to } => write!(f, "failed to convert {} to {}", from, to),
Error::PrivateKeyError => write!(f, "Failed to generate private key."),
Error::InvalidArgumentError { alg, expected } => write!(f, "{} expects {}", alg, expected),
#[cfg(feature = "slip10")]
Error::Slip10Error(inner) => write!(f, "slip10 error: {inner:?}"),
Error::SystemError {
call,
raw_os_error: None,
} => write!(f, "system error when calling {}", call),
Error::SystemError {
call,
raw_os_error: Some(errno),
} => write!(f, "system error when calling {}: {}", call, errno),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}