Skip to content

Commit dba1e29

Browse files
committed
PR updates
1 parent bc3dbcb commit dba1e29

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

src/haystack/val/dict.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -385,16 +385,17 @@ where
385385
}
386386

387387
if let Some(val) = dict.get("disMacro") {
388-
return match val {
389-
Value::Str(val) => dis_macro(&val.value, |val| dict.get(val), get_localized),
390-
_ => decode_str_from_value(val),
388+
return if let Value::Str(val) = val {
389+
dis_macro(&val.value, |val| dict.get(val), get_localized)
390+
} else {
391+
decode_str_from_value(val)
391392
};
392393
}
393394

394395
if let Some(val) = dict.get("disKey") {
395-
if let Value::Str(val) = val {
396-
if let Some(val) = get_localized(&val.value) {
397-
return val;
396+
if let Value::Str(val_str) = val {
397+
if let Some(val_str) = get_localized(&val_str.value) {
398+
return val_str;
398399
}
399400
}
400401
return decode_str_from_value(val);
@@ -413,9 +414,10 @@ where
413414
}
414415

415416
if let Some(val) = dict.get("id") {
416-
return match val {
417-
Value::Ref(val) => Cow::Borrowed(val.dis.as_ref().unwrap_or(&val.value)),
418-
_ => decode_str_from_value(val),
417+
return if let Value::Ref(val) = val {
418+
Cow::Borrowed(val.dis.as_ref().unwrap_or(&val.value))
419+
} else {
420+
decode_str_from_value(val)
419421
};
420422
}
421423

src/haystack/val/dis_macro.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::{borrow::Cow, sync::OnceLock};
44

55
use super::Value;
6-
use regex::{Captures, Match, Regex, Replacer};
6+
use regex::{Captures, Regex, Replacer};
77

88
/// A regular expression replacer implementation for replacing formatted
99
/// values in a display macro string.
@@ -24,37 +24,31 @@ where
2424
GetLocalizedFunc: Fn(&str) -> Option<Cow<'a, str>>,
2525
{
2626
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
27-
let default_replace = |dst: &mut String| dst.push_str(caps.get(0).unwrap().as_str());
27+
let mut default_replace = || dst.push_str(caps.get(0).unwrap().as_str());
2828

29-
let mut handle_value_capture = |cap_match: Match<'_>| {
29+
// Replace $tag or ${tag}
30+
if let Some(cap_match) = caps.get(2).or_else(|| caps.get(4)) {
3031
if let Some(value) = (self.get_value)(cap_match.as_str()) {
3132
if let Value::Ref(val) = value {
3233
dst.push_str(val.dis.as_ref().unwrap_or(&val.value));
3334
} else if let Value::Str(val) = value {
3435
dst.push_str(&val.value);
3536
} else {
36-
dst.push_str(&value.to_string())
37+
dst.push_str(&value.to_string());
3738
}
3839
} else {
39-
default_replace(dst);
40+
default_replace();
4041
}
41-
};
42-
43-
// Replace $tag
44-
if let Some(cap_match) = caps.get(2) {
45-
handle_value_capture(cap_match);
46-
// Replace ${tag}
47-
} else if let Some(cap_match) = caps.get(4) {
48-
handle_value_capture(cap_match);
42+
}
4943
// Replace $<pod::key>
50-
} else if let Some(cap_match) = caps.get(6) {
44+
else if let Some(cap_match) = caps.get(6) {
5145
if let Some(value) = (self.get_localized)(cap_match.as_str()) {
5246
dst.push_str(&value);
5347
} else {
54-
default_replace(dst);
48+
default_replace();
5549
}
5650
} else {
57-
default_replace(dst);
51+
default_replace();
5852
}
5953
}
6054
}

0 commit comments

Comments
 (0)