yard/
lib.rs

1//! Evaluate arithmetic operations of a string,
2//! based on the shunting yard algorithm.
3extern crate num;
4
5pub mod token;
6pub mod parser;
7pub mod evaluator;
8
9use std::str::FromStr;
10use num::Num;
11use parser::parse;
12use evaluator::eval;
13
14/// evaluate is a wrapper reducing the amount of code needed to process a string.
15/// #Example
16/// ```rust
17///
18/// fn main() {
19///     let code = "3 + 4";
20///     if let Ok(total) = yard::evaluate::<i32>(&code) {
21///         println!("{}", total);
22///     }
23/// }
24/// ```
25pub fn evaluate<T>(code: &str) -> Result<T, String> where T: Num + FromStr + Clone + Into<f64> {
26    match parse::<T>(code) {
27        Ok(tokens) => Ok(eval::<T>(&tokens)),
28        Err(e) => Err(e),
29    }
30}