diff --git a/src/run.rs b/src/run.rs index e23f834..39f0182 100644 --- a/src/run.rs +++ b/src/run.rs @@ -28,7 +28,7 @@ impl Scope { self.variables.get(name.as_ref()).cloned() } - pub fn set(&mut self, name: String, value: Value) { + pub fn rawset(&mut self, name: String, value: Value) { if let Some(value) = value { self.variables.insert(name, value); } else { @@ -36,6 +36,11 @@ impl Scope { } } + pub fn set(&mut self, name: impl AsRef, value: &(impl Clone + Into)) { + self.variables + .insert(name.as_ref().to_string(), value.clone().into()); + } + pub fn add_fn( &mut self, name: &'static str, @@ -44,7 +49,7 @@ impl Scope { self.functions.insert( name, Function { - name: name, + name, inner: Rc::new(f), }, ); @@ -112,7 +117,7 @@ impl Exec for Statement { let value = source.eval(scope); match target { Location::Variable { name } => { - scope.set(name.into(), value); + scope.rawset(name.into(), value); } Location::Field { table, index } => { let table = table.eval(scope); @@ -165,7 +170,7 @@ mod tests { let bar = ast::Location::Variable { name: "bar".into() }; assert_eq!(foo.eval(&mut scope), None); assert_eq!(bar.eval(&mut scope), None); - scope.variables.insert("bar".into(), 42.into()); + scope.set("bar", &42); assert_eq!(foo.eval(&mut scope), None); assert_eq!(bar.eval(&mut scope), Some(42.into())); } @@ -181,9 +186,9 @@ mod tests { }; let foo = types::Table::default(); let bar = types::Table::default(); - scope.variables.insert("foo".into(), foo.clone().into()); + scope.set("foo", &foo); assert_eq!(loc.eval(&mut scope), None); - scope.variables.insert("bar".into(), bar.clone().into()); + scope.set("bar", &bar); assert_eq!(loc.eval(&mut scope), None); bar.set("foo".into(), Some(666.into())); assert_eq!(loc.eval(&mut scope), None); @@ -232,8 +237,8 @@ mod tests { let mut scope = Scope::default(); let foo = types::Table::default(); let bar = types::Table::default(); - scope.variables.insert("foo".into(), foo.clone().into()); - scope.variables.insert("bar".into(), bar.clone().into()); + scope.set("foo", &foo); + scope.set("bar", &bar); assert_eq!(foo.get("foo".into()), None); assert_eq!(foo.get("bar".into()), None); assert_eq!(bar.get("foo".into()), None); @@ -264,7 +269,7 @@ mod tests { let Some(ValueInner::String(key)) = key else { panic!("string expected"); }; - scope.set(key.clone(), value.clone()); + scope.rawset(key.clone(), value.clone()); None }); assert_eq!(scope.get("foo"), None);