File: util.rs

package info (click to toggle)
rust-bat 0.25.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,096 kB
  • sloc: sh: 255; python: 39; makefile: 14
file content (21 lines) | stat: -rw-r--r-- 564 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#![allow(dead_code)]

use std::{collections::HashMap, fs, path::Path};

/// Generates a file from a template.
pub fn render_template(
    variables: &HashMap<&str, String>,
    in_file: &str,
    out_file: impl AsRef<Path>,
) -> anyhow::Result<()> {
    let mut content = fs::read_to_string(in_file)?;

    for (variable_name, value) in variables {
        // Replace {{variable_name}} by the value
        let pattern = format!("{{{{{variable_name}}}}}");
        content = content.replace(&pattern, value);
    }

    fs::write(out_file, content)?;
    Ok(())
}