From b1abbd6cfd4033d2878efa1f305687c175ef3c5a Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Fri, 15 Mar 2019 19:28:59 +0200 Subject: [PATCH] Implemented configuration --- src/configuration/mod.rs | 15 ++++++++++++--- src/error/mod.rs | 2 +- src/lib.rs | 2 +- src/operator/mod.rs | 6 +++++- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/configuration/mod.rs b/src/configuration/mod.rs index ef20033..5659161 100644 --- a/src/configuration/mod.rs +++ b/src/configuration/mod.rs @@ -1,13 +1,22 @@ use crate::{error::Error, value::Value}; +use std::collections::HashMap; pub trait Configuration { - fn get_value(&self, identifier: &str) -> Result; + fn get_value(&self, identifier: &str) -> Option<&Value>; } pub struct EmptyConfiguration; impl Configuration for EmptyConfiguration { - fn get_value(&self, _identifier: &str) -> Result { - Err(Error::IdentifierNotFound) + fn get_value(&self, identifier: &str) -> Option<&Value> { + None } } + +pub type HashMapConfiguration = HashMap; + +impl Configuration for HashMapConfiguration { + fn get_value(&self, identifier: &str) -> Option<&Value> { + self.get(identifier) + } +} \ No newline at end of file diff --git a/src/error/mod.rs b/src/error/mod.rs index 5749afa..d1cded5 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -29,7 +29,7 @@ pub enum Error { PrecedenceViolation, /// An identifier operation did not find its value in the configuration. - IdentifierNotFound, + IdentifierNotFound(String), /// A value has the wrong type. TypeError, diff --git a/src/lib.rs b/src/lib.rs index 7dc5eb9..edcbd1a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,7 +24,7 @@ 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)); + 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))); diff --git a/src/operator/mod.rs b/src/operator/mod.rs index 5965fa3..dcccf83 100644 --- a/src/operator/mod.rs +++ b/src/operator/mod.rs @@ -479,6 +479,10 @@ impl Operator for Identifier { fn eval(&self, arguments: &[Value], configuration: &Configuration) -> Result { expect_argument_amount(arguments.len(), 0)?; - configuration.get_value(&self.identifier) + if let Some(value) = configuration.get_value(&self.identifier).cloned() { + Ok(value) + } else { + Err(Error::IdentifierNotFound(self.identifier.clone())) + } } }