Skip to main content

yog_cli/design/
files.rs

1//! A project's files, by format.
2
3use std::path::{Path, PathBuf};
4
5use yog_ids::Format;
6
7/// Every file under `dir` (all the way down) whose extension is `ext`, sorted.
8pub fn with_ext(dir: &Path, ext: &str) -> Vec<PathBuf> {
9    let mut out = Vec::new();
10    let mut pending = vec![dir.to_path_buf()];
11    while let Some(dir) = pending.pop() {
12        for entry in std::fs::read_dir(&dir).into_iter().flatten().flatten() {
13            let path = entry.path();
14            if path.is_dir() {
15                pending.push(path);
16            } else if path.extension().is_some_and(|e| e == ext) {
17                out.push(path);
18            }
19        }
20    }
21    out.sort();
22    out
23}
24
25/// Every file of `format` under `dir`, sorted.
26pub fn of(dir: &Path, format: Format) -> Vec<PathBuf> {
27    with_ext(dir, format.ext())
28}