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 { Error::Crypto(msg.into()) }
121
122    /// Create an SSH error.
123    pub fn ssh(msg: impl Into<String>) -> Self { Error::Ssh(msg.into()) }
124
125    /// Create a compression error.
126    pub fn compression(msg: impl Into<String>) -> Self {
127        Error::Compression(msg.into())
128    }
129
130    /// Create a post-quantum cryptography error.
131    pub fn post_quantum(msg: impl Into<String>) -> Self {
132        Error::PostQuantum(msg.into())
133    }
134
135    /// Create an SSH agent error.
136    pub fn ssh_agent(msg: impl Into<String>) -> Self {
137        Error::SshAgent(msg.into())
138    }
139
140    /// Create an SSH agent client error.
141    pub fn ssh_agent_client(msg: impl Into<String>) -> Self {
142        Error::SshAgentClient(msg.into())
143    }
144}
145
146// Convert our error to dcbor::Error for CBOR trait implementations
147impl From<Error> for dcbor::Error {
148    fn from(err: Error) -> Self {
149        match err {
150            Error::Cbor(cbor_err) => cbor_err,
151            _ => dcbor::Error::msg(err.to_string()),
152        }
153    }
154}
155
156// Convert SSH agent client errors
157impl From<ssh_agent_client_rs::Error> for Error {
158    fn from(err: ssh_agent_client_rs::Error) -> Self {
159        Error::ssh_agent_client(err.to_string())
160    }
161}
162
163// Convert SSH key errors
164impl From<ssh_key::Error> for Error {
165    fn from(err: ssh_key::Error) -> Self { Error::ssh(err.to_string()) }
166}
167
168// Convert bc_crypto errors
169impl From<bc_crypto::Error> for Error {
170    fn from(err: bc_crypto::Error) -> Self { Error::crypto(err.to_string()) }
171}
172
173pub type Result<T> = std::result::Result<T, Error>;