metrics_evaluation
metrics_evaluation is a simple text parser that evaluates a given logic against a Resolver (which resolves names to values) resulting in a single bool-result. It is possible to perform simple arithmetic operations on the values before the comparison takes place.
It can be used for example to evaluate named IoT-metrics against a given logic to trigger or not to trigger an action if the result of evaluate results to true. The format of the input is equal to the if-style of rust (no initial braces needed). Arithmetics are limited to + (add), - (sub), * (mul) and / (div) at the moment.
It is possible to evaluate comparisons of variables against fixed values or variables against other variables. Anyway comparisons are limited to always have a variable-name on the left hand at the moment (WIP).
Usage
Add this crate to your cargo.tml
use metrics_evaluation::{parse_tree, solve_tree, Resolver};
Implement a Resolver that can deliver a Value-reference for the give variable-name-lookup (or none if there is no value for this). For the ease of use and pure laziness there is a MapResolver wich is a Resolver and can be formed from HashMap<K: AsRef<str>, V: Into<Value>> (see tests).
Give this Resolver to the evaluate function and call it with a simple text-evaluation as you would do to check if a given value evaluates to true|false in rust. For example: foo + 2 == 42 && bar < 2 || (baz == true && baz + "30sec" >= "42min").
The following operators are supported and behave like in rust:
><>=<===!=
The following logics are supported and behave like in rust:
and,&&or,||
The following arithmetic operators are supported but differ in implementation for different Value-Types:
+(add)-(sub)*(mul)/(div)
The following [Value]s can be compared:
Value::Numeric- maps internally to a f64 and hasFrom-implementations ranging fromu8tof64Value::String- a string literal which must be always encapsulated by quotation marks.Value::Time- maps a NaiveTime and the string-representation must match "%H:%M:%S". Must be always encapsulated by quotation marks.Value::Durationa string that is a humantime representing a duration. Must be always encapsulated by quotation marks.
The general form of a comparison is [Name] [Arithmetic] [Comparison-Operator] Value [Arithmetic] [[Logic]...].
Feature-flags
async- additionally have [AsyncResolver] and [AsyncSolver] over [Resolver] and [Solver] for cases a [Resolver] needs async functionality (async database for example). Useevaluate_async' in this case.MapResolver` is only available in test-configuration here (as it makes no sense to have such in production).
Easy example
See solver tests (end of file) or use it like this:
use HashMap;
use Result;
use ;
License
metrics_evaluation is distributed under the terms the MIT license.
See LICENSE for details.