Smart Calculator
Natural-language calculation for Rust.
smart-calculator takes a single string like sqrt(144), 3 km to m, time in tokyo, or 100 usd to inr and returns a structured result with the detected intent, raw value, formatted output, and optional metadata.
- Crate:
smart-calculator - docs.rs: https://docs.rs/smart-calculator
- crates.io: https://crates.io/crates/smart-calculator
What It Does
| Capability | Examples |
|---|---|
| Math | 2^10, 5!, 0xFF, square root of 144, 20% of 500 |
| Unit conversion | 3 km to m, 100 fahrenheit to celsius, 1 acre to m² |
| Time zones | time in tokyo, pst to est, 12:30 ist to london |
| Date queries | tomorrow, next friday, 1741000000, today to unix |
| Fiat currency | usd to eur, 100 dollars to rupees |
| Crypto | btc to usd, 100 usd to eth, bitcoin to inr |
Highlights
- Single async entrypoint:
calculate(input, options) - Natural-language parsing with intent detection
- Structured Rust types for results and configuration
- Locale-aware number formatting
- Online-first rate providers with static fallbacks
- No API keys required for built-in currency and crypto lookups
- Backed by 700+ imported compatibility tests
Quick Start
1. Add the crate
Install it from crates.io:
If your app does not already use an async runtime, add Tokio for the examples below:
Or add the dependencies manually to Cargo.toml:
[]
= "1.0.2"
= { = "1", = ["macros", "rt-multi-thread"] }
smart-calculator exposes an async API. You only need Tokio if your application does not already have an async runtime.
2. Call calculate
use calculate;
async
Example outputs
use calculate;
#
# async
API
calculate
pub async
Config
use Config;
let config = new
.with_timezone
.with_locale
.with_precision;
Available options:
with_rate_provider(...)to override fiat/crypto pricingwith_timezone(...)to control local time assumptionswith_locale(...)to control formattingwith_precision(...)to clamp significant digits from1..=21
CalculatorResult
ResultType can be:
MathUnitCurrencyCryptoTimeDate
Supported Inputs
Math
- Arithmetic:
+,-,*,/,% - Exponentiation:
^,** - Bitwise:
&,|,~,<<,>> - Constants:
pi,e,tau,phi,infinity - Functions:
sqrt,cbrt,abs,ceil,floor,round,log,log2,log10,ln,exp,sin,cos,tan,asin,acos,atan,sinh,cosh,tanh,pow,max,min - Literals: hex, binary, octal, scientific notation
- Natural language:
what is 5 + 3,factorial of 5,2 to the power of 10
Units
Supports conversions across:
- Length
- Mass
- Volume
- Area
- Temperature
- Speed
- Time
- Data
- Pressure
- Energy
- Power
- Angle
- Frequency
- Electric current
- Voltage
- Fuel economy
Examples:
1 light year to km100 celsius to fahrenheit1 gb to byte360 deg to revolution30 mpg to km/l
Time and Dates
time in londonutc to istmidnight utc to psttodayday after tomorrow2 weeks ago2025-03-03now to unixfrom 2025-01-01 to 2025-12-31
Fiat and Crypto
usd to inr100 dollars to eurosbtc to usd100 usd to bitcoinethereum to rupees
Built-in providers use multiple public endpoints and fall back to static reference rates when network providers fail.
Custom Rate Providers
You can inject your own provider by implementing RateProvider.
use async_trait;
use RateProvider;
use Error;
;
Project Shape
| Path | Purpose |
|---|---|
src/lib.rs |
public entrypoint |
src/parser/ |
intent detection and normalization |
src/evaluators/ |
math, unit, time, date, currency, crypto evaluation |
src/provider/ |
live and static rate providers |
src/data/ |
unit, currency, timezone, and math data |
tests/ |
imported compatibility suites by category |
Development
Run the full test suite:
Format the codebase:
Notes
- This repository is library-first. The binary target is currently a placeholder and not a CLI interface.
- Empty input returns an error.
- Currency and crypto conversions are async because they may hit network providers.
- Formatting is locale-aware, but parsing is driven by the normalized input language rules in the parser.
License
MIT