1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! [balena] **tem**plating **en**gine
//!
//! A crate that provides facilities to:
//!
//! * parse an expression
//! * evaluate an expression
//! * evaluate an expression as a boolean (logical expressions)
//! * register custom functions and filters
//!
//! [Expression language documentation].
//!
//! # Versioning
//!
//! This crate is being actively developed and it does NOT follow [Semantic Versioning] yet.
//! It will follow semantic versioning when it reaches version 1.0.
//!
//! MINOR version changes denotes incompatible API changes and PATCH version changes denotes
//! both new functionality in a backwards-compatible manner and backwards-compatible bug fixes.
//!
//! # Examples
//!
//! ## Helpers
//!
//! ### JSON evaluation
//!
//! ```rust
//! use balena_temen::{evaluate, Value};
//! use serde_json::json;
//!
//! let data = json!({
//!     "wifi": {
//!         "ssid": "Balena Ltd",
//!         "id": {
//!             "$$eval": "super.ssid | slugify"
//!         }
//!     }
//! });
//! let evaluated = json!({
//!     "wifi": {
//!         "ssid": "Balena Ltd",
//!         "id": "balena-ltd"
//!     }
//! });
//! assert_eq!(evaluate(data).unwrap(), evaluated);
//! ```
//!
//! ### JSON with custom evaluation keyword
//!
//! ```rust
//! use balena_temen::{Context, Engine, EngineBuilder, evaluate_with_engine, Value};
//! use serde_json::json;
//!
//! let data = json!({
//!     "evalMePlease": "3 + 5 * 2"
//! });
//! let evaluated = json!(13);
//!
//! let mut ctx = Context::default();
//! let engine: Engine = EngineBuilder::default()
//!     .eval_keyword("evalMePlease")
//!     .into();
//!
//! assert_eq!(evaluate_with_engine(data, &engine, &mut ctx).unwrap(), evaluated);
//! ```
//!
//! ## Intermediate
//!
//! ### Single expression evaluation
//!
//! ```rust
//! use balena_temen::{
//!     ast::Identifier,
//!     Engine, Context, Value
//! };
//! use serde_json::json;
//!
//! let engine = Engine::default();       // Default functions, filters
//! let mut ctx = Context::default();     // Default context
//! let position = Identifier::default(); // Evaluate from the root
//! let data = Value::Null;
//!
//! assert_eq!(
//!     engine.eval("2 + 3", &position, &data, &mut ctx).unwrap(),
//!     json!(5)
//! );
//!
//! assert_eq!(
//!     engine.eval("`Balena templating engine!` | slugify", &position, &data, &mut ctx).unwrap(),
//!     json!("balena-templating-engine")
//! );
//! ```
//!
//! ### Single logical expression evaluation
//!
//! ```rust
//! use balena_temen::{
//!     ast::Identifier,
//!     Engine, Context, Value
//! };
//! use serde_json::json;
//!
//! let engine = Engine::default();       // Default functions, filters
//! let mut ctx = Context::default();     // Default context
//! let position = Identifier::default(); // Evaluate from the root
//! let data = Value::Null;
//!
//! assert_eq!(
//!     engine.eval_as_bool("1 == 1", &position, &data, &mut ctx).unwrap(),
//!     json!(true)
//! );
//!
//! assert_eq!(
//!     engine.eval_as_bool("2 > 4", &position, &data, &mut ctx).unwrap(),
//!     json!(false)
//! );
//!
//! assert_eq!(
//!     engine.eval_as_bool("1 > 4 or 3 > 2", &position, &data, &mut ctx).unwrap(),
//!     json!(true)
//! );
//! ```
//!
//! ## Advanced
//!
//! ### Custom functions and filters
//!
//! Visit [`EngineBuilder::function`] and [`EngineBuilder::filter`] methods
//! documentation for examples.
//!
//! ### Expression parsing
//!
//! ```rust
//! use balena_temen::ast::*;
//!
//! // Parse expression automatically
//! let parsed: Expression = "1 + 2 == 3".parse().unwrap();
//!
//! // Construct expression manually
//! let lhs = Expression::new(
//!     ExpressionValue::Math(
//!         MathExpression::new(
//!             Expression::new(ExpressionValue::Integer(1)),
//!             Expression::new(ExpressionValue::Integer(2)),
//!             MathOperator::Addition
//!         )
//!     )
//! );
//! let rhs = Expression::new(
//!     ExpressionValue::Integer(3)
//! );
//! let full_expression = Expression::new(
//!     ExpressionValue::Logical(
//!         LogicalExpression::new(
//!             lhs, rhs, LogicalOperator::Equal
//!         )
//!     )
//! );
//!
//! // Compare and check that the expression was parsed correctly
//! assert_eq!(parsed, full_expression);
//! ```
//!
//! Visit [ast] module documentation for more info.
//!
//! ### Identifier parsing
//!
//! ```rust
//! use balena_temen::ast::*;
//!
//! let s = "wifi.networks[0].ssid";
//!
//! // Parse it
//! let parsed: Identifier = s.parse().unwrap();
//!
//! // Create manually
//! let manual = Identifier::default()
//!     .name("wifi")
//!     .name("networks")
//!     .index(0)
//!     .name("ssid");
//!
//! assert_eq!(parsed, manual);
//! ```
//!
//! [balena]: https://www.balena.io
//! [ast]: ast/index.html
//! [`EngineBuilder::function`]: struct.EngineBuilder.html#method.function
//! [`EngineBuilder::filter`]: struct.EngineBuilder.html#method.filter
//! [Semantic Versioning]: https://semver.org/
//! [Expression language documentation]: https://github.com/balena-io-modules/balena-temen/blob/master/docs/expression.md
#![allow(clippy::possible_missing_comma)]

pub use serde_json::Value;

pub use crate::{
    builtin::{
        filter::FilterFn,
        function::FunctionFn,
    },
    context::Context,
    engine::{
        builder::EngineBuilder,
        Engine,
        helper::{
            evaluate,
            evaluate_with_engine
        }
    }
};

#[allow(deprecated)]
pub use crate::engine::helper::{eval, eval_with_engine};

pub(crate) mod builtin;

pub mod ast;
mod context;
mod engine;
pub mod error;
mod parser;
mod utils;