Skip to content

Commit

Permalink
Use fold to replace all placeholders in template 🪭
Browse files Browse the repository at this point in the history
  • Loading branch information
pmeinhardt committed May 24, 2024
1 parent e741e37 commit 851617e
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@ fn convert(input: &str, dangerous: bool) -> Result<String, Message> {
}

fn render(template: &str, values: &HashMap<&str, &str>) -> String {
let mut result = String::from(template);

for (key, value) in values {
let pattern = format!("{{{{ {key} }}}}");
result = result.replace(&pattern, value)
}

result
return values
.iter()
.fold(String::from(template), |result, (key, value)| {
let pattern = format!("{{{{ {key} }}}}");
result.replace(&pattern, value)
});
}

fn main() -> Result<()> {
Expand Down

0 comments on commit 851617e

Please sign in to comment.