Format code and specify exports

This commit is contained in:
Sebastian Schmidt 2019-03-18 18:02:45 +02:00
parent b3a616c39a
commit 22d0d2c3d0
4 changed files with 47 additions and 21 deletions

View File

@ -1,6 +1,6 @@
use crate::{error::Error, value::Value}; use crate::value::Value;
use std::collections::HashMap;
use function::Function; use function::Function;
use std::collections::HashMap;
pub trait Configuration { pub trait Configuration {
fn get_value(&self, identifier: &str) -> Option<&Value>; fn get_value(&self, identifier: &str) -> Option<&Value>;
@ -11,11 +11,11 @@ pub trait Configuration {
pub struct EmptyConfiguration; pub struct EmptyConfiguration;
impl Configuration for EmptyConfiguration { impl Configuration for EmptyConfiguration {
fn get_value(&self, identifier: &str) -> Option<&Value> { fn get_value(&self, _identifier: &str) -> Option<&Value> {
None None
} }
fn get_function(&self, identifier: &str) -> Option<&Function> { fn get_function(&self, _identifier: &str) -> Option<&Function> {
None None
} }
} }

View File

@ -1,28 +1,34 @@
use configuration::{EmptyConfiguration, Configuration};
use error::Error;
use value::Value;
mod configuration; mod configuration;
mod error; mod error;
mod function;
mod operator; mod operator;
mod token; mod token;
mod tree; mod tree;
mod value; mod value;
mod function;
// Exports
pub use configuration::{Configuration, EmptyConfiguration, HashMapConfiguration};
pub use error::Error;
pub use function::Function;
pub use value::Value;
pub fn eval(string: &str) -> Result<Value, Error> { pub fn eval(string: &str) -> Result<Value, Error> {
tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval(&EmptyConfiguration) tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval(&EmptyConfiguration)
} }
pub fn eval_with_configuration(string: &str, configuration: &Configuration) -> Result<Value, Error> { pub fn eval_with_configuration(
string: &str,
configuration: &Configuration,
) -> Result<Value, Error> {
tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval(configuration) tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval(configuration)
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::{eval, value::Value}; use crate::{eval, value::Value};
use error::Error;
use configuration::HashMapConfiguration; use configuration::HashMapConfiguration;
use error::Error;
use eval_with_configuration; use eval_with_configuration;
#[test] #[test]
@ -31,7 +37,10 @@ mod test {
assert_eq!(eval("3.3"), Ok(Value::Float(3.3))); assert_eq!(eval("3.3"), Ok(Value::Float(3.3)));
assert_eq!(eval("true"), Ok(Value::Boolean(true))); assert_eq!(eval("true"), Ok(Value::Boolean(true)));
assert_eq!(eval("false"), Ok(Value::Boolean(false))); assert_eq!(eval("false"), Ok(Value::Boolean(false)));
assert_eq!(eval("blub"), Err(Error::IdentifierNotFound("blub".to_string()))); assert_eq!(
eval("blub"),
Err(Error::IdentifierNotFound("blub".to_string()))
);
assert_eq!(eval("-3"), Ok(Value::Int(-3))); assert_eq!(eval("-3"), Ok(Value::Int(-3)));
assert_eq!(eval("-3.6"), Ok(Value::Float(-3.6))); assert_eq!(eval("-3.6"), Ok(Value::Float(-3.6)));
assert_eq!(eval("----3"), Ok(Value::Int(3))); assert_eq!(eval("----3"), Ok(Value::Int(3)));
@ -117,12 +126,30 @@ mod test {
configuration.insert_variable("half".to_string(), Value::Float(0.5)); configuration.insert_variable("half".to_string(), Value::Float(0.5));
configuration.insert_variable("zero".to_string(), Value::Int(0)); configuration.insert_variable("zero".to_string(), Value::Int(0));
assert_eq!(eval_with_configuration("tr", &configuration), Ok(Value::Boolean(true))); assert_eq!(
assert_eq!(eval_with_configuration("fa", &configuration), Ok(Value::Boolean(false))); eval_with_configuration("tr", &configuration),
assert_eq!(eval_with_configuration("tr && false", &configuration), Ok(Value::Boolean(false))); Ok(Value::Boolean(true))
assert_eq!(eval_with_configuration("five + six", &configuration), Ok(Value::Int(11))); );
assert_eq!(eval_with_configuration("five * half", &configuration), Ok(Value::Float(2.5))); assert_eq!(
assert_eq!(eval_with_configuration("five < six && true", &configuration), Ok(Value::Boolean(true))); eval_with_configuration("fa", &configuration),
Ok(Value::Boolean(false))
);
assert_eq!(
eval_with_configuration("tr && false", &configuration),
Ok(Value::Boolean(false))
);
assert_eq!(
eval_with_configuration("five + six", &configuration),
Ok(Value::Int(11))
);
assert_eq!(
eval_with_configuration("five * half", &configuration),
Ok(Value::Float(2.5))
);
assert_eq!(
eval_with_configuration("five < six && true", &configuration),
Ok(Value::Boolean(true))
);
} }
#[test] #[test]

View File

@ -21,7 +21,6 @@ pub trait Operator {
} }
pub struct RootNode; pub struct RootNode;
pub struct Braced;
pub struct Add; pub struct Add;
pub struct Sub; pub struct Sub;