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

@@ -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
}