Skip to main content

mdvault_core/captures/
repository.rs

1use std::path::{Path, PathBuf};
2
3use super::discovery::discover_captures;
4use super::lua_loader::load_capture_from_lua;
5use super::types::{CaptureDiscoveryError, CaptureInfo, CaptureRepoError, LoadedCapture};
6
7/// Repository for discovering and loading capture specifications
8pub struct CaptureRepository {
9    pub root: PathBuf,
10    pub captures: Vec<CaptureInfo>,
11}
12
13impl CaptureRepository {
14    /// Create a new repository by scanning the captures directory
15    pub fn new(root: &Path) -> Result<Self, CaptureDiscoveryError> {
16        let captures = discover_captures(root)?;
17        Ok(Self { root: root.to_path_buf(), captures })
18    }
19
20    /// List all discovered captures
21    pub fn list_all(&self) -> &[CaptureInfo] {
22        &self.captures
23    }
24
25    /// Load a capture by its logical name
26    pub fn get_by_name(&self, name: &str) -> Result<LoadedCapture, CaptureRepoError> {
27        let info = self
28            .captures
29            .iter()
30            .find(|c| c.logical_name == name)
31            .ok_or_else(|| CaptureRepoError::NotFound(name.to_string()))?;
32
33        let spec = load_capture_from_lua(&info.path)?;
34
35        Ok(LoadedCapture {
36            logical_name: info.logical_name.clone(),
37            path: info.path.clone(),
38            spec,
39        })
40    }
41}