Crate rs_jsonnet

Crate rs_jsonnet 

Source
Expand description

§rs-jsonnet

A complete Rust implementation of Jsonnet 0.21.0 compatible with the Jsonnet specification. This crate provides a pure Rust implementation without external C dependencies, making it easy to embed in any Rust project.

§Features

  • Full Compatibility: Implements all features of Google Jsonnet v0.21.0.
  • Pure Rust: No C++ dependencies, ensuring memory safety and easy compilation.
  • Standard Library: Complete implementation of the Jsonnet standard library.
  • YAML Support: Optional feature to output YAML.

§Pure Kernel & Effects Shell Architecture

This crate follows the Pure Kernel/Effects Shell pattern:

  • Pure Kernel: PureEvaluator - performs deterministic Jsonnet evaluation without side effects.
  • Effects Shell: Evaluator - wraps the pure evaluator and handles I/O operations like import and importstr.

§Examples

§Basic Evaluation

use rs_jsonnet::{evaluate, JsonnetValue};

let code = r#"{ name: "Alice", age: 30 }"#;
let result = evaluate(code).unwrap();

match result {
    JsonnetValue::Object(obj) => {
        assert_eq!(obj.get("name"), Some(&JsonnetValue::String("Alice".to_string())));
        assert_eq!(obj.get("age"), Some(&JsonnetValue::Number(30.0)));
    },
    _ => panic!("Expected an object"),
}

§Evaluate to JSON

use rs_jsonnet::evaluate_to_json;

let code = r#"{ message: "Hello, " + self.name, name: "World" }"#;
let json_output = evaluate_to_json(code).unwrap();

assert!(json_output.contains(r#""message": "Hello, World""#));

Re-exports§

pub use error::JsonnetError;
pub use error::Result;
pub use evaluator::Evaluator;
pub use parser::Parser;
pub use value::JsonnetValue;
pub use pure_evaluator::PureEvaluator;

Modules§

ast
Abstract Syntax Tree definitions for Jsonnet. Abstract Syntax Tree for Jsonnet
error
Error types and Result wrapper for the crate. Error types for Jsonnet evaluation
eval
Evaluation-related components, including context and handlers. Evaluation components for Jsonnet
evaluator
The main Evaluator which handles I/O operations. Effects Shell Jsonnet evaluator
lexer
Lexical analyzer for tokenizing Jsonnet source code. Lexical analyzer for Jsonnet
parser
Parser for converting tokens into an Abstract Syntax Tree. Parser for Jsonnet AST
pure_evaluator
The pure, side-effect-free Jsonnet evaluator. Pure Jsonnet evaluator - no side effects, fully deterministic
runtime
Runtime components for handling external interactions. Runtime components for external function execution
stdlib
The Jsonnet standard library implementation. Jsonnet standard library implementation
value
Representation of Jsonnet values (JsonnetValue). Jsonnet value representation

Constants§

VERSION
Version information

Functions§

evaluate
Evaluate a Jsonnet snippet and return a JsonnetValue.
evaluate_to_json
Evaluate a Jsonnet snippet and format the result as a JSON string.
evaluate_with_filename
Evaluate a Jsonnet snippet with a specific filename for error reporting.