| Crates.io | bexeval |
| lib.rs | bexeval |
| version | 0.1.2 |
| created_at | 2025-01-24 04:32:19.663003+00 |
| updated_at | 2025-01-26 05:19:39.376835+00 |
| description | bexeval is a Rust crate for evaluating string formulas restricted to values as integers only. |
| homepage | |
| repository | https://github.com/taka8t/bexeval |
| max_upload_size | |
| id | 1529049 |
| size | 47,323 |
Bexeval is a Rust crate for evaluating mathematical expressions with integer numeric types written as strings.
+, -, *, /, %, &, |, ^, <<, >>, !(operator not)==, !=, <, <=, >, >=pow, count_ones, abs...u8, u16, u32, u64, usize, i8, i16, i32, i64, isizenum-traits).Add the bexeval crate to your Cargo.toml file:
[dependencies]
bexeval = "<version>"
Then, in your Rust code:
use bexeval::*;
let parser = Parser::<i32>::new();
assert_eq!(parser.eval("1 + 2 * max(3, 4) - (8 ^ 5)").unwrap(), 1 + 2 * 3.max(4) - (8 ^ 5));
assert_eq!(parser.eval_context("1 + x", &[("x", 5)]).unwrap(), 1 + 5);
assert_eq!(parser.eval(&format!("1 + {}", 5)).unwrap(), 1 + 5);
The context argument stores information about the variable in array of tuples(&str, T).
Wrapping operations are used for operations that may overflow. (wrapping_add, wrapping_mul...)
Be careful of overflow.
Dividing by zero causes panic.
You can assign numerical values to variables and evaluate them using Context.
# use bexeval::*;
#
let mut ctx = Context::<u8>::new();
ctx.assign("x", 1);
assert_eq!(ctx.eval("x + 3").unwrap(), 4);
ctx.assign_stmt("y = x + 5").unwrap();
assert_eq!(ctx.eval("y + x * 2").unwrap(), 8);
// wrapping operation
assert_eq!(ctx.eval("y + x * (pow(2, 8) - 1)").unwrap(), 5);
powabsabs_diff (Available even with rustc versions below 1.60)count_onescount_zerosleading_zerostrailing_zerosrotate_leftrotate_rightDetailed API documentation can be found here.
This project is licensed under the MIT license.