1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum Error {
5 #[error("invalid {data_type} size: expected {expected}, got {actual}")]
7 InvalidSize {
8 data_type: String,
9 expected: usize,
10 actual: usize,
11 },
12
13 #[error("invalid {data_type}: {reason}")]
15 InvalidData { data_type: String, reason: String },
16
17 #[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 #[error("cryptographic operation failed: {0}")]
29 Crypto(String),
30
31 #[error("CBOR error: {0}")]
33 Cbor(#[from] dcbor::Error),
34
35 #[error("SSKR error: {0}")]
37 Sskr(#[from] sskr::Error),
38
39 #[error("SSH operation failed: {0}")]
41 Ssh(String),
42
43 #[error("invalid URI: {0}")]
45 Uri(#[from] url::ParseError),
46
47 #[error("compression error: {0}")]
49 Compression(String),
50
51 #[error("post-quantum cryptography error: {0}")]
53 PostQuantum(String),
54
55 #[error("signature level does not match key level")]
57 LevelMismatch,
58
59 #[error("SSH agent error: {0}")]
61 SshAgent(String),
62
63 #[error("hex decoding error: {0}")]
65 Hex(#[from] hex::FromHexError),
66
67 #[error("UTF-8 conversion error: {0}")]
69 Utf8(#[from] std::string::FromUtf8Error),
70
71 #[error("environment variable error: {0}")]
73 Env(#[from] std::env::VarError),
74
75 #[error("SSH agent client error: {0}")]
77 SshAgentClient(String),
78
79 #[error("{0}")]
81 General(String),
82}
83
84impl Error {
85 pub fn general(msg: impl Into<String>) -> Self {
87 Error::General(msg.into())
88 }
89
90 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 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 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 pub fn crypto(msg: impl Into<String>) -> Self {
121 Error::Crypto(msg.into())
122 }
123
124 pub fn ssh(msg: impl Into<String>) -> Self {
126 Error::Ssh(msg.into())
127 }
128
129 pub fn compression(msg: impl Into<String>) -> Self {
131 Error::Compression(msg.into())
132 }
133
134 pub fn post_quantum(msg: impl Into<String>) -> Self {
136 Error::PostQuantum(msg.into())
137 }
138
139 pub fn ssh_agent(msg: impl Into<String>) -> Self {
141 Error::SshAgent(msg.into())
142 }
143
144 pub fn ssh_agent_client(msg: impl Into<String>) -> Self {
146 Error::SshAgentClient(msg.into())
147 }
148}
149
150impl 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
160impl 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
167impl From<ssh_key::Error> for Error {
169 fn from(err: ssh_key::Error) -> Self {
170 Error::ssh(err.to_string())
171 }
172}
173
174impl 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>;