| 1 |
//! Certificate and key import stats. |
| 2 |
|
| 3 |
use std::{ |
| 4 |
fmt, |
| 5 |
sync::Arc, |
| 6 |
}; |
| 7 |
|
| 8 |
use anyhow::Result; |
| 9 |
|
| 10 |
use sequoia_cert_store::{ |
| 11 |
LazyCert, |
| 12 |
store::{ |
| 13 |
MergeCerts, |
| 14 |
MergePublicCollectStats, |
| 15 |
}, |
| 16 |
}; |
| 17 |
|
| 18 |
use sequoia_keystore as keystore; |
| 19 |
|
| 20 |
use crate::output::pluralize::Pluralize; |
| 21 |
|
| 22 |
/// Certificate and key import stats. |
| 23 |
#[derive(Debug)] |
| 24 |
pub struct ImportStats { |
| 25 |
pub certs: MergePublicCollectStats, |
| 26 |
pub keys: KeyStats, |
| 27 |
} |
| 28 |
|
| 29 |
impl Default for ImportStats { |
| 30 |
fn default() -> Self { |
| 31 |
ImportStats { |
| 32 |
certs: MergePublicCollectStats::new(), |
| 33 |
keys: Default::default(), |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
impl std::ops::AddAssign for ImportStats { |
| 39 |
fn add_assign(&mut self, other: Self) { |
| 40 |
// XXX: Not ideal. |
| 41 |
(0..other.certs.new_certs()) |
| 42 |
.for_each(|_| self.certs.inc_new_certs()); |
| 43 |
(0..other.certs.unchanged_certs()) |
| 44 |
.for_each(|_| self.certs.inc_unchanged_certs()); |
| 45 |
(0..other.certs.updated_certs()) |
| 46 |
.for_each(|_| self.certs.inc_updated_certs()); |
| 47 |
(0..other.certs.errors()) |
| 48 |
.for_each(|_| self.certs.inc_errors()); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
impl<'a> MergeCerts<'a> for ImportStats { |
| 53 |
fn merge_public<'b>( |
| 54 |
&self, |
| 55 |
new: Arc<LazyCert<'a>>, |
| 56 |
disk: Option<Arc<LazyCert<'b>>>, |
| 57 |
) -> Result<Arc<LazyCert<'a>>> { |
| 58 |
self.certs.merge_public(new, disk) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
impl ImportStats { |
| 63 |
/// Print key and certificate import summary. |
| 64 |
pub fn print_summary(&self, o: &mut dyn std::io::Write, sq: &crate::Sq) |
| 65 |
-> Result<()> |
| 66 |
{ |
| 67 |
if sq.quiet() { |
| 68 |
return Ok(()); |
| 69 |
} |
| 70 |
|
| 71 |
if ! self.keys.is_empty() { |
| 72 |
self.keys.print_summary(o)?; |
| 73 |
} |
| 74 |
|
| 75 |
wwriteln!(o, |
| 76 |
"Imported {}, updated {}, {} unchanged, {}.", |
| 77 |
self.certs.new_certs().of("new certificate"), |
| 78 |
self.certs.updated_certs().of("certificate"), |
| 79 |
self.certs.unchanged_certs().of("certificate"), |
| 80 |
self.certs.errors().of("error")); |
| 81 |
Ok(()) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/// Key import stats. |
| 86 |
#[derive(Debug, Default)] |
| 87 |
pub struct KeyStats { |
| 88 |
/// Number of new keys. |
| 89 |
pub new: usize, |
| 90 |
|
| 91 |
/// Number of unchanged keys. |
| 92 |
pub unchanged: usize, |
| 93 |
|
| 94 |
/// Number of updated keys. |
| 95 |
pub updated: usize, |
| 96 |
|
| 97 |
/// Number of errors. |
| 98 |
pub errors: usize, |
| 99 |
} |
| 100 |
|
| 101 |
impl std::ops::AddAssign for KeyStats { |
| 102 |
fn add_assign(&mut self, other: Self) { |
| 103 |
self.new += other.new; |
| 104 |
self.unchanged += other.unchanged; |
| 105 |
self.updated += other.updated; |
| 106 |
self.errors += other.errors; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
impl KeyStats { |
| 111 |
/// Returns whether there haven't been any key imports at all. |
| 112 |
pub fn is_empty(&self) -> bool { |
| 113 |
self.new == 0 |
| 114 |
&& self.unchanged == 0 |
| 115 |
&& self.updated == 0 |
| 116 |
&& self.errors == 0 |
| 117 |
} |
| 118 |
|
| 119 |
/// Print key and certificate import summary. |
| 120 |
pub fn print_summary(&self, o: &mut dyn std::io::Write) -> Result<()> { |
| 121 |
wwriteln!(o, |
| 122 |
"Imported {}, updated {}, {} unchanged, {}.", |
| 123 |
self.new.of("new key"), |
| 124 |
self.updated.of("key"), |
| 125 |
self.unchanged.of("key"), |
| 126 |
self.errors.of("error")); |
| 127 |
Ok(()) |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/// Whether a cert or key was freshly imported, updated, or unchanged. |
| 132 |
/// |
| 133 |
/// Returned by [`Sq::import_key`]. |
| 134 |
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 135 |
pub enum ImportStatus { |
| 136 |
/// The certificate or key is unchanged. |
| 137 |
Unchanged, |
| 138 |
|
| 139 |
/// The certificate or key is new. |
| 140 |
New, |
| 141 |
|
| 142 |
/// The certificate or key has been updated. |
| 143 |
Updated, |
| 144 |
} |
| 145 |
|
| 146 |
impl From<keystore::ImportStatus> for ImportStatus { |
| 147 |
fn from(status: keystore::ImportStatus) -> ImportStatus { |
| 148 |
match status { |
| 149 |
keystore::ImportStatus::Unchanged => ImportStatus::Unchanged, |
| 150 |
keystore::ImportStatus::New => ImportStatus::New, |
| 151 |
keystore::ImportStatus::Updated => ImportStatus::Updated, |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
impl fmt::Display for ImportStatus { |
| 157 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 158 |
match self { |
| 159 |
ImportStatus::Unchanged => f.write_str("unchanged"), |
| 160 |
ImportStatus::New => f.write_str("new"), |
| 161 |
ImportStatus::Updated => f.write_str("updated"), |
| 162 |
} |
| 163 |
} |
| 164 |
} |