use chrono::NaiveDate;
pub use tokio_util::sync::CancellationToken;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum InitializationStatus {
Ready,
DirectoryMissing,
PathIsFile,
NoKeyringFiles,
NoTrustDb,
IncorrectPermissions { actual: u32 },
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Key {
pub fingerprint: String,
pub uid: String,
pub created: Option<NaiveDate>,
pub expires: Option<NaiveDate>,
pub validity: KeyValidity,
pub key_type: KeyType,
pub usage: KeyUsage,
pub subkeys: Vec<Subkey>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Subkey {
pub fingerprint: String,
pub key_type: KeyType,
pub created: Option<NaiveDate>,
pub expires: Option<NaiveDate>,
pub usage: KeyUsage,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct KeyUsage {
pub sign: bool,
pub certify: bool,
pub encrypt: bool,
pub authenticate: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyType {
pub algorithm: String,
pub bits: u32,
}
impl std::fmt::Display for KeyType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.algorithm.to_lowercase(), self.bits)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum KeyValidity {
#[default]
Unknown,
Undefined,
Never,
Marginal,
Full,
Ultimate,
Expired,
Revoked,
Disabled,
Invalid,
}
impl KeyValidity {
pub fn from_gpg_char(c: char) -> Self {
match c {
'o' => Self::Unknown,
'q' => Self::Undefined,
'n' => Self::Never,
'm' => Self::Marginal,
'f' => Self::Full,
'u' => Self::Ultimate,
'e' => Self::Expired,
'r' => Self::Revoked,
'd' => Self::Disabled,
'i' => Self::Invalid,
_ => Self::Unknown,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Signature {
pub keyid: String,
pub uid: String,
pub created: Option<NaiveDate>,
pub expires: Option<NaiveDate>,
pub sig_class: String,
pub status: Option<SigStatus>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SigStatus {
Good,
Bad,
MissingKey,
Error,
}
impl SigStatus {
pub fn from_gpg_marker(marker: &str) -> Option<Self> {
match marker {
"!" => Some(Self::Good),
"-" => Some(Self::Bad),
"?" => Some(Self::MissingKey),
"%" => Some(Self::Error),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VerifyResult {
pub good: bool,
pub trusted: bool,
pub key_fpr: Option<String>,
pub uid: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OwnerTrust {
Unknown,
Undefined,
Never,
Marginal,
Full,
Ultimate,
}
impl OwnerTrust {
pub fn from_gpg_level(level: &str) -> Self {
match level {
"2" => Self::Undefined,
"3" => Self::Never,
"4" => Self::Marginal,
"5" => Self::Full,
"6" => Self::Ultimate,
_ => Self::Unknown,
}
}
pub fn to_gpg_level(self) -> u8 {
match self {
Self::Unknown => 1,
Self::Undefined => 2,
Self::Never => 3,
Self::Marginal => 4,
Self::Full => 5,
Self::Ultimate => 6,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum RefreshProgress {
Starting {
total_keys: usize,
},
Refreshing {
current: usize,
total: usize,
keyid: String,
},
Completed,
Error {
keyid: String,
message: String,
},
}
#[derive(Debug, Clone, Default)]
pub struct OperationOptions {
pub timeout_secs: Option<u64>,
pub cancel_token: Option<CancellationToken>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_key_validity_from_gpg_char() {
assert_eq!(KeyValidity::from_gpg_char('o'), KeyValidity::Unknown);
assert_eq!(KeyValidity::from_gpg_char('q'), KeyValidity::Undefined);
assert_eq!(KeyValidity::from_gpg_char('n'), KeyValidity::Never);
assert_eq!(KeyValidity::from_gpg_char('m'), KeyValidity::Marginal);
assert_eq!(KeyValidity::from_gpg_char('f'), KeyValidity::Full);
assert_eq!(KeyValidity::from_gpg_char('u'), KeyValidity::Ultimate);
assert_eq!(KeyValidity::from_gpg_char('e'), KeyValidity::Expired);
assert_eq!(KeyValidity::from_gpg_char('r'), KeyValidity::Revoked);
assert_eq!(KeyValidity::from_gpg_char('d'), KeyValidity::Disabled);
assert_eq!(KeyValidity::from_gpg_char('i'), KeyValidity::Invalid);
assert_eq!(KeyValidity::from_gpg_char('x'), KeyValidity::Unknown);
assert_eq!(KeyValidity::from_gpg_char('-'), KeyValidity::Unknown);
}
#[test]
fn test_ownertrust_roundtrip() {
for t in [
OwnerTrust::Undefined,
OwnerTrust::Never,
OwnerTrust::Marginal,
OwnerTrust::Full,
OwnerTrust::Ultimate,
] {
let level = t.to_gpg_level().to_string();
assert_eq!(OwnerTrust::from_gpg_level(&level), t);
}
assert_eq!(OwnerTrust::from_gpg_level(""), OwnerTrust::Unknown);
}
#[test]
fn test_sig_status_marker() {
assert_eq!(SigStatus::from_gpg_marker("!"), Some(SigStatus::Good));
assert_eq!(SigStatus::from_gpg_marker("-"), Some(SigStatus::Bad));
assert_eq!(SigStatus::from_gpg_marker("?"), Some(SigStatus::MissingKey));
assert_eq!(SigStatus::from_gpg_marker("%"), Some(SigStatus::Error));
assert_eq!(SigStatus::from_gpg_marker(""), None);
}
}