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 5c909bd
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,10 @@ 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 {
return values.iter().fold(String::from(template), |result, (key, value)| {
let pattern = format!("{{{{ {key} }}}}");
result = result.replace(&pattern, value)
}

result
result.replace(&pattern, value)
})
}

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

0 comments on commit 5c909bd

Please sign in to comment.