Skip to main content

moss_core/resolve/
folder_class.rs

1//! Directory-shaped resolution capability (injected; pure core).
2//! Build impl is backed by the content graph + html_files; the editor impl
3//! by read_dir. Separate from AssetIndex because the backing data and the
4//! query shape (is_dir / markdown-index / static-index) differ.
5
6pub trait FolderIndex {
7    /// Does a directory exist at this root-relative path?
8    fn is_dir(&self, root_rel: &str) -> bool;
9    /// Does this folder resolve a markdown index (→ FolderListing)?
10    /// Build: a doc whose url_path is this folder's index URL (covers
11    /// content-folder promotion). Editor: FS index.md/README.md/_index.md.
12    fn dir_has_markdown_index(&self, root_rel: &str) -> bool;
13    /// Does this folder have a static index.html/.htm (and no markdown index)?
14    /// Returns the index filename (→ FolderIndexIframe).
15    fn dir_has_static_index(&self, root_rel: &str) -> Option<String>;
16}
17
18#[cfg(test)]
19pub(crate) struct FakeFolderIndex {
20    pub dirs: std::collections::HashSet<String>,
21    pub md_index: std::collections::HashSet<String>,
22    pub static_index: std::collections::HashMap<String, String>,
23}
24
25#[cfg(test)]
26impl FakeFolderIndex {
27    pub fn new() -> Self {
28        FakeFolderIndex {
29            dirs: Default::default(),
30            md_index: Default::default(),
31            static_index: Default::default(),
32        }
33    }
34}
35
36#[cfg(test)]
37impl FolderIndex for FakeFolderIndex {
38    fn is_dir(&self, p: &str) -> bool {
39        self.dirs.contains(p)
40    }
41    fn dir_has_markdown_index(&self, p: &str) -> bool {
42        self.md_index.contains(p)
43    }
44    fn dir_has_static_index(&self, p: &str) -> Option<String> {
45        self.static_index.get(p).cloned()
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn fake_reports_dir_kinds() {
55        let mut idx = FakeFolderIndex::new();
56        idx.dirs.insert("Resources/app".into());
57        idx.static_index
58            .insert("Resources/app".into(), "index.html".into());
59        assert!(idx.is_dir("Resources/app"));
60        assert!(!idx.dir_has_markdown_index("Resources/app"));
61        assert_eq!(
62            idx.dir_has_static_index("Resources/app"),
63            Some("index.html".into())
64        );
65    }
66}