resolver
Source
This work is a derivative of this repository: https://github.com/fengcen/eval
The aforementioned repository has been abandoned, hence the reason for this repository/crate.
Features
Operators
| Operator | Priority | Description | Example |
|---|---|---|---|
** |
12 | Exponentiation (left-assoc) | 2 ** 10 |
-x ~x |
11 | Unary negation / bitwise NOT | -5, ~0xFF |
* / % |
10 | Multiplicative | 10 % 3 |
<< >> |
9 | Bit shifts | 1 << 4 |
+ - |
8 | Additive | 2 + 3 |
& |
7 | Bitwise AND | 12 & 10 |
== != > < >= <= in not in |
6 | Comparison / membership | x in arr |
^ |
5 | Bitwise XOR | 12 ^ 10 |
&& |
4 | Logical AND (short-circuit) | a && b |
| |
3 | Bitwise OR | 12 | 10 |
|| |
2 | Logical OR (short-circuit) | a || b |
?? |
1 | Null-coalescing | value ?? "default" |
! |
— | Logical NOT (prefix) | !flag |
n..m |
— | Integer range | 0..5 |
() [] . |
— | Grouping, indexing, field access | obj.foo[0] |
"" '' |
— | String literals | "hello" |
Built-in Functions
Conditional:
| Function | Description |
|---|---|
if(cond, then, else) |
Short-circuit conditional — only evaluates the matching branch |
Collections:
| Function | Description |
|---|---|
min(a, b, ...) |
Minimum value |
max(a, b, ...) |
Maximum value |
len(x) |
Length of string, array, or object |
is_empty(x) |
True if string/array/object is empty or value is null |
array(a, b, ...) |
Create an array |
sort(arr) |
Sort array of numbers or strings (ascending) |
reverse(arr) |
Reverse an array |
unique(arr) |
Remove duplicate elements (preserves first occurrence) |
index_of(arr, val) |
Index of first match, or -1 if not found |
any(arr, val) |
True if any element equals val |
all(arr, val) |
True if every element equals val |
keys(obj) |
Array of object keys |
values(obj) |
Array of object values |
Math:
| Function | Description |
|---|---|
abs(x) |
Absolute value |
floor(x) |
Floor (round down) |
ceil(x) |
Ceiling (round up) |
round(x) |
Round to nearest integer (half-away-from-zero) |
sqrt(x) |
Square root |
pow(base, exp) |
Exponentiation (also available as **) |
clamp(x, min, max) |
Clamp x to [min, max] |
log(x) |
Natural logarithm (ln) |
log2(x) |
Base-2 logarithm |
log10(x) |
Base-10 logarithm |
String:
| Function | Description |
|---|---|
contains(haystack, needle) |
Check if string/array/object contains value |
starts_with(s, prefix) |
Check if string starts with prefix |
ends_with(s, suffix) |
Check if string ends with suffix |
upper(s) |
Convert to uppercase |
lower(s) |
Convert to lowercase |
trim(s) |
Trim leading/trailing whitespace |
replace(s, from, to) |
Replace all occurrences of from with to |
split(s, delim) |
Split string into an array |
join(arr, delim) |
Join array elements into a string |
format(tmpl, ...) |
Replace {} placeholders with arguments in order |
Type checking:
| Function | Description |
|---|---|
is_null(x) |
Check if value is null |
is_number(x) |
Check if value is a number |
is_string(x) |
Check if value is a string |
is_array(x) |
Check if value is an array |
type_of(x) |
Returns "null", "bool", "number", "string", "array", or "object" |
Type conversion:
| Function | Description |
|---|---|
int(x) |
Convert to integer (string, number, or bool) |
float(x) |
Convert to float (string, number, or bool) |
str(x) |
Convert to string |
Where can resolver be used?
- Template engine
- Configuration with computed values
- User-defined filters / rules
- ...
Usage
Add dependency to Cargo.toml
[]
= "^0.1"
In your main.rs or lib.rs:
extern crate resolver;
Examples
You can do mathematical calculations with supported operators:
use ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
You can eval with context:
use ;
assert_eq!;
You can access data like javascript by using . and []. [] supports expression.
use ;
use HashMap;
let mut object = new;
object.insert;
assert_eq!;
You can eval with function:
use ;
assert_eq!;
You can create an array with array():
use ;
assert_eq!;
You can create an integer array with n..m:
use ;
assert_eq!;
Null-coalescing lets you provide defaults for missing values:
use ;
assert_eq!;
The in / not in operators check membership in arrays, objects, or strings:
use ;
assert_eq!;
assert_eq!;
assert_eq!;
Use if() for conditional expressions (only the matching branch is evaluated):
use ;
assert_eq!;
Bitwise operators and shifts work on integers:
use ;
assert_eq!;
assert_eq!;
assert_eq!;
String utilities:
use ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Array utilities:
use ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
License
resolver is under the terms of the MIT license.
See LICENSE for details.