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
validationfeature 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§
Methods from Deref<Target = Engine<LuaValue>>§
Sourcepub fn register_fetcher<F>(&mut self, name: &str, func: F) -> &mut Fetcher<Ctx>
pub fn register_fetcher<F>(&mut self, name: &str, func: F) -> &mut Fetcher<Ctx>
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)
Sourcepub fn register_async_fetcher<F>(
&mut self,
name: &str,
func: F,
) -> &mut Fetcher<Ctx>
pub fn register_async_fetcher<F>( &mut self, name: &str, func: F, ) -> &mut Fetcher<Ctx>
Registers an async fetcher with its name and function, using the default matcher.
See Self::register_fetcher for more details.
Sourcepub fn register_operator<O>(&mut self, name: &str, op: O)where
O: ToOperator<Ctx> + 'static,
pub fn register_operator<O>(&mut self, name: &str, op: O)where
O: ToOperator<Ctx> + 'static,
Registers a custom operator
Sourcepub fn compile_rule(&self, value: &Value) -> Result<Rule<Ctx>, Error>
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.
Sourcepub fn validate_rule<'a>(
&self,
value: &'a Value,
) -> Result<(), ValidationError<'a>>
pub fn validate_rule<'a>( &self, value: &'a Value, ) -> Result<(), ValidationError<'a>>
Validates a JSON rule against dynamically generated JSON Schema of this engine.
Sourcepub fn json_schema(&self) -> Value
pub fn json_schema(&self) -> Value
Builds a JSON Schema for rules, including dynamic properties.
Trait Implementations§
Source§impl UserData for Engine
impl UserData for Engine
Source§fn register(registry: &mut LuaUserDataRegistry<Self>)
fn register(registry: &mut LuaUserDataRegistry<Self>)
Source§fn add_fields<F>(fields: &mut F)where
F: UserDataFields<Self>,
fn add_fields<F>(fields: &mut F)where
F: UserDataFields<Self>,
Source§fn add_methods<M>(methods: &mut M)where
M: UserDataMethods<Self>,
fn add_methods<M>(methods: &mut M)where
M: UserDataMethods<Self>,
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> 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
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>
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>
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