Skip to main content

Crate async_jsonata_rust

Crate async_jsonata_rust 

Source
Expand description

Async-first JSONata crate for Rust.

This crate exposes a stable public surface focused on:

  • JSONata parsing (Parser, Expression)
  • function registry management (FunctionRegistry)
  • async custom functions (types::JsonCallable / types::JsonataCallable)
  • evaluator facade (Evaluator, runtime parity in progress)

JSONata language reference:

§Quick Start

use async_jsonata_rust::Parser;

let parser = Parser::new();
let expression = parser.parse("Account.Order[0].Product")?;
assert_eq!(expression.ast()["type"], "path");

§JSONata syntax

async_jsonata_rust follows JSONata syntax from the official docs and upstream test-suite. Parser support is production-oriented, while evaluator parity is declared separately in docs/compatibility.md and crate README.

§Async functions

use std::any::Any;
use std::sync::Arc;

use futures::executor::block_on;
use futures::future::BoxFuture;
use async_jsonata_rust::functions::core;
use async_jsonata_rust::types::{FunctionContext, JsonArray, JsonCallable, JsonError, JsonFunction, JsonValue};

#[derive(Clone)]
struct DoubleCallable;

impl JsonCallable for DoubleCallable {
    fn call(&self, _ctx: FunctionContext, args: Vec<JsonValue>) -> BoxFuture<'static, Result<JsonValue, JsonError>> {
        let input = args.first().cloned().unwrap_or(JsonValue::Undefined);
        Box::pin(async move {
            if let JsonValue::Number(value) = input {
                return Ok(JsonValue::Number(value * 2.0));
            }
            Ok(JsonValue::Undefined)
        })
    }

    fn arity(&self) -> Option<usize> {
        Some(1)
    }

    fn as_any(&self) -> &(dyn Any + Send + Sync) {
        self
    }
}

let input = JsonValue::Array(JsonArray::new(
    vec![JsonValue::Number(1.0), JsonValue::Number(2.0)],
    true,
    false,
));
let func = JsonValue::Function(JsonFunction::new(Arc::new(DoubleCallable)));
let out = block_on(core::map(FunctionContext::empty(), input, func)).unwrap();
assert!(matches!(out, JsonValue::Array(_)));

§Errors

Stable APIs return unified Error with JSONata-style code (S0201, D3040, …) and structured context fields.

Re-exports§

pub use api::Evaluator;
pub use api::Expression;
pub use api::FunctionRegistry;
pub use api::Parser;
pub use error::Error;
pub use parser::parse_expression;
pub use parser::AstNode;
pub use parser::Parser as LowLevelParser;
pub use parser::ParserError;
pub use parser::Token;
pub use parser::TokenKind;
pub use parser::Tokenizer;
pub use registry::create_builtin_registry;
pub use registry::lookup_builtin;
pub use types::JsonArray;
pub use types::JsonCallable;
pub use types::JsonError;
pub use types::JsonFunction;
pub use types::JsonObject;
pub use types::JsonValue;
pub use types::JsonataArray;
pub use types::JsonataCallable;
pub use types::JsonataFocus;
pub use types::JsonataFunction;
pub use types::JsonataObject;
pub use types::JsonataValue;
pub use types::NativeRef;
pub use types::NativeType;

Modules§

api
error
functions
parser
Low-level JSONata parser module.
registry
types