Skip to content

Commit 6816683

Browse files
committed
Tweak conditionals
1 parent eb1b7dc commit 6816683

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

src/haystack/val/dict.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,10 +436,9 @@ mod test {
436436
use crate::val::{dict_to_dis, Dict, HaystackDict, Value};
437437

438438
fn get_localized<'a>(key: &str) -> Option<Cow<'a, str>> {
439-
if key == "key" {
440-
Some(Cow::Borrowed("translated"))
441-
} else {
442-
None
439+
match key {
440+
"key" => Some(Cow::Borrowed("translated")),
441+
_ => None,
443442
}
444443
}
445444

src/haystack/val/dis_macro.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ 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());
28+
2729
let mut handle_value_capture = |cap_match: Match<'_>| {
2830
if let Some(value) = (self.get_value)(cap_match.as_str()) {
2931
if let Value::Ref(val) = value {
@@ -34,7 +36,7 @@ where
3436
dst.push_str(&value.to_string())
3537
}
3638
} else {
37-
dst.push_str(caps.get(0).unwrap().as_str());
39+
default_replace(dst);
3840
}
3941
};
4042

@@ -49,10 +51,10 @@ where
4951
if let Some(value) = (self.get_localized)(cap_match.as_str()) {
5052
dst.push_str(&value);
5153
} else {
52-
dst.push_str(caps.get(0).unwrap().as_str());
54+
default_replace(dst);
5355
}
5456
} else {
55-
dst.push_str(caps.get(0).unwrap().as_str());
57+
default_replace(dst);
5658
}
5759
}
5860
}
@@ -80,18 +82,19 @@ where
8082
// Cache the regular expression in memory so it doesn't need to compile on each invocation.
8183
static REG_EX: OnceLock<Regex> = OnceLock::new();
8284

83-
let regex = REG_EX.get_or_init(|| {
84-
// Replace $tags, ${tag} or $<pod::key>
85-
Regex::new(r"(\$([a-z][a-zA-Z0-9_]+))|(\$\{([a-z][a-zA-Z0-9_]+)\})|(\$<([^>]+)>)").unwrap()
86-
});
87-
88-
regex.replace_all(
89-
pattern,
90-
DisReplacer {
91-
get_value: &get_value,
92-
get_localized: &get_localized,
93-
},
94-
)
85+
REG_EX
86+
.get_or_init(|| {
87+
// Replace $tags, ${tag} or $<pod::key>
88+
Regex::new(r"(\$([a-z][a-zA-Z0-9_]+))|(\$\{([a-z][a-zA-Z0-9_]+)\})|(\$<([^>]+)>)")
89+
.unwrap()
90+
})
91+
.replace_all(
92+
pattern,
93+
DisReplacer {
94+
get_value: &get_value,
95+
get_localized: &get_localized,
96+
},
97+
)
9598
}
9699

97100
#[cfg(test)]
@@ -120,10 +123,9 @@ mod test {
120123
}
121124

122125
fn i18n_cb<'a>(name: &str) -> Option<Cow<'a, str>> {
123-
if name == "pod::hello" {
124-
Some(Cow::Borrowed("world"))
125-
} else {
126-
None
126+
match name {
127+
"pod::hello" => Some(Cow::Borrowed("world")),
128+
_ => None,
127129
}
128130
}
129131

0 commit comments

Comments
 (0)