mirror of
https://github.com/gorules/zen.git
synced 2026-10-05 00:02:26 +00:00
improve expression api
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
use crate::error::ZenError;
|
||||
use crate::types::JsonBuffer;
|
||||
use std::sync::Arc;
|
||||
use tokio::task;
|
||||
use zen_expression::expression::{Standard, Unary};
|
||||
use zen_expression::{Expression, Variable};
|
||||
|
||||
#[uniffi::export()]
|
||||
pub fn evaluate_expression_sync(
|
||||
#[uniffi::export]
|
||||
pub fn evaluate_expression(
|
||||
expression: String,
|
||||
context: Option<Arc<JsonBuffer>>,
|
||||
) -> Result<JsonBuffer, ZenError> {
|
||||
@@ -24,8 +23,8 @@ pub fn evaluate_expression_sync(
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[uniffi::export()]
|
||||
pub fn evaluate_unary_expression_sync(
|
||||
#[uniffi::export]
|
||||
pub fn evaluate_unary_expression(
|
||||
expression: String,
|
||||
context: Arc<JsonBuffer>,
|
||||
) -> Result<bool, ZenError> {
|
||||
@@ -38,34 +37,12 @@ pub fn evaluate_unary_expression_sync(
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[uniffi::export(async_runtime = "tokio")]
|
||||
pub async fn evaluate_expression(
|
||||
expression: String,
|
||||
context: Option<Arc<JsonBuffer>>,
|
||||
) -> Result<JsonBuffer, ZenError> {
|
||||
task::spawn_blocking(move || evaluate_expression_sync(expression, context))
|
||||
.await
|
||||
.map_err(|_| ZenError::ExecutionTaskSpawnError)?
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[uniffi::export(async_runtime = "tokio")]
|
||||
pub async fn evaluate_unary_expression(
|
||||
expression: String,
|
||||
context: Arc<JsonBuffer>,
|
||||
) -> Result<bool, ZenError> {
|
||||
task::spawn_blocking(move || evaluate_unary_expression_sync(expression, context))
|
||||
.await
|
||||
.map_err(|_| ZenError::ExecutionTaskSpawnError)?
|
||||
}
|
||||
|
||||
#[derive(uniffi::Object)]
|
||||
pub(crate) struct ZenExpression {
|
||||
expression: Expression<Standard>,
|
||||
}
|
||||
|
||||
#[uniffi::export(async_runtime = "tokio")]
|
||||
#[uniffi::export]
|
||||
impl ZenExpression {
|
||||
#[uniffi::constructor]
|
||||
pub fn compile(expression: String) -> Result<Self, ZenError> {
|
||||
@@ -74,17 +51,7 @@ impl ZenExpression {
|
||||
.map(|expression| Self { expression })
|
||||
}
|
||||
|
||||
pub async fn evaluate(&self, context: Option<Arc<JsonBuffer>>) -> Result<JsonBuffer, ZenError> {
|
||||
let expression = Self {
|
||||
expression: self.expression.clone(),
|
||||
};
|
||||
|
||||
task::spawn_blocking(move || expression.evaluate_sync(context))
|
||||
.await
|
||||
.map_err(|_| ZenError::ExecutionTaskSpawnError)?
|
||||
}
|
||||
|
||||
pub fn evaluate_sync(&self, context: Option<Arc<JsonBuffer>>) -> Result<JsonBuffer, ZenError> {
|
||||
pub fn evaluate(&self, context: Option<Arc<JsonBuffer>>) -> Result<JsonBuffer, ZenError> {
|
||||
let ctx: Variable = context.map(|b| b.to_variable()).unwrap_or(Variable::Null);
|
||||
let res = self.expression.evaluate(ctx).map_err(|e| {
|
||||
ZenError::EvaluationError(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string()))
|
||||
@@ -99,7 +66,7 @@ pub(crate) struct ZenExpressionUnary {
|
||||
expression: Expression<Unary>,
|
||||
}
|
||||
|
||||
#[uniffi::export(async_runtime = "tokio")]
|
||||
#[uniffi::export]
|
||||
impl ZenExpressionUnary {
|
||||
#[uniffi::constructor]
|
||||
pub fn compile(expression: String) -> Result<Self, ZenError> {
|
||||
@@ -108,17 +75,7 @@ impl ZenExpressionUnary {
|
||||
.map(|expression| Self { expression })
|
||||
}
|
||||
|
||||
pub async fn evaluate(&self, context: Arc<JsonBuffer>) -> Result<bool, ZenError> {
|
||||
let expression = Self {
|
||||
expression: self.expression.clone(),
|
||||
};
|
||||
|
||||
task::spawn_blocking(move || expression.evaluate_sync(context))
|
||||
.await
|
||||
.map_err(|_| ZenError::ExecutionTaskSpawnError)?
|
||||
}
|
||||
|
||||
pub fn evaluate_sync(&self, context: Arc<JsonBuffer>) -> Result<bool, ZenError> {
|
||||
pub fn evaluate(&self, context: Arc<JsonBuffer>) -> Result<bool, ZenError> {
|
||||
let ctx: Variable = context.to_variable();
|
||||
|
||||
self.expression.evaluate(ctx).map_err(|e| {
|
||||
|
||||
Reference in New Issue
Block a user