mod serde;
use std::collections::HashMap;
use value::{Value, FromValue, FromValueMut};
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Table {
map: HashMap<String, Value>,
}
impl Table {
pub fn new() -> Table {
Table {
map: HashMap::new(),
}
}
pub fn with_map(map: HashMap<String, Value>) -> Table {
Table {
map,
}
}
pub fn exists(&self, name: &str) -> bool {
self.map.contains_key(name)
}
pub fn get<'a, T>(&'a self, name: &str) -> Option<T>
where
T: FromValue<'a>,
{
self.map.get(name)
.and_then(T::from_value)
}
pub fn get_mut<'a, T>(&'a mut self, name: &str) -> Option<T>
where
T: FromValueMut<'a>,
{
self.map.get_mut(name)
.and_then(T::from_value_mut)
}
pub fn set<T>(&mut self, name: &str, value: T) -> Option<Value>
where
Value: From<T>,
{
self.map.insert(name.to_string(), Value::from(value))
}
pub fn remove(&mut self, name: &str) -> Option<Value> {
self.map.remove(name)
}
pub fn clear(&mut self) {
self.map.clear()
}
pub fn iter<'a>(&'a self)
-> ::std::collections::hash_map::Iter<'a, String, Value>
{
self.map.iter()
}
pub fn iter_mut<'a>(&'a mut self)
-> ::std::collections::hash_map::IterMut<'a, String, Value>
{
self.map.iter_mut()
}
}
impl From<HashMap<String, Value>> for Table {
fn from(map: HashMap<String, Value>) -> Table {
Table::with_map(map)
}
}
impl<'a> IntoIterator for &'a Table {
type Item = (&'a String, &'a Value);
type IntoIter = ::std::collections::hash_map::Iter<'a, String, Value>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a> IntoIterator for &'a mut Table {
type Item = (&'a String, &'a mut Value);
type IntoIter = ::std::collections::hash_map::IterMut<'a, String, Value>;
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}