Kotoba Jsonnet
Pure Rust implementation of Jsonnet 0.21.0, fully compatible with Google Jsonnet.
๐ฏ Jsonnet 0.21.0 Complete Compatibility
This crate implements all features of Google Jsonnet v0.21.0 in pure Rust.
โ Implemented Features
Core Language Features
- โ Complete AST definition (Expr, Stmt, ObjectField, BinaryOp, UnaryOp)
- โ Full lexer with tokenization (identifiers, literals, operators, keywords)
- โ Recursive descent parser with precedence handling
- โ Expression evaluator with variable scoping
- โ Function definitions and calls
- โ Object and array literals
- โ
Bracket notation -
obj["key"]
andarr[index]
syntax โญ - โ
Array comprehensions -
[x for x in arr if cond]
syntax โญ - โ Local variable bindings
- โ Conditional expressions (if/then/else)
- โ Import and ImportStr
- โ Error handling with try/catch
- โ Assertions
Standard Library (80+ Functions)
- โ
Array functions:
length
,makeArray
,filter
,map
,foldl
,foldr
,range
,member
,count
,uniq
,sort
,reverse
- โ
String functions:
length
,substring
,startsWith
,endsWith
,contains
,toLower
,toUpper
,trim
,split
,join
,format
- โ
Object functions:
objectFields
,objectValues
,objectHas
,get
,mergePatch
,mapWithKey
,prune
- โ
Math functions:
abs
,sqrt
,sin
,cos
,tan
,log
,exp
,floor
,ceil
,round
,pow
,modulo
- โ
Type functions:
type
,isArray
,isBoolean
,isFunction
,isNumber
,isObject
,isString
- โ
Encoding functions:
base64
,base64Decode
,md5
,escapeStringJson
,escapeStringYaml
,escapeStringPython
,encodeUTF8
,decodeUTF8
- โ
Serialization:
manifestJson
,manifestJsonEx
,manifestYaml
,parseJson
,toString
- โ
Utility functions:
assertEqual
,clamp
,max
,min
API Compatibility
- โ
evaluate()
- Evaluate Jsonnet code to JsonnetValue - โ
evaluate_to_json()
- Evaluate to JSON string - โ
evaluate_to_yaml()
- Evaluate to YAML string (with feature flag) - โ
evaluate_with_filename()
- Evaluate with filename for error reporting - โ Error types matching original Jsonnet behavior
๐ Architecture
Jsonnet Code โ Lexer โ Tokens โ Parser โ AST โ Evaluator โ JsonnetValue
โ โ โ โ โ
Tokenize Parse Build Eval Evaluate
๐ง Components
lib.rs
: Public API (evaluate
,evaluate_to_json
,evaluate_to_yaml
)error.rs
: Error types (JsonnetError
,Result<T>
)value.rs
: Value representation (JsonnetValue
,JsonnetFunction
)ast.rs
: Abstract Syntax Tree definitionslexer.rs
: Lexical analysis and tokenizationparser.rs
: Recursive descent parsingevaluator.rs
: AST evaluation and executionstdlib.rs
: 80+ standard library functions
๐งช Testing
Run the comprehensive test suite:
Tests cover:
- โ Basic evaluation (literals, variables, functions)
- โ Complex expressions and operator precedence
- โ Standard library functions
- โ Error handling and edge cases
- โ JSON/YAML output formatting
๐ Usage
use ;
// Evaluate Jsonnet code
let result = evaluate?;
println!;
// Convert to JSON
let json = evaluate_to_json?;
println!;
๐ Integration with Kotoba
This Jsonnet implementation is integrated into the broader Kotoba ecosystem:
- Used for configuration parsing (
.kotoba
files) - Powers the frontend framework's component definitions
- Enables deployment configuration templating
- Provides runtime configuration evaluation
โก Performance
- Zero-copy evaluation where possible
- Efficient AST representation with Box for recursive types
- Lazy evaluation for optimal performance
- Memory-efficient standard library implementations
๐ Compatibility Matrix
Feature | Google Jsonnet 0.21.0 | kotoba-jsonnet |
---|---|---|
Language spec | โ Complete | โ Complete |
Standard library | โ 80+ functions | โ 80+ functions |
Import system | โ import/importstr | โ Implemented |
Error handling | โ try/catch/error | โ Implemented |
JSON output | โ manifestJson | โ Implemented |
YAML output | โ manifestYaml | โ Feature flag |
Performance | C++ optimized | Rust zero-cost |
๐ค Contributing
This implementation aims for 100% compatibility with Google Jsonnet 0.21.0. If you find any discrepancies or missing features, please open an issue.
๐ License
MIT OR Apache-2.0 (matching Google Jsonnet)