keechain_core/
error.rs

1// Copyright (c) 2022-2023 Yuki Kishimoto
2// Distributed under the MIT software license
3
4use 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    /// I/O error
13    IO(String),
14    /// AES error
15    Aes(aes::Error),
16    /// ECDSA error
17    Secp256k1(bitcoin::secp256k1::Error),
18    /// BIP32 error
19    BIP32(bitcoin::util::bip32::Error),
20    /// BIP39 error
21    BIP39(bdk::keys::bip39::Error),
22    /// Base58 error
23    Base58(bitcoin::util::base58::Error),
24    /// Base64 decode error
25    Base64(base64::DecodeError),
26    /// JSON error
27    JSON(String),
28    /// BDK error
29    BDK(String),
30    /// Parse error
31    Parse(String),
32    /// Generic error
33    Generic(String),
34    /// Bech32 error
35    #[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}