diff --git a/src/haystack/val/dict.rs b/src/haystack/val/dict.rs index 1f5f0a8..e27e729 100644 --- a/src/haystack/val/dict.rs +++ b/src/haystack/val/dict.rs @@ -385,16 +385,17 @@ where } if let Some(val) = dict.get("disMacro") { - return match val { - Value::Str(val) => dis_macro(&val.value, |val| dict.get(val), get_localized), - _ => decode_str_from_value(val), + return if let Value::Str(val) = val { + dis_macro(&val.value, |val| dict.get(val), get_localized) + } else { + decode_str_from_value(val) }; } if let Some(val) = dict.get("disKey") { - if let Value::Str(val) = val { - if let Some(val) = get_localized(&val.value) { - return val; + if let Value::Str(val_str) = val { + if let Some(val_str) = get_localized(&val_str.value) { + return val_str; } } return decode_str_from_value(val); @@ -413,9 +414,10 @@ where } if let Some(val) = dict.get("id") { - return match val { - Value::Ref(val) => Cow::Borrowed(val.dis.as_ref().unwrap_or(&val.value)), - _ => decode_str_from_value(val), + return if let Value::Ref(val) = val { + Cow::Borrowed(val.dis.as_ref().unwrap_or(&val.value)) + } else { + decode_str_from_value(val) }; } diff --git a/src/haystack/val/dis_macro.rs b/src/haystack/val/dis_macro.rs index 806aaeb..6e3ed07 100644 --- a/src/haystack/val/dis_macro.rs +++ b/src/haystack/val/dis_macro.rs @@ -3,7 +3,7 @@ use std::{borrow::Cow, sync::OnceLock}; use super::Value; -use regex::{Captures, Match, Regex, Replacer}; +use regex::{Captures, Regex, Replacer}; /// A regular expression replacer implementation for replacing formatted /// values in a display macro string. @@ -24,37 +24,31 @@ where GetLocalizedFunc: Fn(&str) -> Option>, { fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) { - let default_replace = |dst: &mut String| dst.push_str(caps.get(0).unwrap().as_str()); + let mut default_replace = || dst.push_str(caps.get(0).unwrap().as_str()); - let mut handle_value_capture = |cap_match: Match<'_>| { + // Replace $tag or ${tag} + if let Some(cap_match) = caps.get(2).or_else(|| caps.get(4)) { if let Some(value) = (self.get_value)(cap_match.as_str()) { if let Value::Ref(val) = value { dst.push_str(val.dis.as_ref().unwrap_or(&val.value)); } else if let Value::Str(val) = value { dst.push_str(&val.value); } else { - dst.push_str(&value.to_string()) + dst.push_str(&value.to_string()); } } else { - default_replace(dst); + default_replace(); } - }; - - // Replace $tag - if let Some(cap_match) = caps.get(2) { - handle_value_capture(cap_match); - // Replace ${tag} - } else if let Some(cap_match) = caps.get(4) { - handle_value_capture(cap_match); + } // Replace $ - } else if let Some(cap_match) = caps.get(6) { + else if let Some(cap_match) = caps.get(6) { if let Some(value) = (self.get_localized)(cap_match.as_str()) { dst.push_str(&value); } else { - default_replace(dst); + default_replace(); } } else { - default_replace(dst); + default_replace(); } } }