Skip to main content

LuaEngine

Struct LuaEngine 

Source
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

Source

pub fn new(config: SandboxConfig) -> Result<Self, ScriptingError>

Create a new Lua engine with the given sandbox configuration.

Source

pub fn sandboxed() -> Result<Self, ScriptingError>

Create a new engine with default restrictive sandbox.

Source

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" })
"#)?;
Source

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.

Source

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.

Source

pub fn eval_bool(&self, script: &str) -> Result<bool, ScriptingError>

Execute a Lua script that returns a boolean.

Source

pub fn lua(&self) -> &Lua

Get a reference to the underlying Lua state (for advanced usage).

Trait Implementations§

Source§

impl Default for LuaEngine

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> MaybeSend for T