Skip to main content

mdvault_core/scripting/
hooks.rs

1//! Hook types and errors.
2//!
3//! This module provides types for hook execution, including the note context
4//! passed to hooks and error types for hook failures.
5
6use std::path::PathBuf;
7
8use thiserror::Error;
9
10/// Note context passed to Lua hooks.
11///
12/// Contains all information about a note that hooks might need to access.
13#[derive(Debug, Clone)]
14pub struct NoteContext {
15    /// Path to the note file (relative to vault root).
16    pub path: PathBuf,
17    /// Note type from frontmatter (e.g., "task", "meeting").
18    pub note_type: String,
19    /// Parsed frontmatter as YAML value.
20    pub frontmatter: serde_yaml::Value,
21    /// Full content of the note (including frontmatter).
22    pub content: String,
23    /// Template variables used to render the note (as a map).
24    pub variables: serde_yaml::Value,
25}
26
27impl NoteContext {
28    /// Create a new NoteContext.
29    pub fn new(
30        path: PathBuf,
31        note_type: String,
32        frontmatter: serde_yaml::Value,
33        content: String,
34        variables: serde_yaml::Value,
35    ) -> Self {
36        Self { path, note_type, frontmatter, content, variables }
37    }
38}
39
40/// Errors that can occur during hook execution.
41#[derive(Debug, Error)]
42pub enum HookError {
43    /// Template not found.
44    #[error("template not found: {0}")]
45    TemplateNotFound(String),
46
47    /// Capture not found.
48    #[error("capture not found: {0}")]
49    CaptureNotFound(String),
50
51    /// Macro not found.
52    #[error("macro not found: {0}")]
53    MacroNotFound(String),
54
55    /// Hook execution failed.
56    #[error("hook execution failed: {0}")]
57    Execution(String),
58
59    /// Lua runtime error.
60    #[error("Lua error: {0}")]
61    LuaError(String),
62
63    /// Template rendering error.
64    #[error("template render error: {0}")]
65    TemplateRender(String),
66
67    /// Capture execution error.
68    #[error("capture execution error: {0}")]
69    CaptureExecution(String),
70
71    /// Macro execution error.
72    #[error("macro execution error: {0}")]
73    MacroExecution(String),
74
75    /// IO error during hook execution.
76    #[error("IO error: {0}")]
77    Io(#[from] std::io::Error),
78}