Engine

Struct Engine 

Source
pub struct Engine(/* private fields */);
Expand description

A Lua wrapper for the rusty_rules::Engine.

The Engine is the main entry point for working with rules. It allows you to:

  • Register custom fetchers that extract data from context
  • Compile rule definitions
  • Validate rule syntax (when validation feature is enabled)
  • Generate JSON schema for rule structure

§Examples

local engine = Engine.new()

-- Register a simple fetcher
engine:register_fetcher("user_attr", function(ctx, attr)
    return ctx.user[attr]
end)

-- Compile and evaluate a rule
local rule = engine:compile({["user_attr(role)"] = "admin"})
local is_admin = rule:evaluate({user = {role = "admin"}})

Implementations§

Source§

impl Engine

Source

pub fn new() -> Self

Creates a new rules engine instance for Lua.

Methods from Deref<Target = Engine<LuaValue>>§

Source

pub fn register_fetcher<F>(&mut self, name: &str, func: F) -> &mut Fetcher<Ctx>
where F: for<'a> Fn(&'a Ctx, &[String]) -> Result<Value<'a>, Box<dyn Error>> + MaybeSend + MaybeSync + 'static,

Registers a synchronous fetcher with its name and function, using the default matcher.

A fetcher is a function that extracts values from the context type. The fetcher name is used in rule definitions to reference this fetcher. By default, the DefaultMatcher is used, which supports basic equality and comparison operations.

§Returns

A mutable reference to the created Fetcher, allowing you to customize it (e.g., change the matcher)

Source

pub fn register_async_fetcher<F>( &mut self, name: &str, func: F, ) -> &mut Fetcher<Ctx>
where F: for<'a> Fn(&'a Ctx, Arc<[String]>) -> Pin<Box<dyn Future<Output = Result<Value<'a>, Box<dyn Error>>> + 'a>> + MaybeSend + MaybeSync + 'static,

Registers an async fetcher with its name and function, using the default matcher.

See Self::register_fetcher for more details.

Source

pub fn register_operator<O>(&mut self, name: &str, op: O)
where O: ToOperator<Ctx> + 'static,

Registers a custom operator

Source

pub fn compile_rule(&self, value: &Value) -> Result<Rule<Ctx>, Error>

Compiles a JSON value into a Rule::All using the registered fetchers and operators.

Source

pub fn validate_rule<'a>( &self, value: &'a Value, ) -> Result<(), ValidationError<'a>>

Validates a JSON rule against dynamically generated JSON Schema of this engine.

Source

pub fn json_schema(&self) -> Value

Builds a JSON Schema for rules, including dynamic properties.

Trait Implementations§

Source§

impl Deref for Engine

Source§

type Target = Engine<Value>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for Engine

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl UserData for Engine

Source§

fn register(registry: &mut LuaUserDataRegistry<Self>)

Registers this type for use in Lua. Read more
Source§

fn add_fields<F>(fields: &mut F)
where F: UserDataFields<Self>,

Adds custom fields specific to this userdata.
Source§

fn add_methods<M>(methods: &mut M)
where M: UserDataMethods<Self>,

Adds custom methods and operators specific to this userdata.

Auto Trait Implementations§

§

impl Freeze for Engine

§

impl !RefUnwindSafe for Engine

§

impl !Send for Engine

§

impl !Sync for Engine

§

impl Unpin for Engine

§

impl !UnwindSafe for Engine

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> IntoLua for T
where T: UserData + MaybeSend + 'static,

Source§

fn into_lua(self, lua: &Lua) -> Result<Value, Error>

Performs the conversion.
Source§

impl<T> IntoLuaMulti for T
where T: IntoLua,

Source§

fn into_lua_multi(self, lua: &Lua) -> Result<MultiValue, Error>

Performs the conversion.
Source§

unsafe fn push_into_stack_multi(self, lua: &RawLua) -> Result<i32, Error>

Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSend for T

Source§

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

Source§

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