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
|
|
|
|
2020-04-12 19:37:12 -04:00
|
|
|
use codemap::Span;
|
|
|
|
|
2020-03-30 15:43:15 -04:00
|
|
|
use super::Value;
|
2020-04-02 15:22:18 -04:00
|
|
|
use crate::common::{Brackets, ListSeparator};
|
2020-03-30 15:43:15 -04:00
|
|
|
use crate::error::SassResult;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub(crate) struct SassMap(Vec<(Value, Value)>);
|
|
|
|
|
|
|
|
impl SassMap {
|
2020-03-30 17:06:23 -04:00
|
|
|
pub const fn new() -> SassMap {
|
2020-03-30 15:43:15 -04:00
|
|
|
SassMap(Vec::new())
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:37:12 -04:00
|
|
|
pub fn get(self, key: &Value, span: Span) -> SassResult<Option<Value>> {
|
2020-03-30 15:43:15 -04:00
|
|
|
for (k, v) in self.0 {
|
2020-04-12 19:37:12 -04:00
|
|
|
if k.equals(key.clone(), span)? {
|
2020-03-30 15:43:15 -04:00
|
|
|
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 17:06:23 -04:00
|
|
|
for (key, value) in other {
|
2020-03-30 16:33:43 -04:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2020-04-02 15:22:18 -04:00
|
|
|
pub fn entries(self) -> Vec<Value> {
|
|
|
|
self.0
|
|
|
|
.into_iter()
|
|
|
|
.map(|(k, v)| Value::List(vec![k, v], ListSeparator::Space, Brackets::None))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2020-04-03 16:38:01 -04:00
|
|
|
/// Returns true if the key already exists
|
|
|
|
pub fn insert(&mut self, key: Value, value: Value) -> bool {
|
|
|
|
for (ref k, ref mut v) in &mut self.0 {
|
2020-03-30 15:43:15 -04:00
|
|
|
if k == &key {
|
|
|
|
*v = value;
|
2020-04-03 16:38:01 -04:00
|
|
|
return true;
|
2020-03-30 15:43:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self.0.push((key, value));
|
2020-04-03 16:38:01 -04:00
|
|
|
false
|
2020-03-30 15:43:15 -04:00
|
|
|
}
|
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
}
|