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