nlcalc 0.1.0

Plugin-based natural language calculator for Rust and the command line
Documentation

NLCalc

A natural language calculator for Rust and the command line, with pluggable parsing and operations.

Supports arithmetic, units, percentages, dates and time zones, finance, algebra, statistics, and supplied physical models. See the guide for supported expressions and examples.

Rust

Add the crate to your project:

[dependencies]
nlcalc = "0.1"
use nlcalc::Calculator;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let answer = Calculator::extended().evaluate("5 ft 3 in to cm")?;
    println!("{}", answer.display); // 160.02 cm
    Ok(())
}

Calculator::extended() includes natural-language and model adapters; Calculator::standard() provides the standard expression parser. Results include display text and highlight spans as UTF-8 byte offsets into the input.

Use evaluate_with(input, &context) with nlcalc::Context to supply the clock, timezone, rates, or unit settings. The default timezone is UTC. Custom calculators can register implementations of nlcalc::Plugin through Calculator::empty(context).with_plugin(plugin).

Command line

Install with Cargo:

cargo install nlcalc
nlcalc 'is 97 prime'              # true
nlcalc 'Solve x^2 + 2x + 4 = 0'   # x = -1 ± i√3

Without an argument, NLCalc reads requests from stdin. Use single quotes for expressions containing dollar signs. Run nlcalc --help for options.

Currency rates are offline by default. Set NLCALC_RATES to a local rates JSON file, or use --online for cached daily ECB fiat rates via curl. Library callers supply rates through Context; the library does not fetch them.

Development

cargo test
cargo fmt --check
cargo clippy --all-targets -- -D warnings

MIT license.