Add max_args! to most functions
This commit is contained in:
parent
f081259b02
commit
26b0f2c998
@ -6,6 +6,7 @@ use crate::value::{Number, Value};
|
||||
|
||||
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
decl!(f "percentage", |args, _| {
|
||||
max_args!(args, 1);
|
||||
let num = match arg!(args, 0, "number").eval() {
|
||||
Value::Dimension(n, Unit::None) => n * Number::from(100),
|
||||
_ => todo!("expected unitless number in builtin function `percentage()`")
|
||||
@ -13,30 +14,35 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
Ok(Value::Dimension(num, Unit::Percent))
|
||||
});
|
||||
decl!(f "round", |args, _| {
|
||||
max_args!(args, 1);
|
||||
match arg!(args, 0, "number").eval() {
|
||||
Value::Dimension(n, u) => Ok(Value::Dimension(n.round(), u)),
|
||||
v => return Err(format!("$number: {} is not a number.", v).into()),
|
||||
}
|
||||
});
|
||||
decl!(f "ceil", |args, _| {
|
||||
max_args!(args, 1);
|
||||
match arg!(args, 0, "number").eval() {
|
||||
Value::Dimension(n, u) => Ok(Value::Dimension(n.ceil(), u)),
|
||||
v => return Err(format!("$number: {} is not a number.", v).into()),
|
||||
}
|
||||
});
|
||||
decl!(f "floor", |args, _| {
|
||||
max_args!(args, 1);
|
||||
match arg!(args, 0, "number").eval() {
|
||||
Value::Dimension(n, u) => Ok(Value::Dimension(n.floor(), u)),
|
||||
v => return Err(format!("$number: {} is not a number.", v).into()),
|
||||
}
|
||||
});
|
||||
decl!(f "abs", |args, _| {
|
||||
max_args!(args, 1);
|
||||
match arg!(args, 0, "number").eval() {
|
||||
Value::Dimension(n, u) => Ok(Value::Dimension(n.abs(), u)),
|
||||
v => return Err(format!("$number: {} is not a number.", v).into()),
|
||||
}
|
||||
});
|
||||
decl!(f "comparable", |args, _| {
|
||||
max_args!(args, 2);
|
||||
let unit1 = match arg!(args, 0, "number1").eval() {
|
||||
Value::Dimension(_, u) => u,
|
||||
v => return Err(format!("$number1: {} is not a number.", v).into()),
|
||||
|
@ -7,6 +7,7 @@ use crate::value::Value;
|
||||
|
||||
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
decl!(f "if", |args, _| {
|
||||
max_args!(args, 3);
|
||||
if arg!(args, 0, "condition").is_true() {
|
||||
Ok(arg!(args, 1, "if-true").eval())
|
||||
} else {
|
||||
@ -14,6 +15,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
}
|
||||
});
|
||||
decl!(f "feature-exists", |args, _| {
|
||||
max_args!(args, 1);
|
||||
match arg!(args, 0, "feature").eval().unquote().to_string().as_str() {
|
||||
// A local variable will shadow a global variable unless
|
||||
// `!global` is used.
|
||||
@ -34,6 +36,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
}
|
||||
});
|
||||
decl!(f "unit", |args, _| {
|
||||
max_args!(args, 1);
|
||||
let unit = match arg!(args, 0, "number") {
|
||||
Value::Dimension(_, u) => u.to_string(),
|
||||
_ => String::new()
|
||||
@ -41,10 +44,12 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
Ok(Value::Ident(unit, QuoteKind::Double))
|
||||
});
|
||||
decl!(f "type-of", |args, _| {
|
||||
max_args!(args, 1);
|
||||
let value = arg!(args, 0, "value").eval();
|
||||
Ok(Value::Ident(value.kind().to_owned(), QuoteKind::None))
|
||||
});
|
||||
decl!(f "unitless", |args, _| {
|
||||
max_args!(args, 1);
|
||||
match arg!(args, 0, "number") {
|
||||
Value::Dimension(_, Unit::None) => Ok(Value::True),
|
||||
Value::Dimension(_, _) => Ok(Value::False),
|
||||
@ -52,18 +57,22 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
}
|
||||
});
|
||||
decl!(f "inspect", |args, _| {
|
||||
max_args!(args, 1);
|
||||
let value = arg!(args, 0, "value");
|
||||
Ok(Value::Ident(value.to_string(), QuoteKind::None))
|
||||
});
|
||||
decl!(f "variable-exists", |args, scope| {
|
||||
max_args!(args, 1);
|
||||
let value = arg!(args, 0, "name");
|
||||
Ok(Value::bool(scope.var_exists(&value.to_string())))
|
||||
});
|
||||
decl!(f "mixin-exists", |args, scope| {
|
||||
max_args!(args, 1);
|
||||
let value = arg!(args, 0, "name");
|
||||
Ok(Value::bool(scope.mixin_exists(&value.to_string())))
|
||||
});
|
||||
decl!(f "function-exists", |args, scope| {
|
||||
max_args!(args, 1);
|
||||
let value = arg!(args, 0, "name");
|
||||
let s = value.eval().unquote().to_string();
|
||||
Ok(Value::bool(scope.fn_exists(&s) || GLOBAL_FUNCTIONS.contains_key(&s)))
|
||||
|
@ -11,6 +11,7 @@ use crate::value::{Number, Value};
|
||||
|
||||
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
decl!(f "to-upper-case", |args, _| {
|
||||
max_args!(args, 1);
|
||||
let s: &Value = arg!(args, 0, "string");
|
||||
match s.eval() {
|
||||
Value::Ident(i, q) => Ok(Value::Ident(i.to_ascii_uppercase(), q)),
|
||||
@ -18,6 +19,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
}
|
||||
});
|
||||
decl!(f "to-lower-case", |args, _| {
|
||||
max_args!(args, 1);
|
||||
let s: &Value = arg!(args, 0, "string");
|
||||
match s.eval() {
|
||||
Value::Ident(i, q) => Ok(Value::Ident(i.to_ascii_lowercase(), q)),
|
||||
@ -25,6 +27,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
}
|
||||
});
|
||||
decl!(f "str-length", |args, _| {
|
||||
max_args!(args, 1);
|
||||
let s: &Value = arg!(args, 0, "string");
|
||||
match s.eval() {
|
||||
Value::Ident(i, _) => Ok(Value::Dimension(Number::from(i.len()), Unit::None)),
|
||||
@ -32,6 +35,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
}
|
||||
});
|
||||
decl!(f "quote", |args, _| {
|
||||
max_args!(args, 1);
|
||||
let s = arg!(args, 0, "string").eval();
|
||||
match s {
|
||||
Value::Ident(i, _) => Ok(Value::Ident(i, QuoteKind::Double)),
|
||||
@ -39,9 +43,11 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
|
||||
}
|
||||
});
|
||||
decl!(f "unquote", |args, _| {
|
||||
max_args!(args, 1);
|
||||
Ok(arg!(args, 0, "string").eval().unquote())
|
||||
});
|
||||
decl!(f "str-slice", |args, _| {
|
||||
max_args!(args, 3);
|
||||
let (string, quotes) = match arg!(args, 0, "string").eval() {
|
||||
Value::Ident(s, q) => (s, q),
|
||||
_ => todo!("____ is not a string")
|
||||
|
Loading…
x
Reference in New Issue
Block a user