Skip to content

Commit

Permalink
PR updates
Browse files Browse the repository at this point in the history
  • Loading branch information
garethj2 committed Feb 20, 2024
1 parent bc3dbcb commit dba1e29
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
20 changes: 11 additions & 9 deletions src/haystack/val/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
};
}

Expand Down
26 changes: 10 additions & 16 deletions src/haystack/val/dis_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -24,37 +24,31 @@ where
GetLocalizedFunc: Fn(&str) -> Option<Cow<'a, str>>,
{
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 $<pod::key>
} 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();
}
}
}
Expand Down

0 comments on commit dba1e29

Please sign in to comment.