feat: display sid or kid

This commit is contained in:
2023-10-15 14:16:14 +08:00
parent b0af535aa3
commit 93b957a4c3
3 changed files with 14 additions and 13 deletions

View File

@@ -33,7 +33,7 @@ pub struct CmdEncrypt {
/// Encryption profile (use default when --key-filter is assigned)
#[arg(long, short = 'p')]
pub profile: Option<String>,
/// Encryption key filter (key_id or type:TYPE(e.g. ecdh, pgp, ecdh-p384, pgp-ed25519), multiple joined by ',')
/// Encryption key filter (key_id or type:TYPE(e.g. ecdh, pgp, ecdh-p384, pgp-ed25519), multiple joined by ',', ALL for all)
#[arg(long, short = 'k')]
pub key_filter: Option<String>,
/// Compress before encrypt

View File

@@ -92,7 +92,7 @@ impl TinyEncryptConfig {
pub fn find_by_kid_or_type(&self, k_filter: &str) -> Vec<&TinyEncryptConfigEnvelop> {
self.find_by_kid_or_filter(k_filter, |e| {
k_filter == &format!("type:{}", &e.r#type.get_name())
k_filter == "ALL" || k_filter == &format!("type:{}", &e.r#type.get_name())
})
}

View File

@@ -1,25 +1,26 @@
use rust_util::iff;
use crate::config::TinyEncryptConfig;
use crate::config::{TinyEncryptConfig, TinyEncryptConfigEnvelop};
use crate::spec::TinyEncryptEnvelop;
pub fn format_envelop(envelop: &TinyEncryptEnvelop, config: &Option<TinyEncryptConfig>) -> String {
let kid = iff!(envelop.kid.is_empty(), "".into(), format!(", Kid: {}", envelop.kid));
let envelop_desc = get_envelop_desc(&kid, envelop, &config);
let config_envelop = config.as_ref().and_then(|c| c.find_by_kid(&envelop.kid));
let envelop_kid = config_envelop.and_then(|e| e.sid.as_ref()).map(|sid|
format!(", Sid: {}", sid)
).unwrap_or_else(|| {
format!(", Kid: {}", envelop.kid)
});
let envelop_desc = get_envelop_desc(envelop, &config_envelop);
let desc = envelop_desc.as_ref()
.map(|desc| format!(", Desc: {}", desc))
.unwrap_or_else(|| "".to_string());
format!("{}{}{}", envelop.r#type.get_upper_name(), kid, desc)
format!("{}{}{}", envelop.r#type.get_upper_name(), envelop_kid, desc)
}
pub fn get_envelop_desc(kid: &str, envelop: &TinyEncryptEnvelop, config: &Option<TinyEncryptConfig>) -> Option<String> {
fn get_envelop_desc(envelop: &TinyEncryptEnvelop, config_envelop: &Option<&TinyEncryptConfigEnvelop>) -> Option<String> {
if let Some(desc) = &envelop.desc {
return Some(desc.to_string());
}
if let Some(config) = config {
if let Some(config_envelop) = config.find_by_kid(kid) {
return config_envelop.desc.clone();
}
if let Some(config_envelop) = config_envelop {
return config_envelop.desc.clone();
}
None
}