grass/src/value/map.rs

70 lines
1.5 KiB
Rust
Raw Normal View History

2020-03-30 15:43:15 -04:00
use std::slice::Iter;
2020-03-30 16:33:43 -04:00
use std::vec::IntoIter;
2020-03-30 15:43:15 -04:00
use super::Value;
use crate::error::SassResult;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct SassMap(Vec<(Value, Value)>);
impl SassMap {
pub fn new() -> SassMap {
SassMap(Vec::new())
}
pub fn get(self, key: Value) -> SassResult<Option<Value>> {
for (k, v) in self.0 {
if k.equals(key.clone())? {
return Ok(Some(v));
}
}
Ok(None)
}
2020-03-30 16:01:44 -04:00
pub fn len(&self) -> usize {
self.0.len()
}
2020-03-30 15:43:15 -04:00
#[allow(dead_code)]
pub fn remove(&mut self, key: &Value) {
self.0.retain(|(ref k, ..)| k != key);
}
pub fn merge(&mut self, other: SassMap) {
2020-03-30 16:33:43 -04:00
for (key, value) in other.into_iter() {
self.insert(key, value);
}
2020-03-30 15:43:15 -04:00
}
pub fn iter(&self) -> Iter<(Value, Value)> {
self.0.iter()
}
pub fn keys(self) -> Vec<Value> {
self.0.into_iter().map(|(k, ..)| k).collect()
}
pub fn values(self) -> Vec<Value> {
self.0.into_iter().map(|(.., v)| v).collect()
}
pub fn insert(&mut self, key: Value, value: Value) {
for &mut (ref k, ref mut v) in &mut self.0 {
if k == &key {
*v = value;
return;
}
}
self.0.push((key, value));
}
}
2020-03-30 16:33:43 -04:00
impl IntoIterator for SassMap {
type Item = (Value, Value);
type IntoIter = IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}