| 1 |
//! Collection of functions to provide a unified user experience. |
| 2 |
|
| 3 |
use anyhow::Result; |
| 4 |
|
| 5 |
use sequoia_openpgp::{ |
| 6 |
self as openpgp, |
| 7 |
KeyHandle, |
| 8 |
packet::UserID, |
| 9 |
}; |
| 10 |
|
| 11 |
use crate::{ |
| 12 |
Sq, |
| 13 |
}; |
| 14 |
|
| 15 |
pub use crate::output::sanitize::Safe; |
| 16 |
|
| 17 |
/// Emits a certificate header. |
| 18 |
pub fn emit_cert(o: &mut dyn std::io::Write, sq: &Sq, cert: &openpgp::Cert) |
| 19 |
-> Result<()> |
| 20 |
{ |
| 21 |
emit_cert_userid_str( |
| 22 |
o, cert, &sq.best_userid(cert, true).display().to_string()) |
| 23 |
} |
| 24 |
|
| 25 |
/// Emits a certificate header. |
| 26 |
pub fn emit_cert_indent(o: &mut dyn std::io::Write, |
| 27 |
indent: &str, |
| 28 |
sq: &Sq, cert: &openpgp::Cert) |
| 29 |
-> Result<()> |
| 30 |
{ |
| 31 |
emit_cert_userid_str_indent( |
| 32 |
o, indent, cert, &sq.best_userid(cert, true).display().to_string()) |
| 33 |
} |
| 34 |
|
| 35 |
/// Emits a certificate header. |
| 36 |
pub fn emit_cert_userid(o: &mut dyn std::io::Write, |
| 37 |
cert: &openpgp::Cert, |
| 38 |
userid: &UserID) |
| 39 |
-> Result<()> |
| 40 |
{ |
| 41 |
emit_cert_userid_str(o, cert, &String::from_utf8_lossy(userid.value())) |
| 42 |
} |
| 43 |
|
| 44 |
/// Emits a certificate header. |
| 45 |
pub fn emit_cert_userid_indent(o: &mut dyn std::io::Write, |
| 46 |
indent: &str, |
| 47 |
cert: &openpgp::Cert, |
| 48 |
userid: &UserID) |
| 49 |
-> Result<()> |
| 50 |
{ |
| 51 |
emit_cert_userid_str_indent( |
| 52 |
o, indent, cert, &String::from_utf8_lossy(userid.value())) |
| 53 |
} |
| 54 |
|
| 55 |
/// Emits a certificate header. |
| 56 |
pub fn emit_cert_userid_str(o: &mut dyn std::io::Write, |
| 57 |
cert: &openpgp::Cert, |
| 58 |
userid: &str) |
| 59 |
-> Result<()> |
| 60 |
{ |
| 61 |
emit_cert_key_handle_userid_str(o, &cert.key_handle(), userid) |
| 62 |
} |
| 63 |
|
| 64 |
/// Emits a certificate header. |
| 65 |
pub fn emit_cert_userid_str_indent(o: &mut dyn std::io::Write, |
| 66 |
indent: &str, |
| 67 |
cert: &openpgp::Cert, |
| 68 |
userid: &str) |
| 69 |
-> Result<()> |
| 70 |
{ |
| 71 |
emit_cert_key_handle_userid_str_indent( |
| 72 |
o, indent, &cert.key_handle(), userid) |
| 73 |
} |
| 74 |
|
| 75 |
/// Emits a certificate header. |
| 76 |
pub fn emit_cert_key_handle_userid_str(o: &mut dyn std::io::Write, |
| 77 |
kh: &KeyHandle, |
| 78 |
userid: &str) |
| 79 |
-> Result<()> |
| 80 |
{ |
| 81 |
emit_cert_key_handle_userid_str_indent(o, "", kh, userid) |
| 82 |
} |
| 83 |
|
| 84 |
/// Emits a certificate header. |
| 85 |
pub fn emit_cert_key_handle_userid_str_indent(o: &mut dyn std::io::Write, |
| 86 |
indent: &str, |
| 87 |
kh: &KeyHandle, |
| 88 |
userid: &str) |
| 89 |
-> Result<()> |
| 90 |
{ |
| 91 |
wwriteln!(stream = o, |
| 92 |
initial_indent = format!("{} - ┌ ", indent), |
| 93 |
subsequent_indent = format!("{} │ ", indent), |
| 94 |
"{}", kh); |
| 95 |
wwriteln!(stream = o, |
| 96 |
initial_indent = format!("{} └ ", indent), |
| 97 |
"{}", Safe(userid)); |
| 98 |
Ok(()) |
| 99 |
} |