Babbage · Developer guide

Errors and guarantees

Errors

Every fallible call returns Error, which implements std::error::Error and Display. The messages are written for the person typing the line.

use babbage_core::{Error, evaluate};

assert_eq!(evaluate("1 / 0").unwrap_err(), Error::DivisionByZero);
assert_eq!(evaluate("5 kg in m").unwrap_err(), Error::IncompatibleUnits);
assert_eq!(
    evaluate("5 kg in m").unwrap_err().to_string(),
    "these units measure different things"
);
assert_eq!(evaluate("10 EUR in USD").unwrap_err(), Error::CurrencyRateNeeded);
assert_eq!(evaluate("2 ^ 3000000").unwrap_err(), Error::Overflow);

The last one is on purpose. Babbage fetches no exchange rates and never guesses one: a conversion between currencies needs a rate in the line (10 EUR in USD at 1.08 USD/EUR), and without one it is an error. Match on Error with a catch-all arm, so a variant added later does not break your build.

Exactness

What 1.x promises