Skip to main content

replace_expressions

Function replace_expressions 

Source
pub async fn replace_expressions<W, F>(
    template: &str,
    w: &mut W,
    f: F,
) -> Result<()>
where W: Write, F: Fn(&str) -> BoxFuture<'_, Result<String>>,
Expand description

Replaces variables between {{ }} with text returned from function.

The function f receives a variable named (trimmed of leading and trailing whitespace) and must return an Ok(String) or an Err, which will terminate the replacement. Any text written to the io::Write w will be left as-is.

Overlapping templates e.g., {{ {{foo}} }} with attempt to replace the text between the first occurrences of both {{ and }}, so the function would receive {{foo and, even if the function returned Ok("".to_string()), would still contain the final }} in the text.

ยงExamples

use akv_cli::parsing::replace_expressions;

let s = "Hello, {{ var }}!";
let mut buf = Vec::new();

replace_expressions(s, &mut buf, |v| {
    assert_eq!(v, "var");
    async { Ok(String::from("world")) }.boxed()
}).await.unwrap();

assert_eq!(String::from_utf8(buf).unwrap(), "Hello, world!");