1use core::fmt::{Display, Formatter, Result as FmtResult};
5
6pub type Result<T, E = Error> = core::result::Result<T, E>;
7
8#[non_exhaustive]
10#[derive(Debug, PartialEq, Eq)]
11pub enum Error {
12 #[cfg(feature = "age")]
14 AgeFormatError(crate::keys::age::DecError),
15 #[cfg(feature = "bip39")]
17 Bip39Error(crate::keys::bip39::Error),
18 #[cfg(feature = "bip44")]
20 Bip44Error(crate::keys::bip44::BadPurpose),
21 BufferSize {
23 name: &'static str,
24 needs: usize,
25 has: usize,
26 },
27 CipherError { alg: &'static str },
29 SignatureError { alg: &'static str },
31 ConvertError { from: &'static str, to: &'static str },
33 PrivateKeyError,
35 InvalidArgumentError { alg: &'static str, expected: &'static str },
37 #[cfg(feature = "slip10")]
38 Slip10Error(crate::keys::slip10::SegmentHardeningError),
40 SystemError {
42 call: &'static str,
43 raw_os_error: Option<i32>,
44 },
45}
46
47impl Display for Error {
48 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
49 match self {
50 #[cfg(feature = "age")]
51 Error::AgeFormatError(inner) => write!(f, "failed to decode/decrypt age format: {inner:?}"),
52 #[cfg(feature = "bip39")]
53 Error::Bip39Error(inner) => write!(f, "bip39 error: {inner:?}"),
54 #[cfg(feature = "bip44")]
55 Error::Bip44Error(inner) => write!(f, "bip44 error: {inner:?}"),
56 Error::BufferSize { name, needs, has } => {
57 write!(f, "{} buffer needs {} bytes, but it only has {}", name, needs, has)
58 }
59 Error::CipherError { alg } => write!(f, "error in algorithm {}", alg),
60 Error::SignatureError { alg } => write!(f, "error in signature algorithm {}", alg),
61 Error::ConvertError { from, to } => write!(f, "failed to convert {} to {}", from, to),
62 Error::PrivateKeyError => write!(f, "Failed to generate private key."),
63 Error::InvalidArgumentError { alg, expected } => write!(f, "{} expects {}", alg, expected),
64 #[cfg(feature = "slip10")]
65 Error::Slip10Error(inner) => write!(f, "slip10 error: {inner:?}"),
66 Error::SystemError {
67 call,
68 raw_os_error: None,
69 } => write!(f, "system error when calling {}", call),
70 Error::SystemError {
71 call,
72 raw_os_error: Some(errno),
73 } => write!(f, "system error when calling {}: {}", call, errno),
74 }
75 }
76}
77
78#[cfg(feature = "std")]
79impl std::error::Error for Error {}