#![cfg(all(feature = "embedded-platform-docs", everruns_has_workspace_docs))]
use everruns_core::capability_types::VirtualFileTree;
use include_dir::{Dir, include_dir};
use std::sync::Arc;
static DOCS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/../../docs");
fn dir_to_tree(dir: &Dir, base: &str) -> VirtualFileTree {
let mut tree = VirtualFileTree::new();
tree.insert_directory(base);
populate_tree(&mut tree, dir, base);
tree
}
fn populate_tree(tree: &mut VirtualFileTree, dir: &Dir, prefix: &str) {
for file in dir.files() {
let name = file
.path()
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("");
let ext = file
.path()
.extension()
.and_then(|e| e.to_str())
.unwrap_or("");
if !matches!(ext, "md" | "mdx") {
continue;
}
let path = format!("{prefix}/{name}");
let content = std::str::from_utf8(file.contents()).unwrap_or("");
tree.insert_text(&path, content);
}
for subdir in dir.dirs() {
let name = subdir
.path()
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("");
let path = format!("{prefix}/{name}");
tree.insert_directory(&path);
populate_tree(tree, subdir, &path);
}
}
pub(super) fn docs_tree() -> Arc<VirtualFileTree> {
use std::sync::OnceLock;
static TREE: OnceLock<Arc<VirtualFileTree>> = OnceLock::new();
TREE.get_or_init(|| Arc::new(dir_to_tree(&DOCS_DIR, "/docs")))
.clone()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn docs_tree_contains_markdown_only() {
let tree = docs_tree();
assert!(tree.get("/docs/capabilities/platform.md").is_some());
assert!(tree.get("/docs/capabilities/citations-ui.png").is_none());
}
}