Fix warnings and split Scope (#1902)

This commit is contained in:
Jonathan Turner
2020-05-27 16:50:26 +12:00
committed by GitHub
parent 9567c1f564
commit fa812849b8
29 changed files with 141 additions and 148 deletions

View File

@ -1,4 +1,4 @@
use crate::value::{Primitive, UntaggedValue, Value};
use crate::value::Value;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
@ -13,69 +13,19 @@ pub struct Scope {
pub env: IndexMap<String, String>,
}
impl Scope {
/// Create a new scope
pub fn new(it: Value) -> Scope {
Scope {
it,
vars: IndexMap::new(),
env: IndexMap::new(),
}
}
}
impl Scope {
/// Create an empty scope
pub fn empty() -> Scope {
pub fn new() -> Scope {
Scope {
it: UntaggedValue::Primitive(Primitive::Nothing).into_untagged_value(),
it: Value::nothing(),
vars: IndexMap::new(),
env: IndexMap::new(),
}
}
/// Create an empty scope, setting $it to a known Value
pub fn it_value(value: Value) -> Scope {
Scope {
it: value,
vars: IndexMap::new(),
env: IndexMap::new(),
}
}
pub fn env(env: IndexMap<String, String>) -> Scope {
Scope {
it: UntaggedValue::Primitive(Primitive::Nothing).into_untagged_value(),
vars: IndexMap::new(),
env,
}
}
pub fn set_it(self, value: Value) -> Scope {
Scope {
it: value,
vars: self.vars,
env: self.env,
}
}
pub fn set_var(self, name: String, value: Value) -> Scope {
let mut new_vars = self.vars.clone();
new_vars.insert(name, value);
Scope {
it: self.it,
vars: new_vars,
env: self.env,
}
}
pub fn set_env_var(self, variable: String, value: String) -> Scope {
let mut new_env_vars = self.env.clone();
new_env_vars.insert(variable, value);
Scope {
it: self.it,
vars: self.vars,
env: new_env_vars,
}
}
}
impl Default for Scope {
fn default() -> Scope {
Scope::new()
}
}