Add max_args! to most functions

This commit is contained in:
ConnorSkees 2020-02-16 18:18:57 -05:00
parent f081259b02
commit 26b0f2c998
3 changed files with 21 additions and 0 deletions

View File

@ -6,6 +6,7 @@ use crate::value::{Number, Value};
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) { pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
decl!(f "percentage", |args, _| { decl!(f "percentage", |args, _| {
max_args!(args, 1);
let num = match arg!(args, 0, "number").eval() { let num = match arg!(args, 0, "number").eval() {
Value::Dimension(n, Unit::None) => n * Number::from(100), Value::Dimension(n, Unit::None) => n * Number::from(100),
_ => todo!("expected unitless number in builtin function `percentage()`") _ => 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)) Ok(Value::Dimension(num, Unit::Percent))
}); });
decl!(f "round", |args, _| { decl!(f "round", |args, _| {
max_args!(args, 1);
match arg!(args, 0, "number").eval() { match arg!(args, 0, "number").eval() {
Value::Dimension(n, u) => Ok(Value::Dimension(n.round(), u)), Value::Dimension(n, u) => Ok(Value::Dimension(n.round(), u)),
v => return Err(format!("$number: {} is not a number.", v).into()), v => return Err(format!("$number: {} is not a number.", v).into()),
} }
}); });
decl!(f "ceil", |args, _| { decl!(f "ceil", |args, _| {
max_args!(args, 1);
match arg!(args, 0, "number").eval() { match arg!(args, 0, "number").eval() {
Value::Dimension(n, u) => Ok(Value::Dimension(n.ceil(), u)), Value::Dimension(n, u) => Ok(Value::Dimension(n.ceil(), u)),
v => return Err(format!("$number: {} is not a number.", v).into()), v => return Err(format!("$number: {} is not a number.", v).into()),
} }
}); });
decl!(f "floor", |args, _| { decl!(f "floor", |args, _| {
max_args!(args, 1);
match arg!(args, 0, "number").eval() { match arg!(args, 0, "number").eval() {
Value::Dimension(n, u) => Ok(Value::Dimension(n.floor(), u)), Value::Dimension(n, u) => Ok(Value::Dimension(n.floor(), u)),
v => return Err(format!("$number: {} is not a number.", v).into()), v => return Err(format!("$number: {} is not a number.", v).into()),
} }
}); });
decl!(f "abs", |args, _| { decl!(f "abs", |args, _| {
max_args!(args, 1);
match arg!(args, 0, "number").eval() { match arg!(args, 0, "number").eval() {
Value::Dimension(n, u) => Ok(Value::Dimension(n.abs(), u)), Value::Dimension(n, u) => Ok(Value::Dimension(n.abs(), u)),
v => return Err(format!("$number: {} is not a number.", v).into()), v => return Err(format!("$number: {} is not a number.", v).into()),
} }
}); });
decl!(f "comparable", |args, _| { decl!(f "comparable", |args, _| {
max_args!(args, 2);
let unit1 = match arg!(args, 0, "number1").eval() { let unit1 = match arg!(args, 0, "number1").eval() {
Value::Dimension(_, u) => u, Value::Dimension(_, u) => u,
v => return Err(format!("$number1: {} is not a number.", v).into()), v => return Err(format!("$number1: {} is not a number.", v).into()),

View File

@ -7,6 +7,7 @@ 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, _| {
max_args!(args, 3);
if arg!(args, 0, "condition").is_true() { if arg!(args, 0, "condition").is_true() {
Ok(arg!(args, 1, "if-true").eval()) Ok(arg!(args, 1, "if-true").eval())
} else { } else {
@ -14,6 +15,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
} }
}); });
decl!(f "feature-exists", |args, _| { decl!(f "feature-exists", |args, _| {
max_args!(args, 1);
match arg!(args, 0, "feature").eval().unquote().to_string().as_str() { match arg!(args, 0, "feature").eval().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.
@ -34,6 +36,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
} }
}); });
decl!(f "unit", |args, _| { decl!(f "unit", |args, _| {
max_args!(args, 1);
let unit = match arg!(args, 0, "number") { let unit = match arg!(args, 0, "number") {
Value::Dimension(_, u) => u.to_string(), Value::Dimension(_, u) => u.to_string(),
_ => String::new() _ => String::new()
@ -41,10 +44,12 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
Ok(Value::Ident(unit, QuoteKind::Double)) Ok(Value::Ident(unit, QuoteKind::Double))
}); });
decl!(f "type-of", |args, _| { decl!(f "type-of", |args, _| {
max_args!(args, 1);
let value = arg!(args, 0, "value").eval(); 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, _| {
max_args!(args, 1);
match arg!(args, 0, "number") { match arg!(args, 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),
@ -52,18 +57,22 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
} }
}); });
decl!(f "inspect", |args, _| { decl!(f "inspect", |args, _| {
max_args!(args, 1);
let value = arg!(args, 0, "value"); let value = arg!(args, 0, "value");
Ok(Value::Ident(value.to_string(), QuoteKind::None)) Ok(Value::Ident(value.to_string(), QuoteKind::None))
}); });
decl!(f "variable-exists", |args, scope| { decl!(f "variable-exists", |args, scope| {
max_args!(args, 1);
let value = arg!(args, 0, "name"); let value = arg!(args, 0, "name");
Ok(Value::bool(scope.var_exists(&value.to_string()))) Ok(Value::bool(scope.var_exists(&value.to_string())))
}); });
decl!(f "mixin-exists", |args, scope| { decl!(f "mixin-exists", |args, scope| {
max_args!(args, 1);
let value = arg!(args, 0, "name"); let value = arg!(args, 0, "name");
Ok(Value::bool(scope.mixin_exists(&value.to_string()))) Ok(Value::bool(scope.mixin_exists(&value.to_string())))
}); });
decl!(f "function-exists", |args, scope| { decl!(f "function-exists", |args, scope| {
max_args!(args, 1);
let value = arg!(args, 0, "name"); let value = arg!(args, 0, "name");
let s = value.eval().unquote().to_string(); let s = value.eval().unquote().to_string();
Ok(Value::bool(scope.fn_exists(&s) || GLOBAL_FUNCTIONS.contains_key(&s))) Ok(Value::bool(scope.fn_exists(&s) || GLOBAL_FUNCTIONS.contains_key(&s)))

View File

@ -11,6 +11,7 @@ use crate::value::{Number, Value};
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) { pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
decl!(f "to-upper-case", |args, _| { decl!(f "to-upper-case", |args, _| {
max_args!(args, 1);
let s: &Value = arg!(args, 0, "string"); let s: &Value = arg!(args, 0, "string");
match s.eval() { match s.eval() {
Value::Ident(i, q) => Ok(Value::Ident(i.to_ascii_uppercase(), q)), 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, _| { decl!(f "to-lower-case", |args, _| {
max_args!(args, 1);
let s: &Value = arg!(args, 0, "string"); let s: &Value = arg!(args, 0, "string");
match s.eval() { match s.eval() {
Value::Ident(i, q) => Ok(Value::Ident(i.to_ascii_lowercase(), q)), 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, _| { decl!(f "str-length", |args, _| {
max_args!(args, 1);
let s: &Value = arg!(args, 0, "string"); let s: &Value = arg!(args, 0, "string");
match s.eval() { match s.eval() {
Value::Ident(i, _) => Ok(Value::Dimension(Number::from(i.len()), Unit::None)), 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, _| { decl!(f "quote", |args, _| {
max_args!(args, 1);
let s = arg!(args, 0, "string").eval(); let s = arg!(args, 0, "string").eval();
match s { match s {
Value::Ident(i, _) => Ok(Value::Ident(i, QuoteKind::Double)), 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, _| { decl!(f "unquote", |args, _| {
max_args!(args, 1);
Ok(arg!(args, 0, "string").eval().unquote()) Ok(arg!(args, 0, "string").eval().unquote())
}); });
decl!(f "str-slice", |args, _| { decl!(f "str-slice", |args, _| {
max_args!(args, 3);
let (string, quotes) = match arg!(args, 0, "string").eval() { let (string, quotes) = match arg!(args, 0, "string").eval() {
Value::Ident(s, q) => (s, q), Value::Ident(s, q) => (s, q),
_ => todo!("____ is not a string") _ => todo!("____ is not a string")