simplify declaraction of map fns

This commit is contained in:
ConnorSkees 2020-04-30 15:18:54 -04:00
parent 2eef3e9f6a
commit 2d094490ba

View File

@ -1,13 +1,15 @@
use super::GlobalFunctionMap; use super::GlobalFunctionMap;
use super::Builtin; use super::Builtin;
use crate::args::CallArgs;
use crate::common::{Brackets, ListSeparator}; use crate::common::{Brackets, ListSeparator};
use crate::error::SassResult;
use crate::scope::Scope;
use crate::selector::Selector;
use crate::value::{SassMap, Value}; use crate::value::{SassMap, Value};
pub(crate) fn register(f: &mut GlobalFunctionMap) { pub(crate) fn register(f: &mut GlobalFunctionMap) {
f.insert( fn map_get(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
"map-get",
Builtin::new(|mut args, scope, super_selector| {
args.max_args(2)?; args.max_args(2)?;
let key = arg!(args, scope, super_selector, 1, "key"); let key = arg!(args, scope, super_selector, 1, "key");
let map = match arg!(args, scope, super_selector, 0, "map") { let map = match arg!(args, scope, super_selector, 0, "map") {
@ -22,11 +24,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
} }
}; };
Ok(map.get(&key, args.span())?.unwrap_or(Value::Null)) Ok(map.get(&key, args.span())?.unwrap_or(Value::Null))
}), }
);
f.insert( fn map_has_key(
"map-has-key", mut args: CallArgs,
Builtin::new(|mut args, scope, super_selector| { scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
args.max_args(2)?; args.max_args(2)?;
let key = arg!(args, scope, super_selector, 1, "key"); let key = arg!(args, scope, super_selector, 1, "key");
let map = match arg!(args, scope, super_selector, 0, "map") { let map = match arg!(args, scope, super_selector, 0, "map") {
@ -41,11 +45,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
} }
}; };
Ok(Value::bool(map.get(&key, args.span())?.is_some())) Ok(Value::bool(map.get(&key, args.span())?.is_some()))
}), }
);
f.insert( fn map_keys(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
"map-keys",
Builtin::new(|mut args, scope, super_selector| {
args.max_args(1)?; args.max_args(1)?;
let map = match arg!(args, scope, super_selector, 0, "map") { let map = match arg!(args, scope, super_selector, 0, "map") {
Value::Map(m) => m, Value::Map(m) => m,
@ -63,11 +65,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
ListSeparator::Comma, ListSeparator::Comma,
Brackets::None, Brackets::None,
)) ))
}), }
);
f.insert( fn map_values(
"map-values", mut args: CallArgs,
Builtin::new(|mut args, scope, super_selector| { scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
args.max_args(1)?; args.max_args(1)?;
let map = match arg!(args, scope, super_selector, 0, "map") { let map = match arg!(args, scope, super_selector, 0, "map") {
Value::Map(m) => m, Value::Map(m) => m,
@ -85,11 +89,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
ListSeparator::Comma, ListSeparator::Comma,
Brackets::None, Brackets::None,
)) ))
}), }
);
f.insert( fn map_merge(
"map-merge", mut args: CallArgs,
Builtin::new(|mut args, scope, super_selector| { scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
args.max_args(2)?; args.max_args(2)?;
let mut map1 = match arg!(args, scope, super_selector, 0, "map1") { let mut map1 = match arg!(args, scope, super_selector, 0, "map1") {
Value::Map(m) => m, Value::Map(m) => m,
@ -115,11 +121,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}; };
map1.merge(map2); map1.merge(map2);
Ok(Value::Map(map1)) Ok(Value::Map(map1))
}), }
);
f.insert( fn map_remove(
"map-remove", mut args: CallArgs,
Builtin::new(|mut args, scope, super_selector| { scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
let mut map = match arg!(args, scope, super_selector, 0, "map") { let mut map = match arg!(args, scope, super_selector, 0, "map") {
Value::Map(m) => m, Value::Map(m) => m,
Value::List(v, ..) if v.is_empty() => SassMap::new(), Value::List(v, ..) if v.is_empty() => SassMap::new(),
@ -136,6 +144,12 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
map.remove(&key); map.remove(&key);
} }
Ok(Value::Map(map)) Ok(Value::Map(map))
}), }
);
f.insert("map-get", Builtin::new(map_get));
f.insert("map-has-key", Builtin::new(map_has_key));
f.insert("map-keys", Builtin::new(map_keys));
f.insert("map-values", Builtin::new(map_values));
f.insert("map-merge", Builtin::new(map_merge));
f.insert("map-remove", Builtin::new(map_remove));
} }