frequenz_microgrid_formula_engine/lib.rs
1// License: MIT
2// Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3
4/*!
5# frequenz-formula-engine-rs
6
7A library to create formulas over streamed data
8
9## Usage
10
11A [`FormulaEngine`] instance can be created from a [`String`] formula with the [`try_new`][`FormulaEngine::try_new`] method.
12Such a formula can contain component placeholders, which are represented by `#`
13followed by a number.
14To calculate the formula, you need to provide an iterator of Option values,
15where
16- `None` represents a missing value
17- `Some(value)` represents a value.
18
19The iterator must contain as many values as the formula has placeholders.
20The result of the calculation is an Option value.
21
22```rust
23use frequenz_microgrid_formula_engine::{FormulaEngine, FormulaError};
24use std::collections::HashMap;
25
26fn main() -> Result<(), FormulaError> {
27 let fe = FormulaEngine::try_new("#0 + #1")?;
28 let components = fe.components();
29 assert_eq!(components, &[0, 1].into_iter().collect());
30 assert_eq!(fe.calculate(&HashMap::from([(0, Some(1.)), (1, Some(2.))]))?, Some(3.0));
31 Ok(())
32}
33```
34*/
35
36mod error;
37mod expression;
38mod formula_engine;
39mod parser;
40pub mod traits;
41
42pub use error::FormulaError;
43pub use formula_engine::FormulaEngine;
44
45#[cfg(test)]
46mod tests;