File size: 1,511 Bytes
1851bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use handlebars::Handlebars;
use include_dir::{Dir, DirEntry, File};

/// Returns an iterator over all files embedded in `dir`, recursively
/// descending into subdirectories.
pub fn files(dir: &'static Dir<'static>) -> impl Iterator<Item = &'static File<'static>> {
    dir.entries().iter().flat_map(walk_entry)
}

fn walk_entry(
    entry: &'static DirEntry<'static>,
) -> Box<dyn Iterator<Item = &'static File<'static>>> {
    match entry {
        DirEntry::File(f) => Box::new(std::iter::once(f)),
        DirEntry::Dir(d) => Box::new(d.entries().iter().flat_map(walk_entry)),
    }
}

/// Registers all files in `dir` (recursively) as Handlebars templates.
///
/// Template names match the relative file path as returned by
/// [`File::path`] (e.g. `forge-system-prompt.md`). Panics if any file path
/// is not valid UTF-8, if any file content is not valid UTF-8, or if template
/// parsing fails.
pub fn register_templates(hb: &mut Handlebars<'_>, dir: &'static Dir<'static>) {
    for file in files(dir) {
        let name = file.path().to_str().unwrap_or_else(|| {
            panic!(
                "embedded template path '{:?}' is not valid UTF-8",
                file.path()
            )
        });
        let content = file
            .contents_utf8()
            .unwrap_or_else(|| panic!("embedded template '{}' is not valid UTF-8", name));
        hb.register_template_string(name, content)
            .unwrap_or_else(|e| panic!("failed to register template '{}': {}", name, e));
    }
}