From 22d0d2c3d0c08ae5be99bdc94fed222d2b0a344c Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Mon, 18 Mar 2019 18:02:45 +0200 Subject: [PATCH] Format code and specify exports --- src/configuration/mod.rs | 10 ++++---- src/function.rs | 2 +- src/lib.rs | 55 ++++++++++++++++++++++++++++++---------- src/operator/mod.rs | 1 - 4 files changed, 47 insertions(+), 21 deletions(-) diff --git a/src/configuration/mod.rs b/src/configuration/mod.rs index 048c190..5ede6b2 100644 --- a/src/configuration/mod.rs +++ b/src/configuration/mod.rs @@ -1,6 +1,6 @@ -use crate::{error::Error, value::Value}; -use std::collections::HashMap; +use crate::value::Value; use function::Function; +use std::collections::HashMap; pub trait Configuration { fn get_value(&self, identifier: &str) -> Option<&Value>; @@ -11,11 +11,11 @@ pub trait Configuration { pub struct EmptyConfiguration; impl Configuration for EmptyConfiguration { - fn get_value(&self, identifier: &str) -> Option<&Value> { + fn get_value(&self, _identifier: &str) -> Option<&Value> { None } - fn get_function(&self, identifier: &str) -> Option<&Function> { + fn get_function(&self, _identifier: &str) -> Option<&Function> { None } } @@ -50,4 +50,4 @@ impl Configuration for HashMapConfiguration { fn get_function(&self, identifier: &str) -> Option<&Function> { self.functions.get(identifier) } -} \ No newline at end of file +} diff --git a/src/function.rs b/src/function.rs index 8af8061..cb3bdd6 100644 --- a/src/function.rs +++ b/src/function.rs @@ -4,4 +4,4 @@ use value::Value; pub struct Function { parameter_amount: usize, function: fn() -> Result, // TODO continue type -} \ No newline at end of file +} diff --git a/src/lib.rs b/src/lib.rs index 523c636..13e8d8d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,28 +1,34 @@ -use configuration::{EmptyConfiguration, Configuration}; -use error::Error; -use value::Value; - mod configuration; mod error; +mod function; mod operator; mod token; mod tree; 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 { tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval(&EmptyConfiguration) } -pub fn eval_with_configuration(string: &str, configuration: &Configuration) -> Result { +pub fn eval_with_configuration( + string: &str, + configuration: &Configuration, +) -> Result { tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval(configuration) } #[cfg(test)] mod test { use crate::{eval, value::Value}; - use error::Error; use configuration::HashMapConfiguration; + use error::Error; use eval_with_configuration; #[test] @@ -31,7 +37,10 @@ mod test { assert_eq!(eval("3.3"), Ok(Value::Float(3.3))); assert_eq!(eval("true"), Ok(Value::Boolean(true))); 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.6"), Ok(Value::Float(-3.6))); 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("zero".to_string(), Value::Int(0)); - assert_eq!(eval_with_configuration("tr", &configuration), Ok(Value::Boolean(true))); - assert_eq!(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))); + assert_eq!( + eval_with_configuration("tr", &configuration), + Ok(Value::Boolean(true)) + ); + assert_eq!( + 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] diff --git a/src/operator/mod.rs b/src/operator/mod.rs index bc70c9e..8b75b39 100644 --- a/src/operator/mod.rs +++ b/src/operator/mod.rs @@ -21,7 +21,6 @@ pub trait Operator { } pub struct RootNode; -pub struct Braced; pub struct Add; pub struct Sub;