summaryrefslogtreecommitdiffstats
path: root/src/launcher/strsub.rs
diff options
context:
space:
mode:
authorLibravatar bigfoot547 <[email protected]>2025-01-12 03:58:01 -0600
committerLibravatar bigfoot547 <[email protected]>2025-01-12 03:58:01 -0600
commitc0986823af246ccee2247b881974a2b7ce6ee491 (patch)
tree9dcb8a66692d5c0067450c60e1de72bd4fce92a5 /src/launcher/strsub.rs
parentuse a macro for map_err (diff)
add some logging and stuff
Diffstat (limited to 'src/launcher/strsub.rs')
-rw-r--r--src/launcher/strsub.rs28
1 files changed, 20 insertions, 8 deletions
diff --git a/src/launcher/strsub.rs b/src/launcher/strsub.rs
index cdf395b..e01ef80 100644
--- a/src/launcher/strsub.rs
+++ b/src/launcher/strsub.rs
@@ -12,23 +12,35 @@ fn prev_char(slice: &str, idx: usize) -> Option<(usize, char)> {
slice[..idx].char_indices().rev().next()
}
-// basically the same thing as replace_string, but it creates the String itself and returns it.
-pub fn replace_str<'rep, T>(input: &str, sub: T) -> String
+pub trait SubFunc<'rep>: Fn(&str) -> Option<Cow<'rep, str>> {
+ fn substitute(&self, key: &str) -> Option<Cow<'rep, str>>;
+}
+
+impl<'rep, F> SubFunc<'rep> for F
where
- T: Fn(/*key: */ &str) -> Option<Cow<'rep, str>>
+ F: Fn(&str) -> Option<Cow<'rep, str>>
{
+ fn substitute(&self, key: &str) -> Option<Cow<'rep, str>> {
+ self(key)
+ }
+}
+
+// basically the same thing as replace_string, but it creates the String itself and returns it.
+pub fn replace_str<'rep>(input: &str, sub: impl SubFunc<'rep>) -> String {
let mut input = String::from(input);
replace_string(&mut input, sub);
input
}
+pub fn replace_thru<'rep>(mut input: String, sub: impl SubFunc<'rep>) -> String {
+ replace_string(&mut input, sub);
+ input
+}
+
// handles ${replacements} on this string IN-PLACE. Calls the "sub" function for each key it receives.
// if "sub" returns None, it will use a default value or ignore the ${substitution}.
// There are no "invalid inputs" and this function should never panic unless "sub" panics.
-pub fn replace_string<'rep, T>(input: &mut String, sub: T)
-where
- T: Fn(/*key: */ &str) -> Option<Cow<'rep, str>>
-{
+pub fn replace_string<'rep>(input: &mut String, sub: impl SubFunc<'rep>) {
let mut cursor = input.len();
while let Some(idx) = input[..cursor].rfind(VAR_BEGIN) {
// note: for some reason, apache processes escapes BEFORE checking if it's even a valid
@@ -58,7 +70,7 @@ where
def_opt = None;
}
- if let Some(sub_val) = sub(name).map_or_else(|| def_opt.map(|d| Cow::Owned(d.to_owned())), |v| Some(v)) {
+ if let Some(sub_val) = sub.substitute(name).map_or_else(|| def_opt.map(|d| Cow::Owned(d.to_owned())), |v| Some(v)) {
input.replace_range(idx..(endidx + VAR_END.len()), sub_val.as_ref());
}