Skip to content

Commit

Permalink
Enhance documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
codetiger committed Dec 20, 2024
1 parent 8688859 commit e97cae0
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
//! Error types for JSON Logic operations
//!
//! This module provides comprehensive error handling for rule parsing and evaluation.
use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
#[error("Unknown operator: {0}")]
UnknownOperator(String),

#[error("Invalid rule format: {0}")]
InvalidRule(String),

#[error("Invalid arguments: {0}")]
InvalidArguments(String),

#[error("Type error: {0}")]
Type(String),
}
54 changes: 52 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
//! A high-performance JSON Logic implementation for Rust
//!
//! This crate provides a way to write portable logic rules as JSON, following the
//! [JSONLogic specification](http://jsonlogic.com). It offers:
//!
//! - Full compliance with JSONLogic specification
//! - Thread-safe evaluation of rules
//! - Zero-copy JSON handling
//! - Comprehensive error handling
//!
//! # Quick Example
//! ```rust
//! use datalogic_rs::{JsonLogic, Rule};
//! use serde_json::json;
//!
//! let rule = Rule::from_value(&json!({
//! "if": [
//! {">": [{"var": "temp"}, 110]},
//! "too hot",
//! "ok"
//! ]
//! })).unwrap();
//!
//! let data = json!({"temp": 120});
//! let result = JsonLogic::apply(&rule, &data).unwrap();
//! assert_eq!(result, json!("too hot"));
//! ```
mod error;
mod rule;

use error::Error;
use serde_json::Value;
pub use rule::Rule;

/// Result type for JSON Logic operations
pub type JsonLogicResult = Result<Value, Error>;

/// Main entry point for evaluating JSON Logic rules
///
/// Provides a thread-safe, zero-copy implementation for evaluating rules against data.
#[derive(Clone)]
pub struct JsonLogic {
}
pub struct JsonLogic {}

impl Default for JsonLogic {
fn default() -> Self {
Expand All @@ -18,10 +49,29 @@ impl Default for JsonLogic {
}

impl JsonLogic {
/// Creates a new JsonLogic evaluator
pub fn new() -> Self {
Self {}
}

/// Evaluates a rule against the provided data
///
/// ## Arguments
/// * `rule` - The compiled rule to evaluate
/// * `data` - The data to evaluate against
///
/// ## Returns
/// * `JsonLogicResult` containing either the evaluation result or an error
///
/// ## Example
/// ```rust
/// ### use datalogic_rs::{JsonLogic, Rule};
/// ### use serde_json::json;
/// let rule = Rule::from_value(&json!({"var": "user.name"})).unwrap();
/// let data = json!({"user": {"name": "John"}});
/// let result = JsonLogic::apply(&rule, &data).unwrap();
/// assert_eq!(result, json!("John"));
/// ```
pub fn apply(rule: &Rule, data: &Value) -> JsonLogicResult {
rule.apply(data)
}
Expand Down
46 changes: 45 additions & 1 deletion src/rule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,51 @@ impl Rule {
}
}

/// Creates a new `Rule` from a JSON Value
///
/// Parses a serde_json::Value into a Rule that can be evaluated. The value must follow
/// the JSONLogic specification format.
///
/// ## Arguments
/// * `value` - A JSON value representing the rule. Must be a valid JSONLogic expression.
///
/// ## Returns
/// * `Result<Rule, Error>` - A Result containing either the parsed Rule or an error
///
/// ## Examples
///
/// Basic usage:
/// ```rust
/// use datalogic_rs::Rule;
/// use serde_json::json;
///
/// let rule = Rule::from_value(&json!({"==": [1, 1]})).unwrap();
/// assert!(rule.apply(&json!(null)).unwrap().as_bool().unwrap());
/// ```
///
/// Complex nested rules:
/// ```rust
/// use datalogic_rs::Rule;
/// use serde_json::json;
///
/// let rule = Rule::from_value(&json!({
/// "and": [
/// {">": [{"var": "age"}, 18]},
/// {"<": [{"var": "age"}, 65]}
/// ]
/// })).unwrap();
/// ```
///
/// Error handling:
/// ```rust
/// use datalogic_rs::Rule;
/// use serde_json::json;
///
/// let result = Rule::from_value(&json!({"invalid_op": []}));
/// assert!(result.is_err());
/// ```
///
/// See also: [`from_str`](Rule::from_str), [`apply`](Rule::apply)
pub fn from_value(value: &Value) -> Result<Self, Error> {
match value {
Value::Object(map) if map.len() == 1 => {
Expand Down Expand Up @@ -352,7 +397,6 @@ impl Rule {
}
}

#[inline(always)]
pub fn apply(&self, data: &Value) -> Result<Value, Error> {
match self {
Rule::Value(value) => {
Expand Down

0 comments on commit e97cae0

Please sign in to comment.