pub struct LuaEngine { /* private fields */ }Expand description
A sandboxed Lua execution environment.
The engine provides access to mdvault functionality through the mdv
global table while restricting dangerous operations like file I/O
and shell execution.
§Example
use mdvault_core::scripting::LuaEngine;
let engine = LuaEngine::sandboxed().unwrap();
let result = engine.eval_string(r#"mdv.date("today + 7d")"#).unwrap();
println!("One week from now: {}", result);Implementations§
Source§impl LuaEngine
impl LuaEngine
Sourcepub fn new(config: SandboxConfig) -> Result<Self, ScriptingError>
pub fn new(config: SandboxConfig) -> Result<Self, ScriptingError>
Create a new Lua engine with the given sandbox configuration.
Sourcepub fn sandboxed() -> Result<Self, ScriptingError>
pub fn sandboxed() -> Result<Self, ScriptingError>
Create a new engine with default restrictive sandbox.
Sourcepub fn with_vault_context(
config: SandboxConfig,
vault_ctx: VaultContext,
) -> Result<Self, ScriptingError>
pub fn with_vault_context( config: SandboxConfig, vault_ctx: VaultContext, ) -> Result<Self, ScriptingError>
Create a new Lua engine with vault context for hook execution.
This provides access to mdv.template(), mdv.capture(), mdv.macro()
and index query functions in addition to the standard sandboxed bindings.
§Example
ⓘ
use mdvault_core::scripting::{LuaEngine, VaultContext, SandboxConfig};
let vault_ctx = VaultContext::new(config, templates, captures, macros, types);
let engine = LuaEngine::with_vault_context(SandboxConfig::restricted(), vault_ctx)?;
// Now Lua scripts can use vault operations
engine.eval_string(r#"
local ok, err = mdv.capture("log-to-daily", { text = "Hello" })
"#)?;
// And query the index (if available)
engine.eval_string(r#"
local tasks = mdv.query({ type = "task" })
"#)?;Sourcepub fn eval(&self, script: &str) -> Result<Option<String>, ScriptingError>
pub fn eval(&self, script: &str) -> Result<Option<String>, ScriptingError>
Execute a Lua script and return the result.
Returns None if the script returns nil or no value.
Sourcepub fn eval_string(&self, script: &str) -> Result<String, ScriptingError>
pub fn eval_string(&self, script: &str) -> Result<String, ScriptingError>
Execute a Lua script that must return a string value.
Returns an error if the script returns nil.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for LuaEngine
impl !RefUnwindSafe for LuaEngine
impl !Send for LuaEngine
impl !Sync for LuaEngine
impl Unpin for LuaEngine
impl UnsafeUnpin for LuaEngine
impl !UnwindSafe for LuaEngine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more