feat: optimize code

This commit is contained in:
2023-10-28 17:52:03 +08:00
parent 67465d584e
commit 6161e2d293

View File

@@ -53,10 +53,10 @@ impl TinyEncryptConfig {
pub fn load(file: &str) -> XResult<Self> { pub fn load(file: &str) -> XResult<Self> {
let resolved_file = resolve_file_path(file); let resolved_file = resolve_file_path(file);
let config_contents = opt_result!( let config_contents = opt_result!(
fs::read_to_string(&resolved_file), "Read file: {}, failed: {}", file fs::read_to_string(&resolved_file), "Read config file: {}, failed: {}", file
); );
let mut config: TinyEncryptConfig = opt_result!( let mut config: TinyEncryptConfig = opt_result!(
serde_json::from_str(&config_contents),"Parse file: {}, failed: {}", file); serde_json::from_str(&config_contents),"Parse config file: {}, failed: {}", file);
let mut splited_profiles = HashMap::new(); let mut splited_profiles = HashMap::new();
for (k, v) in config.profiles.into_iter() { for (k, v) in config.profiles.into_iter() {
if !k.contains(',') { if !k.contains(',') {
@@ -103,7 +103,8 @@ impl TinyEncryptConfig {
}).collect() }).collect()
} }
pub fn find_envelops(&self, profile: &Option<String>, key_filter: &Option<String>) -> XResult<Vec<&TinyEncryptConfigEnvelop>> { pub fn find_envelops(&self, profile: &Option<String>, key_filter: &Option<String>)
-> XResult<Vec<&TinyEncryptConfigEnvelop>> {
debugging!("Profile: {:?}", profile); debugging!("Profile: {:?}", profile);
debugging!("Key filter: {:?}", key_filter); debugging!("Key filter: {:?}", key_filter);
let mut matched_envelops_map = HashMap::new(); let mut matched_envelops_map = HashMap::new();
@@ -123,7 +124,7 @@ impl TinyEncryptConfig {
}); });
} }
if key_ids.is_empty() { if key_ids.is_empty() {
return simple_error!("Profile or key filter cannot find valid envelopes"); return simple_error!("Profile or key filter cannot find any valid envelopes");
} }
for key_id in &key_ids { for key_id in &key_ids {
for envelop in self.find_by_kid_or_type(key_id) { for envelop in self.find_by_kid_or_type(key_id) {
@@ -133,7 +134,7 @@ impl TinyEncryptConfig {
let mut envelops: Vec<_> = matched_envelops_map.values().copied().collect(); let mut envelops: Vec<_> = matched_envelops_map.values().copied().collect();
if envelops.is_empty() { if envelops.is_empty() {
return simple_error!("Profile or key filter cannot find valid envelopes"); return simple_error!("Profile or key filter cannot find any valid envelopes");
} }
envelops.sort_by(|e1, e2| { envelops.sort_by(|e1, e2| {
if e1.r#type < e2.r#type { return Ordering::Greater; } if e1.r#type < e2.r#type { return Ordering::Greater; }