forked from kailan/esi
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up some type coercion and make Variables a real struct which ho…
…lds Values, rather than Strings.
- Loading branch information
Showing
3 changed files
with
94 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use std::collections::HashMap; | ||
|
||
#[derive(Debug, Clone, PartialEq)] | ||
pub enum Value { | ||
String(String), | ||
Error(String), | ||
Null, | ||
} | ||
|
||
impl Value { | ||
pub fn to_bool(&self) -> bool { | ||
match self { | ||
Value::String(_) => true, | ||
Value::Error(_) => false, | ||
Value::Null => false, | ||
} | ||
} | ||
|
||
pub fn to_string(&self) -> String { | ||
match self { | ||
Value::String(s) => s.clone(), | ||
Value::Error(_) => "".to_string(), | ||
Value::Null => "".to_string(), | ||
} | ||
} | ||
} | ||
|
||
pub struct Variables { | ||
map: HashMap<String, Value>, | ||
} | ||
|
||
impl Variables { | ||
pub fn new() -> Variables { | ||
Variables { | ||
map: HashMap::new(), | ||
} | ||
} | ||
|
||
pub fn insert(&mut self, name: String, value: Value) { | ||
match value { | ||
Value::Null => {} | ||
_ => { | ||
self.map.insert(name, value); | ||
} | ||
}; | ||
} | ||
|
||
pub fn get(&self, name: &str) -> &Value { | ||
self.map.get(name).unwrap_or(&Value::Null) | ||
} | ||
} | ||
|
||
impl<const N: usize> From<[(String, Value); N]> for Variables { | ||
fn from(data: [(String, Value); N]) -> Variables { | ||
Variables { | ||
map: HashMap::from(data), | ||
} | ||
} | ||
} |