simplify declaration of meta fns
This commit is contained in:
parent
7270890e45
commit
26aabb42ad
@ -3,26 +3,30 @@ use super::GlobalFunctionMap;
|
||||
use codemap::Spanned;
|
||||
|
||||
use super::{Builtin, GLOBAL_FUNCTIONS};
|
||||
use crate::args::CallArgs;
|
||||
use crate::common::QuoteKind;
|
||||
use crate::error::SassResult;
|
||||
use crate::scope::global_var_exists;
|
||||
use crate::scope::Scope;
|
||||
use crate::selector::Selector;
|
||||
use crate::unit::Unit;
|
||||
use crate::value::{SassFunction, Value};
|
||||
|
||||
pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
f.insert(
|
||||
"if",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
fn if_(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
|
||||
args.max_args(3)?;
|
||||
if arg!(args, scope, super_selector, 0, "condition").is_true(args.span())? {
|
||||
Ok(arg!(args, scope, super_selector, 1, "if-true"))
|
||||
} else {
|
||||
Ok(arg!(args, scope, super_selector, 2, "if-false"))
|
||||
}
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"feature-exists",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
|
||||
fn feature_exists(
|
||||
mut args: CallArgs,
|
||||
scope: &Scope,
|
||||
super_selector: &Selector,
|
||||
) -> SassResult<Value> {
|
||||
args.max_args(1)?;
|
||||
match arg!(args, scope, super_selector, 0, "feature") {
|
||||
Value::Ident(s, _) => Ok(match s.as_str() {
|
||||
@ -52,11 +56,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
)
|
||||
.into()),
|
||||
}
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"unit",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
|
||||
fn unit(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
|
||||
args.max_args(1)?;
|
||||
let unit = match arg!(args, scope, super_selector, 0, "number") {
|
||||
Value::Dimension(_, u) => u.to_string(),
|
||||
@ -72,43 +74,38 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
}
|
||||
};
|
||||
Ok(Value::Ident(unit, QuoteKind::Quoted))
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"type-of",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
|
||||
fn type_of(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
|
||||
args.max_args(1)?;
|
||||
let value = arg!(args, scope, super_selector, 0, "value");
|
||||
Ok(Value::Ident(
|
||||
value.kind(args.span())?.to_owned(),
|
||||
QuoteKind::None,
|
||||
))
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"unitless",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
fn unitless(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
|
||||
args.max_args(1)?;
|
||||
match arg!(args, scope, super_selector, 0, "number") {
|
||||
Value::Dimension(_, Unit::None) => Ok(Value::True),
|
||||
Value::Dimension(_, _) => Ok(Value::False),
|
||||
_ => Ok(Value::True),
|
||||
}
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"inspect",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
|
||||
fn inspect(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
|
||||
args.max_args(1)?;
|
||||
Ok(Value::Ident(
|
||||
arg!(args, scope, super_selector, 0, "value").inspect(args.span())?,
|
||||
QuoteKind::None,
|
||||
))
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"variable-exists",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
|
||||
fn variable_exists(
|
||||
mut args: CallArgs,
|
||||
scope: &Scope,
|
||||
super_selector: &Selector,
|
||||
) -> SassResult<Value> {
|
||||
args.max_args(1)?;
|
||||
match arg!(args, scope, super_selector, 0, "name") {
|
||||
Value::Ident(s, _) => Ok(Value::bool(scope.var_exists(&s))),
|
||||
@ -118,11 +115,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
)
|
||||
.into()),
|
||||
}
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"global-variable-exists",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
|
||||
fn global_variable_exists(
|
||||
mut args: CallArgs,
|
||||
scope: &Scope,
|
||||
super_selector: &Selector,
|
||||
) -> SassResult<Value> {
|
||||
args.max_args(1)?;
|
||||
match arg!(args, scope, super_selector, 0, "name") {
|
||||
Value::Ident(s, _) => Ok(Value::bool(global_var_exists(&s))),
|
||||
@ -132,11 +131,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
)
|
||||
.into()),
|
||||
}
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"mixin-exists",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
|
||||
fn mixin_exists(
|
||||
mut args: CallArgs,
|
||||
scope: &Scope,
|
||||
super_selector: &Selector,
|
||||
) -> SassResult<Value> {
|
||||
args.max_args(2)?;
|
||||
match arg!(args, scope, super_selector, 0, "name") {
|
||||
Value::Ident(s, _) => Ok(Value::bool(scope.mixin_exists(&s))),
|
||||
@ -146,11 +147,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
)
|
||||
.into()),
|
||||
}
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"function-exists",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
|
||||
fn function_exists(
|
||||
mut args: CallArgs,
|
||||
scope: &Scope,
|
||||
super_selector: &Selector,
|
||||
) -> SassResult<Value> {
|
||||
args.max_args(2)?;
|
||||
match arg!(args, scope, super_selector, 0, "name") {
|
||||
Value::Ident(s, _) => Ok(Value::bool(
|
||||
@ -162,11 +165,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
)
|
||||
.into()),
|
||||
}
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"get-function",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
|
||||
fn get_function(
|
||||
mut args: CallArgs,
|
||||
scope: &Scope,
|
||||
super_selector: &Selector,
|
||||
) -> SassResult<Value> {
|
||||
args.max_args(3)?;
|
||||
let name = match arg!(args, scope, super_selector, 0, "name") {
|
||||
Value::Ident(s, _) => s,
|
||||
@ -210,18 +215,14 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
Ok(f) => SassFunction::UserDefined(Box::new(f), name),
|
||||
Err(..) => match GLOBAL_FUNCTIONS.get(name.as_str()) {
|
||||
Some(f) => SassFunction::Builtin(f.clone(), name),
|
||||
None => {
|
||||
return Err((format!("Function not found: {}", name), args.span()).into())
|
||||
}
|
||||
None => return Err((format!("Function not found: {}", name), args.span()).into()),
|
||||
},
|
||||
};
|
||||
|
||||
Ok(Value::Function(func))
|
||||
}),
|
||||
);
|
||||
f.insert(
|
||||
"call",
|
||||
Builtin::new(|mut args, scope, super_selector| {
|
||||
}
|
||||
|
||||
fn call(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
|
||||
let func = match arg!(args, scope, super_selector, 0, "function") {
|
||||
Value::Function(f) => f,
|
||||
v => {
|
||||
@ -236,6 +237,21 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||
}
|
||||
};
|
||||
func.call(args.decrement(), scope, super_selector)
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
f.insert("if", Builtin::new(if_));
|
||||
f.insert("feature-exists", Builtin::new(feature_exists));
|
||||
f.insert("unit", Builtin::new(unit));
|
||||
f.insert("type-of", Builtin::new(type_of));
|
||||
f.insert("unitless", Builtin::new(unitless));
|
||||
f.insert("inspect", Builtin::new(inspect));
|
||||
f.insert("variable-exists", Builtin::new(variable_exists));
|
||||
f.insert(
|
||||
"global-variable-exists",
|
||||
Builtin::new(global_variable_exists),
|
||||
);
|
||||
f.insert("mixin-exists", Builtin::new(mixin_exists));
|
||||
f.insert("function-exists", Builtin::new(function_exists));
|
||||
f.insert("get-function", Builtin::new(get_function));
|
||||
f.insert("call", Builtin::new(call));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user