mdvault_core/scripting/
vault_context.rs

1//! Vault context for Lua scripting.
2//!
3//! This module provides the `VaultContext` struct which holds references
4//! to all vault repositories needed for executing vault operations from Lua.
5
6use std::path::PathBuf;
7use std::sync::Arc;
8
9use crate::captures::CaptureRepository;
10use crate::config::types::ResolvedConfig;
11use crate::index::IndexDb;
12use crate::macros::MacroRepository;
13use crate::templates::repository::TemplateRepository;
14use crate::types::TypeRegistry;
15
16/// Information about the current note being processed.
17///
18/// This is set when validating or processing a specific note,
19/// allowing Lua hooks to access note metadata.
20#[derive(Clone, Debug)]
21pub struct CurrentNote {
22    /// Path to the note relative to vault root.
23    pub path: String,
24    /// Note type from frontmatter.
25    pub note_type: String,
26    /// Note title.
27    pub title: Option<String>,
28    /// Frontmatter as YAML value.
29    pub frontmatter: Option<serde_yaml::Value>,
30    /// Note content.
31    pub content: String,
32}
33
34/// Context for vault operations accessible from Lua hooks.
35///
36/// This struct holds Arc references to avoid cloning large repositories.
37/// It's designed to be passed to Lua bindings for template/capture/macro execution.
38#[derive(Clone)]
39pub struct VaultContext {
40    /// Resolved configuration with paths.
41    pub config: Arc<ResolvedConfig>,
42    /// Template repository for loading templates.
43    pub template_repo: Arc<TemplateRepository>,
44    /// Capture repository for loading captures.
45    pub capture_repo: Arc<CaptureRepository>,
46    /// Macro repository for loading macros.
47    pub macro_repo: Arc<MacroRepository>,
48    /// Type registry for type definitions.
49    pub type_registry: Arc<TypeRegistry>,
50    /// Optional index database for query operations.
51    pub index_db: Option<Arc<IndexDb>>,
52    /// Optional current note being processed.
53    pub current_note: Option<CurrentNote>,
54    /// Vault root path for resolving relative paths.
55    pub vault_root: PathBuf,
56}
57
58impl VaultContext {
59    /// Create a new VaultContext from owned values.
60    pub fn new(
61        config: ResolvedConfig,
62        template_repo: TemplateRepository,
63        capture_repo: CaptureRepository,
64        macro_repo: MacroRepository,
65        type_registry: TypeRegistry,
66    ) -> Self {
67        let vault_root = config.vault_root.clone();
68        Self {
69            config: Arc::new(config),
70            template_repo: Arc::new(template_repo),
71            capture_repo: Arc::new(capture_repo),
72            macro_repo: Arc::new(macro_repo),
73            type_registry: Arc::new(type_registry),
74            index_db: None,
75            current_note: None,
76            vault_root,
77        }
78    }
79
80    /// Create a new VaultContext from Arc references.
81    pub fn from_arcs(
82        config: Arc<ResolvedConfig>,
83        template_repo: Arc<TemplateRepository>,
84        capture_repo: Arc<CaptureRepository>,
85        macro_repo: Arc<MacroRepository>,
86        type_registry: Arc<TypeRegistry>,
87    ) -> Self {
88        let vault_root = config.vault_root.clone();
89        Self {
90            config,
91            template_repo,
92            capture_repo,
93            macro_repo,
94            type_registry,
95            index_db: None,
96            current_note: None,
97            vault_root,
98        }
99    }
100
101    /// Set the index database for query operations.
102    pub fn with_index(mut self, index_db: Arc<IndexDb>) -> Self {
103        self.index_db = Some(index_db);
104        self
105    }
106
107    /// Set the current note being processed.
108    pub fn with_current_note(mut self, note: CurrentNote) -> Self {
109        self.current_note = Some(note);
110        self
111    }
112}