Skip to main content

bookspec_checker/
files.rs

1//! The specification's own files, carried by the crate: a consumer that
2//! depends on this crate (a git checkout carries the whole repository)
3//! needs no path to `examples/` or `spec/`.
4
5use include_dir::{Dir, include_dir};
6
7static EXAMPLES: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/../examples");
8static SPEC: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/../spec");
9
10/// The files of `dir` whose name ends in `ext`, as (file name, text),
11/// sorted by name.
12fn texts(dir: &'static Dir<'static>, ext: &str) -> Vec<(&'static str, &'static str)> {
13    let mut out: Vec<_> = dir
14        .files()
15        .filter_map(|f| {
16            let name = f.path().file_name()?.to_str()?;
17            name.ends_with(ext)
18                .then(|| (name, f.contents_utf8().expect("a UTF-8 file")))
19        })
20        .collect();
21    out.sort();
22    out
23}
24
25/// Every example (`examples/*.toml`) as (file name, text), sorted by name.
26#[must_use]
27pub fn examples() -> Vec<(&'static str, &'static str)> {
28    texts(&EXAMPLES, ".toml")
29}
30
31/// Every specification file (`spec/*.md`) as (file name, text), sorted by
32/// name.
33#[must_use]
34pub fn spec_files() -> Vec<(&'static str, &'static str)> {
35    texts(&SPEC, ".md")
36}