Refactor meta builtins
This commit is contained in:
parent
9626cbc55f
commit
f081259b02
@ -7,18 +7,14 @@ use crate::value::Value;
|
|||||||
|
|
||||||
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||||
decl!(f "if", |args, _| {
|
decl!(f "if", |args, _| {
|
||||||
let cond: &Value = arg!(args, 0, "condition");
|
if arg!(args, 0, "condition").is_true() {
|
||||||
let if_true = arg!(args, 1, "if-true").clone();
|
Ok(arg!(args, 1, "if-true").eval())
|
||||||
let if_false = arg!(args, 2, "if-false").clone();
|
|
||||||
if cond.is_true() {
|
|
||||||
Ok(if_true)
|
|
||||||
} else {
|
} else {
|
||||||
Ok(if_false)
|
Ok(arg!(args, 2, "if-false").eval())
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
decl!(f "feature-exists", |args, _| {
|
decl!(f "feature-exists", |args, _| {
|
||||||
let feature: &Value = arg!(args, 0, "feature");
|
match arg!(args, 0, "feature").eval().unquote().to_string().as_str() {
|
||||||
match feature.clone().unquote().to_string().as_str() {
|
|
||||||
// A local variable will shadow a global variable unless
|
// A local variable will shadow a global variable unless
|
||||||
// `!global` is used.
|
// `!global` is used.
|
||||||
"global-variable-shadowing" => Ok(Value::False),
|
"global-variable-shadowing" => Ok(Value::False),
|
||||||
@ -38,20 +34,18 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
decl!(f "unit", |args, _| {
|
decl!(f "unit", |args, _| {
|
||||||
let number = arg!(args, 0, "number");
|
let unit = match arg!(args, 0, "number") {
|
||||||
let unit = match number {
|
|
||||||
Value::Dimension(_, u) => u.to_string(),
|
Value::Dimension(_, u) => u.to_string(),
|
||||||
_ => String::new()
|
_ => String::new()
|
||||||
};
|
};
|
||||||
Ok(Value::Ident(unit, QuoteKind::Double))
|
Ok(Value::Ident(unit, QuoteKind::Double))
|
||||||
});
|
});
|
||||||
decl!(f "type-of", |args, _| {
|
decl!(f "type-of", |args, _| {
|
||||||
let value = arg!(args, 0, "value");
|
let value = arg!(args, 0, "value").eval();
|
||||||
Ok(Value::Ident(value.kind().to_owned(), QuoteKind::None))
|
Ok(Value::Ident(value.kind().to_owned(), QuoteKind::None))
|
||||||
});
|
});
|
||||||
decl!(f "unitless", |args, _| {
|
decl!(f "unitless", |args, _| {
|
||||||
let number = arg!(args, 0, "number");
|
match arg!(args, 0, "number") {
|
||||||
match 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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user