diff options
| author | 2025-01-20 00:53:39 -0600 | |
|---|---|---|
| committer | 2025-01-20 00:53:39 -0600 | |
| commit | 1782b86d68ec923df965386acd98f80ef6bcaa46 (patch) | |
| tree | 81f9891806e2bf6438f338489ae3a9f2ede23bd3 /src/launcher/strsub.rs | |
| parent | update strsub (diff) | |
get rid of wacky closure business
Diffstat (limited to 'src/launcher/strsub.rs')
| -rw-r--r-- | src/launcher/strsub.rs | 83 |
1 files changed, 39 insertions, 44 deletions
diff --git a/src/launcher/strsub.rs b/src/launcher/strsub.rs index c89681e..0d2357d 100644 --- a/src/launcher/strsub.rs +++ b/src/launcher/strsub.rs @@ -8,17 +8,8 @@ const VAR_BEGIN: &str = "${"; const VAR_END: &str = "}"; const VAR_DEFAULT: &str = ":-"; -pub trait SubFunc<'k, 'rep>: Copy { - fn substitute(self, key: &'k str) -> Option<Cow<'rep, str>>; -} - -impl<'k, 'rep, F> SubFunc<'k, 'rep> for F -where - F: Fn(&'k str) -> Option<Cow<'rep, str>> + Copy -{ - fn substitute(self, key: &'k str) -> Option<Cow<'rep, str>> { - self(key) - } +pub trait SubFunc<'rep> { + fn substitute(&self, key: &str) -> Option<Cow<'rep, str>>; } /* NOTE: the in-place implementation has been replaced for the following reasons: @@ -69,7 +60,7 @@ where } }*/ -pub fn replace_string<'k, 'rep>(input: &'k str, sub: impl SubFunc<'k, 'rep>) -> Cow<'k, str> { +pub fn replace_string<'inp, 'rep>(input: &'inp str, sub: &impl SubFunc<'rep>) -> Cow<'inp, str> { let mut ret: Option<String> = None; let mut cursor = 0usize; @@ -125,73 +116,77 @@ pub fn replace_string<'k, 'rep>(input: &'k str, sub: impl SubFunc<'k, 'rep>) -> mod tests { use super::*; - fn replace_fun(key: &str) -> Option<Cow<'static, str>> { - match key { - "exists" => Some(Cow::Borrowed("value123")), - "empty" => None, - "borger" => Some(Cow::Borrowed("\u{1f354}")), - _ => panic!("replace_fun called with unexpected key: {}", key) + #[derive(Clone, Copy)] + struct TestSub; + impl SubFunc<'static> for TestSub { + fn substitute(&self, key: &str) -> Option<Cow<'static, str>> { + match key { + "exists" => Some(Cow::Borrowed("value123")), + "empty" => None, + "borger" => Some(Cow::Borrowed("\u{1f354}")), + _ => panic!("replace_fun called with unexpected key: {}", key) + } } } #[test] fn test_standard_replace() { - assert_eq!(replace_string("this has ${exists} and more", replace_fun), "this has value123 and more"); - assert_eq!(replace_string("multiple ${exists} repl${exists}ace", replace_fun), "multiple value123 replvalue123ace"); - assert_eq!(replace_string("${exists}${exists}", replace_fun), "value123value123"); + assert_eq!(replace_string("this has ${exists} and more", &TestSub), "this has value123 and more"); + assert_eq!(replace_string("multiple ${exists} repl${exists}ace", &TestSub), "multiple value123 replvalue123ace"); + assert_eq!(replace_string("${exists}${exists}", &TestSub), "value123value123"); } #[test] fn test_empty_replace() { - assert_eq!(replace_string("this has ${empty} and more", replace_fun), "this has ${empty} and more"); - assert_eq!(replace_string("multiple ${empty} repl${empty}ace", replace_fun), "multiple ${empty} repl${empty}ace"); - assert_eq!(replace_string("${empty}${empty}", replace_fun), "${empty}${empty}"); + assert_eq!(replace_string("this has ${empty} and more", &TestSub), "this has ${empty} and more"); + assert_eq!(replace_string("multiple ${empty} repl${empty}ace", &TestSub), "multiple ${empty} repl${empty}ace"); + assert_eq!(replace_string("${empty}${empty}", &TestSub), "${empty}${empty}"); } #[test] fn test_homogenous_replace() { - assert_eq!(replace_string("some ${exists} and ${empty} ...", replace_fun), "some value123 and ${empty} ..."); - assert_eq!(replace_string("some ${empty} and ${exists} ...", replace_fun), "some ${empty} and value123 ..."); - assert_eq!(replace_string("${exists}${empty}", replace_fun), "value123${empty}"); - assert_eq!(replace_string("${empty}${exists}", replace_fun), "${empty}value123"); + assert_eq!(replace_string("some ${exists} and ${empty} ...", &TestSub), "some value123 and ${empty} ..."); + assert_eq!(replace_string("some ${empty} and ${exists} ...", &TestSub), "some ${empty} and value123 ..."); + assert_eq!(replace_string("${exists}${empty}", &TestSub), "value123${empty}"); + assert_eq!(replace_string("${empty}${exists}", &TestSub), "${empty}value123"); } #[test] fn test_default_replace() { - assert_eq!(replace_string("some ${exists:-def1} and ${empty:-def2} ...", replace_fun), "some value123 and def2 ..."); - assert_eq!(replace_string("some ${empty:-def1} and ${exists:-def2} ...", replace_fun), "some def1 and value123 ..."); - assert_eq!(replace_string("abc${empty:-}def", replace_fun), "abcdef"); - assert_eq!(replace_string("${empty:-}${empty:-}", replace_fun), ""); + assert_eq!(replace_string("some ${exists:-def1} and ${empty:-def2} ...", &TestSub), "some value123 and def2 ..."); + assert_eq!(replace_string("some ${empty:-def1} and ${exists:-def2} ...", &TestSub), "some def1 and value123 ..."); + assert_eq!(replace_string("abc${empty:-}def", &TestSub), "abcdef"); + assert_eq!(replace_string("${empty:-}${empty:-}", &TestSub), ""); } #[test] fn test_escape() { - assert_eq!(replace_string("an $${escaped} replacement (${exists})", replace_fun), "an ${escaped} replacement (value123)"); - assert_eq!(replace_string("${exists}$${escaped}${exists}", replace_fun), "value123${escaped}value123"); + assert_eq!(replace_string("an $${escaped} replacement (${exists})", &TestSub), "an ${escaped} replacement (value123)"); + assert_eq!(replace_string("${exists}$${escaped}${exists}", &TestSub), "value123${escaped}value123"); // make sure this weird behavior is preserved... (the original code seemed to show it) - assert_eq!(replace_string("some $${ else", replace_fun), "some ${ else"); + assert_eq!(replace_string("some $${ else", &TestSub), "some ${ else"); } #[test] fn test_weird() { - assert_eq!(replace_string("${exists}", replace_fun), "value123"); - assert_eq!(replace_string("$${empty}", replace_fun), "${empty}"); - assert_eq!(replace_string("${empty:-a}", replace_fun), "a"); - assert_eq!(replace_string("${empty:-}", replace_fun), ""); + assert_eq!(replace_string("${exists}", &TestSub), "value123"); + assert_eq!(replace_string("$${empty}", &TestSub), "${empty}"); + assert_eq!(replace_string("${empty:-a}", &TestSub), "a"); + assert_eq!(replace_string("${empty:-}", &TestSub), ""); } // these make sure it doesn't chop up multibyte characters illegally #[test] fn test_multibyte_surround() { - assert_eq!(replace_string("\u{1f354}$${}\u{1f354}", replace_fun), "\u{1f354}${}\u{1f354}"); - assert_eq!(replace_string("\u{1f354}${exists}\u{1f354}${empty:-}\u{1f354}", replace_fun), "\u{1f354}value123\u{1f354}\u{1f354}"); + assert_eq!(replace_string("\u{1f354}$${}\u{1f354}", &TestSub), "\u{1f354}${}\u{1f354}"); + assert_eq!(replace_string("\u{1f354}${exists}\u{1f354}${empty:-}\u{1f354}", &TestSub), "\u{1f354}value123\u{1f354}\u{1f354}"); } #[test] fn test_multibyte_replace() { - assert_eq!(replace_string("borger ${borger}", replace_fun), "borger \u{1f354}"); - assert_eq!(replace_string("${exists:-\u{1f354}}${empty:-\u{1f354}}", replace_fun), "value123\u{1f354}"); - assert_eq!(replace_string("${borger}$${}${borger}", replace_fun), "\u{1f354}${}\u{1f354}"); + assert_eq!(replace_string("borger ${borger}", &TestSub), "borger \u{1f354}"); + assert_eq!(replace_string("${exists:-\u{1f354}}${empty:-\u{1f354}}", &TestSub), "value123\u{1f354}"); + assert_eq!(replace_string("${borger}$${}${borger}", &TestSub), "\u{1f354}${}\u{1f354}"); } } |
