1use chrono::NaiveDate;
2pub use tokio_util::sync::CancellationToken;
3
4#[derive(Debug, Clone, PartialEq, Eq)]
18#[non_exhaustive]
19pub enum InitializationStatus {
20 Ready,
22 DirectoryMissing,
24 PathIsFile,
26 NoKeyringFiles,
28 NoTrustDb,
30 IncorrectPermissions { actual: u32 },
33}
34
35#[derive(Debug, Clone, PartialEq, Eq)]
37pub struct Key {
38 pub fingerprint: String,
39 pub uid: String,
40 pub created: Option<NaiveDate>,
41 pub expires: Option<NaiveDate>,
42 pub validity: KeyValidity,
43 pub key_type: KeyType,
44 pub usage: KeyUsage,
46 pub subkeys: Vec<Subkey>,
48}
49
50#[derive(Debug, Clone, PartialEq, Eq)]
52pub struct Subkey {
53 pub fingerprint: String,
54 pub key_type: KeyType,
55 pub created: Option<NaiveDate>,
56 pub expires: Option<NaiveDate>,
57 pub usage: KeyUsage,
58}
59
60#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
65pub struct KeyUsage {
66 pub sign: bool,
67 pub certify: bool,
68 pub encrypt: bool,
69 pub authenticate: bool,
70}
71
72#[derive(Debug, Clone, PartialEq, Eq)]
74pub struct KeyType {
75 pub algorithm: String,
76 pub bits: u32,
77}
78
79impl std::fmt::Display for KeyType {
80 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81 write!(f, "{}{}", self.algorithm.to_lowercase(), self.bits)
82 }
83}
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
93#[non_exhaustive]
94pub enum KeyValidity {
95 #[default]
97 Unknown,
98 Undefined,
100 Never,
102 Marginal,
104 Full,
106 Ultimate,
108 Expired,
110 Revoked,
112 Disabled,
114 Invalid,
116}
117
118impl KeyValidity {
119 pub fn from_gpg_char(c: char) -> Self {
120 match c {
121 'o' => Self::Unknown,
122 'q' => Self::Undefined,
123 'n' => Self::Never,
124 'm' => Self::Marginal,
125 'f' => Self::Full,
126 'u' => Self::Ultimate,
127 'e' => Self::Expired,
128 'r' => Self::Revoked,
129 'd' => Self::Disabled,
130 'i' => Self::Invalid,
131 _ => Self::Unknown,
132 }
133 }
134}
135
136#[derive(Debug, Clone, PartialEq, Eq)]
138pub struct Signature {
139 pub keyid: String,
140 pub uid: String,
141 pub created: Option<NaiveDate>,
142 pub expires: Option<NaiveDate>,
143 pub sig_class: String,
144 pub status: Option<SigStatus>,
147}
148
149#[derive(Debug, Clone, Copy, PartialEq, Eq)]
151pub enum SigStatus {
152 Good,
154 Bad,
156 MissingKey,
158 Error,
160}
161
162impl SigStatus {
163 pub fn from_gpg_marker(marker: &str) -> Option<Self> {
166 match marker {
167 "!" => Some(Self::Good),
168 "-" => Some(Self::Bad),
169 "?" => Some(Self::MissingKey),
170 "%" => Some(Self::Error),
171 _ => None,
172 }
173 }
174}
175
176#[derive(Debug, Clone, PartialEq, Eq)]
180pub struct VerifyResult {
181 pub good: bool,
183 pub trusted: bool,
186 pub key_fpr: Option<String>,
188 pub uid: Option<String>,
190}
191
192#[derive(Debug, Clone, Copy, PartialEq, Eq)]
196pub enum OwnerTrust {
197 Unknown,
199 Undefined,
201 Never,
203 Marginal,
205 Full,
207 Ultimate,
209}
210
211impl OwnerTrust {
212 pub fn from_gpg_level(level: &str) -> Self {
214 match level {
215 "2" => Self::Undefined,
216 "3" => Self::Never,
217 "4" => Self::Marginal,
218 "5" => Self::Full,
219 "6" => Self::Ultimate,
220 _ => Self::Unknown,
221 }
222 }
223
224 pub fn to_gpg_level(self) -> u8 {
226 match self {
227 Self::Unknown => 1,
228 Self::Undefined => 2,
229 Self::Never => 3,
230 Self::Marginal => 4,
231 Self::Full => 5,
232 Self::Ultimate => 6,
233 }
234 }
235}
236
237#[derive(Debug, Clone, PartialEq, Eq)]
239#[non_exhaustive]
240pub enum RefreshProgress {
241 Starting {
242 total_keys: usize,
243 },
244 Refreshing {
245 current: usize,
246 total: usize,
247 keyid: String,
248 },
249 Completed,
250 Error {
251 keyid: String,
252 message: String,
253 },
254}
255
256#[derive(Debug, Clone, Default)]
264pub struct OperationOptions {
265 pub timeout_secs: Option<u64>,
268 pub cancel_token: Option<CancellationToken>,
271}
272
273#[cfg(test)]
274mod tests {
275 use super::*;
276
277 #[test]
278 fn test_key_validity_from_gpg_char() {
279 assert_eq!(KeyValidity::from_gpg_char('o'), KeyValidity::Unknown);
280 assert_eq!(KeyValidity::from_gpg_char('q'), KeyValidity::Undefined);
281 assert_eq!(KeyValidity::from_gpg_char('n'), KeyValidity::Never);
282 assert_eq!(KeyValidity::from_gpg_char('m'), KeyValidity::Marginal);
283 assert_eq!(KeyValidity::from_gpg_char('f'), KeyValidity::Full);
284 assert_eq!(KeyValidity::from_gpg_char('u'), KeyValidity::Ultimate);
285 assert_eq!(KeyValidity::from_gpg_char('e'), KeyValidity::Expired);
286 assert_eq!(KeyValidity::from_gpg_char('r'), KeyValidity::Revoked);
287 assert_eq!(KeyValidity::from_gpg_char('d'), KeyValidity::Disabled);
288 assert_eq!(KeyValidity::from_gpg_char('i'), KeyValidity::Invalid);
289 assert_eq!(KeyValidity::from_gpg_char('x'), KeyValidity::Unknown);
290 assert_eq!(KeyValidity::from_gpg_char('-'), KeyValidity::Unknown);
291 }
292
293 #[test]
294 fn test_ownertrust_roundtrip() {
295 for t in [
296 OwnerTrust::Undefined,
297 OwnerTrust::Never,
298 OwnerTrust::Marginal,
299 OwnerTrust::Full,
300 OwnerTrust::Ultimate,
301 ] {
302 let level = t.to_gpg_level().to_string();
303 assert_eq!(OwnerTrust::from_gpg_level(&level), t);
304 }
305 assert_eq!(OwnerTrust::from_gpg_level(""), OwnerTrust::Unknown);
306 }
307
308 #[test]
309 fn test_sig_status_marker() {
310 assert_eq!(SigStatus::from_gpg_marker("!"), Some(SigStatus::Good));
311 assert_eq!(SigStatus::from_gpg_marker("-"), Some(SigStatus::Bad));
312 assert_eq!(SigStatus::from_gpg_marker("?"), Some(SigStatus::MissingKey));
313 assert_eq!(SigStatus::from_gpg_marker("%"), Some(SigStatus::Error));
314 assert_eq!(SigStatus::from_gpg_marker(""), None);
315 }
316}