bc_components/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum Error {
5    /// Invalid data size for the specified type.
6    #[error("invalid {data_type} size: expected {expected}, got {actual}")]
7    InvalidSize {
8        data_type: String,
9        expected: usize,
10        actual: usize,
11    },
12
13    /// Invalid data format or content.
14    #[error("invalid {data_type}: {reason}")]
15    InvalidData { data_type: String, reason: String },
16
17    // Data too short.
18    #[error(
19        "data too short: {data_type} expected at least {minimum}, got {actual}"
20    )]
21    DataTooShort {
22        data_type: String,
23        minimum: usize,
24        actual: usize,
25    },
26
27    /// Cryptographic operation failed.
28    #[error("cryptographic operation failed: {0}")]
29    Crypto(String),
30
31    /// CBOR encoding or decoding error.
32    #[error("CBOR error: {0}")]
33    Cbor(#[from] dcbor::Error),
34
35    /// SSKR error.
36    #[error("SSKR error: {0}")]
37    Sskr(#[from] sskr::Error),
38
39    /// SSH key operation failed.
40    #[error("SSH operation failed: {0}")]
41    Ssh(String),
42
43    /// URI parsing failed.
44    #[error("invalid URI: {0}")]
45    Uri(#[from] url::ParseError),
46
47    /// Data compression/decompression failed.
48    #[error("compression error: {0}")]
49    Compression(String),
50
51    /// Post-quantum cryptography library error.
52    #[error("post-quantum cryptography error: {0}")]
53    PostQuantum(String),
54
55    /// Signature level mismatch.
56    #[error("signature level does not match key level")]
57    LevelMismatch,
58
59    /// SSH agent operation failed.
60    #[error("SSH agent error: {0}")]
61    SshAgent(String),
62
63    /// Hex decoding error.
64    #[error("hex decoding error: {0}")]
65    Hex(#[from] hex::FromHexError),
66
67    /// UTF-8 conversion error.
68    #[error("UTF-8 conversion error: {0}")]
69    Utf8(#[from] std::string::FromUtf8Error),
70
71    /// Environment variable error.
72    #[error("environment variable error: {0}")]
73    Env(#[from] std::env::VarError),
74
75    /// SSH agent client error.
76    #[error("SSH agent client error: {0}")]
77    SshAgentClient(String),
78
79    /// General error with custom message.
80    #[error("{0}")]
81    General(String),
82}
83
84impl Error {
85    /// Create a general error with a custom message.
86    pub fn general(msg: impl Into<String>) -> Self {
87        Error::General(msg.into())
88    }
89
90    /// Create an invalid size error.
91    pub fn invalid_size(
92        data_type: impl Into<String>,
93        expected: usize,
94        actual: usize,
95    ) -> Self {
96        Error::InvalidSize { data_type: data_type.into(), expected, actual }
97    }
98
99    /// Create an invalid data error.
100    pub fn invalid_data(
101        data_type: impl Into<String>,
102        reason: impl Into<String>,
103    ) -> Self {
104        Error::InvalidData {
105            data_type: data_type.into(),
106            reason: reason.into(),
107        }
108    }
109
110    /// Create a data too short error.
111    pub fn data_too_short(
112        data_type: impl Into<String>,
113        minimum: usize,
114        actual: usize,
115    ) -> Self {
116        Error::DataTooShort { data_type: data_type.into(), minimum, actual }
117    }
118
119    /// Create a crypto error.
120    pub fn crypto(msg: impl Into<String>) -> Self {
121        Error::Crypto(msg.into())
122    }
123
124    /// Create an SSH error.
125    pub fn ssh(msg: impl Into<String>) -> Self {
126        Error::Ssh(msg.into())
127    }
128
129    /// Create a compression error.
130    pub fn compression(msg: impl Into<String>) -> Self {
131        Error::Compression(msg.into())
132    }
133
134    /// Create a post-quantum cryptography error.
135    pub fn post_quantum(msg: impl Into<String>) -> Self {
136        Error::PostQuantum(msg.into())
137    }
138
139    /// Create an SSH agent error.
140    pub fn ssh_agent(msg: impl Into<String>) -> Self {
141        Error::SshAgent(msg.into())
142    }
143
144    /// Create an SSH agent client error.
145    pub fn ssh_agent_client(msg: impl Into<String>) -> Self {
146        Error::SshAgentClient(msg.into())
147    }
148}
149
150// Convert our error to dcbor::Error for CBOR trait implementations
151impl From<Error> for dcbor::Error {
152    fn from(err: Error) -> Self {
153        match err {
154            Error::Cbor(cbor_err) => cbor_err,
155            _ => dcbor::Error::msg(err.to_string()),
156        }
157    }
158}
159
160// Convert SSH agent client errors
161impl From<ssh_agent_client_rs::Error> for Error {
162    fn from(err: ssh_agent_client_rs::Error) -> Self {
163        Error::ssh_agent_client(err.to_string())
164    }
165}
166
167// Convert SSH key errors
168impl From<ssh_key::Error> for Error {
169    fn from(err: ssh_key::Error) -> Self {
170        Error::ssh(err.to_string())
171    }
172}
173
174// Convert bc_crypto errors
175impl From<bc_crypto::Error> for Error {
176    fn from(err: bc_crypto::Error) -> Self {
177        Error::crypto(err.to_string())
178    }
179}
180
181pub type Result<T> = std::result::Result<T, Error>;