terminal_magic/
template.rs1use std::path::Path;
2
3use indexmap::IndexMap;
4use mustache::MapBuilder;
5
6use crate::{models::FileSystemEntry, modules::install::write_supporting_files};
7
8pub fn add_files_as_vars(
16 files: &IndexMap<String, FileSystemEntry>,
17 mut mustache_map_builder: MapBuilder,
18 home: &Path,
19 path_to_module: &Path,
20 cwd: &Path,
21 should_overwrite: bool,
22) -> MapBuilder {
23 if should_overwrite {
24 write_supporting_files(files, home, path_to_module, cwd);
25 }
26 for (place_holder, entry) in files.iter() {
27 match entry {
28 FileSystemEntry::File {
29 version: _version,
30 path,
31 destination,
32 } => {
33 let destination = if let Some(destination) = destination {
34 destination
35 .to_owned()
36 .parse()
37 .expect("Could not parse path")
38 } else {
39 cwd.join(path)
40 };
41
42 mustache_map_builder = mustache_map_builder
43 .insert(place_holder, &destination.to_string_lossy())
44 .expect("Error inserting file placeholder");
45 }
46 FileSystemEntry::Directory {
47 version: _version,
48 destination,
49 path,
50 files,
51 } => {
52 let destination = if let Some(destination) = destination {
53 destination
54 .to_owned()
55 .parse()
56 .expect("Could not parse path")
57 } else {
58 cwd.join(path)
59 };
60
61 mustache_map_builder = mustache_map_builder
62 .insert(place_holder, &destination.to_string_lossy())
63 .expect("Error inserting file placeholder");
64 mustache_map_builder = add_files_as_vars(
65 files,
66 mustache_map_builder,
67 home,
68 path_to_module,
69 cwd,
70 should_overwrite,
71 );
72 }
73 }
74 }
75 mustache_map_builder
76}
77
78
79pub fn render(mustache: mustache::Template, mustache_map: mustache::Data) -> String {
80 mustache
81 .render_data_to_string(&mustache_map)
82 .expect("Could not render mustache template")
83}