Skip to main content

hearth_graph/
fs_loader.rs

1//! Filesystem-backed [`SourceLoader`] — the only module that touches
2//! `std::fs`, so the build driver itself stays free of ambient I/O.
3
4use std::path::PathBuf;
5
6use crate::build::SourceLoader;
7
8/// Filesystem-backed source loader rooted at one directory.
9pub struct FsLoader {
10    root: PathBuf,
11}
12
13impl FsLoader {
14    /// Creates a loader that resolves every source path relative to `root`.
15    pub fn new(root: impl Into<PathBuf>) -> Self {
16        Self { root: root.into() }
17    }
18}
19
20impl SourceLoader for FsLoader {
21    fn verify(&self) -> Result<(), String> {
22        let metadata = std::fs::metadata(&self.root).map_err(|error| {
23            format!(
24                "cannot build symbol index: source root '{}' is unavailable: {error}",
25                self.root.display()
26            )
27        })?;
28        if !metadata.is_dir() {
29            return Err(format!(
30                "cannot build symbol index: source root '{}' is not a directory",
31                self.root.display()
32            ));
33        }
34        Ok(())
35    }
36
37    fn probe(&self, path: &str) -> Option<u64> {
38        let metadata = std::fs::metadata(self.root.join(path)).ok()?;
39        metadata.is_file().then_some(metadata.len())
40    }
41
42    fn load(&self, path: &str) -> Option<String> {
43        std::fs::read_to_string(self.root.join(path)).ok()
44    }
45}