Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

substitute and count vars remaining #13

Open
ratmice opened this issue Nov 3, 2020 · 2 comments
Open

substitute and count vars remaining #13

ratmice opened this issue Nov 3, 2020 · 2 comments

Comments

@ratmice
Copy link

ratmice commented Nov 3, 2020

It might be nice if there was a function like:
fn substitute_count_remaining_vars(template: T, variables: &HashMap<String, String>) -> Result<(String, usize), Error>
which returned the number of variables not substituted, so you could perform something like:
Presumably, it'd be nice to have a ValidatedVarMap<String, String> to avoid calling validate() in the loop?

With that you could still ensure that it terminates even though is_templated returns true.
I don't really have a need for it at the moment, but is this something you would consider accepting a patch for?

    let mut foo = "${${foo}bar}".to_string();
    let mut variables_left = 0;
    let mut vars = HashMap::new();
    vars.insert("foo".to_string(), "bar".to_string());
    vars.insert("barbar".to_string(), "baz".to_string());
    loop {
        let (bar, n) = substitute_count_remaining_vars(foo.into(), &vars)?;
        if n < variables_left {
            variables_left = n;
            foo = bar;
            continue;
        }
        foo = bar;
        break;
    }
@ratmice ratmice changed the title substitute and count cars not matching? substitute and count vars remaining Nov 9, 2020
@lucab
Copy link
Contributor

lucab commented Mar 1, 2021

(Sorry for the delay, I missed the original notification for this).

Not against adding more stuff here, but unless there is a clear usecase for those things I'd rather hold. The library is small and naïf on purpose.

And by the way, "${${foo}bar}" is a problematic template which we should probably detect and reject.

@ratmice
Copy link
Author

ratmice commented Mar 1, 2021

I think it is okay in a template (Or at least I'm failing to see why it is problematic at least when vars doesn't change between substitutions), however it is definitely problematic a key, where IIRC vars.insert("${foo}bar"), IIRC is already rejected.

FWIW no worries about the delay, I'll see If I can come up with a sufficiently compelling use case, and if not we can close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants