https://github.com/danielgerlag/powerfx-rust
Power Fx interpreter for Rust
https://github.com/danielgerlag/powerfx-rust
Last synced: 3 months ago
JSON representation
Power Fx interpreter for Rust
- Host: GitHub
- URL: https://github.com/danielgerlag/powerfx-rust
- Owner: danielgerlag
- License: mit
- Created: 2023-09-16T00:07:53.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2025-02-14T16:29:45.000Z (over 1 year ago)
- Last Synced: 2025-06-18T05:16:50.493Z (12 months ago)
- Language: Rust
- Homepage:
- Size: 21.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Power Fx interpreter for Rust
This crate provides an embedded [Power Fx](https://learn.microsoft.com/en-us/power-platform/power-fx/overview) interpreter for Rust projects.
## Getting started
Install the package.
```shell
cargo add powerfx
```
## Testing
Run all tests:
```shell
cargo test
```
The project includes two types of tests:
1. **Unit tests** (`src/tests/mod.rs`) - 37 Rust-based tests
2. **Expression tests** (`tests/expression_tests/`) - 364 txt-based tests in a format similar to Microsoft's Power-Fx test format
### Adding Expression Tests
Create a `.txt` file in `tests/expression_tests/` with this format:
```
// Comments start with //
>> 1 + 2
3
>> Upper('hello')
"HELLO"
```
## Status
This library is still in an alpha status. The following functions have been implemented:
**Table Functions:**
- Table, First, Last, Index, Filter, CountRows
- LookUp, Sort, CountIf, ForAll
- Sequence, Distinct, FirstN, LastN, Shuffle, Reverse
- AddColumns, DropColumns, RenameColumns, ShowColumns, SortByColumns
**Context:**
- Set
**Logical:**
- If, And, Or, Not, Coalesce
- IsBlank, Blank, IsEmpty
- Switch, IfError, With, Boolean
- IsBoolean, IsText, IsNumber, IsDate, IsTable, IsRecord
**Math:**
- Abs, Sqrt, Round, RoundUp, RoundDown
- Int, Trunc, Mod, Power
- Rand, RandBetween, Exp, Ln, Log
- Sin, Cos, Tan, Asin, Acos, Atan, Atan2
- Pi, Floor, Ceiling, Sign, Degrees, Radians
- IsNumeric, Dec2Hex, Hex2Dec
**Text:**
- Left, Mid, Right, Upper, Lower, Len
- Concat, Concatenate
- Trim, TrimEnds, StartsWith, EndsWith
- Find, Replace, Substitute
- Value, Text, Char, Exact, Proper
- Split, Rept, EncodeUrl, PlainText
- HashTags, JSON, GUID
**Date/Time:**
- Now, Today, Date, Year, Month, Day
- Weekday, DateAdd, DateDiff, IsToday
- EOMonth, IsBetween, Calendar
**Aggregation:**
- Average, Sum, Min, Max
- StdevP, VarP, CountA
## Examples
The following example illustrates adding two constant numbers.
```rust
use powerfx::{DataValue, PowerFxEngine};
fn main() {
let engine = PowerFxEngine::new();
let result = engine.evaluate("2 + 3", None).unwrap();
assert_eq!(result, DataValue::Number(5.0));
}
```
This can also be done with variables.
```rust
let engine = PowerFxEngine::new();
let mut session = Session::new();
session.set_variable("a", DataValue::Number(2.0));
session.set_variable("b", DataValue::Number(3.0));
let result = engine.evaluate("a + b", Some(&mut session)).unwrap();
assert_eq!(result, DataValue::Number(5.0));
```
For more examples, please see the [Examples Folder](./examples/)