1use std::fmt;
5
6use crate::crypto::aes;
7
8pub type Result<T, E = Error> = std::result::Result<T, E>;
9
10#[derive(Debug, Eq, PartialEq)]
11pub enum Error {
12 IO(String),
14 Aes(aes::Error),
16 Secp256k1(bitcoin::secp256k1::Error),
18 BIP32(bitcoin::util::bip32::Error),
20 BIP39(bdk::keys::bip39::Error),
22 Base58(bitcoin::util::base58::Error),
24 Base64(base64::DecodeError),
26 JSON(String),
28 BDK(String),
30 Parse(String),
32 Generic(String),
34 #[cfg(feature = "nostr")]
36 Bech32(bitcoin::bech32::Error),
37}
38
39impl fmt::Display for Error {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 match self {
42 Self::IO(err) => write!(f, "{}", err),
43 Self::Aes(err) => write!(f, "{}", err),
44 Self::Secp256k1(err) => write!(f, "{}", err),
45 Self::BIP32(err) => write!(f, "{}", err),
46 Self::BIP39(err) => write!(f, "{}", err),
47 Self::Base58(err) => write!(f, "{}", err),
48 Self::Base64(err) => write!(f, "{}", err),
49 Self::JSON(err) => write!(f, "{}", err),
50 Self::BDK(err) => write!(f, "{}", err),
51 Self::Parse(err) => write!(f, "{}", err),
52 Self::Generic(err) => write!(f, "{}", err),
53 #[cfg(feature = "nostr")]
54 Self::Bech32(err) => write!(f, "{}", err),
55 }
56 }
57}
58
59impl std::error::Error for Error {}
60
61impl From<std::io::Error> for Error {
62 fn from(err: std::io::Error) -> Self {
63 Self::IO(err.to_string())
64 }
65}
66
67impl From<aes::Error> for Error {
68 fn from(err: aes::Error) -> Self {
69 Self::Aes(err)
70 }
71}
72
73impl From<bitcoin::secp256k1::Error> for Error {
74 fn from(err: bitcoin::secp256k1::Error) -> Self {
75 Self::Secp256k1(err)
76 }
77}
78
79impl From<bitcoin::util::bip32::Error> for Error {
80 fn from(err: bitcoin::util::bip32::Error) -> Self {
81 Self::BIP32(err)
82 }
83}
84
85impl From<bdk::keys::bip39::Error> for Error {
86 fn from(err: bdk::keys::bip39::Error) -> Self {
87 Self::BIP39(err)
88 }
89}
90
91impl From<bitcoin::util::base58::Error> for Error {
92 fn from(err: bitcoin::util::base58::Error) -> Self {
93 Self::Base58(err)
94 }
95}
96
97impl From<base64::DecodeError> for Error {
98 fn from(err: base64::DecodeError) -> Self {
99 Self::Base64(err)
100 }
101}
102
103impl From<serde_json::Error> for Error {
104 fn from(err: serde_json::Error) -> Self {
105 Self::JSON(err.to_string())
106 }
107}
108
109impl From<bdk::Error> for Error {
110 fn from(err: bdk::Error) -> Self {
111 Self::BDK(err.to_string())
112 }
113}
114
115#[cfg(feature = "nostr")]
116impl From<bitcoin::bech32::Error> for Error {
117 fn from(err: bitcoin::bech32::Error) -> Self {
118 Self::Bech32(err)
119 }
120}