simplify declaration of math fns

This commit is contained in:
ConnorSkees 2020-04-30 15:48:54 -04:00
parent 2d094490ba
commit fda95683ce

View File

@ -6,13 +6,19 @@ use num_traits::{One, Signed, ToPrimitive, Zero};
use rand::Rng;
use super::Builtin;
use crate::args::CallArgs;
use crate::error::SassResult;
use crate::scope::Scope;
use crate::selector::Selector;
use crate::unit::Unit;
use crate::value::{Number, Value};
pub(crate) fn register(f: &mut GlobalFunctionMap) {
f.insert(
"percentage",
Builtin::new(|mut args, scope, super_selector| {
fn percentage(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
args.max_args(1)?;
let num = match arg!(args, scope, super_selector, 0, "number") {
Value::Dimension(n, Unit::None) => n * Number::from(100),
@ -38,11 +44,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
}
};
Ok(Value::Dimension(num, Unit::Percent))
}),
);
f.insert(
"round",
Builtin::new(|mut args, scope, super_selector| {
}
fn round(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "number") {
Value::Dimension(n, u) => Ok(Value::Dimension(n.round(), u)),
@ -55,11 +59,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
)
.into()),
}
}),
);
f.insert(
"ceil",
Builtin::new(|mut args, scope, super_selector| {
}
fn ceil(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "number") {
Value::Dimension(n, u) => Ok(Value::Dimension(n.ceil(), u)),
@ -72,11 +74,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
)
.into()),
}
}),
);
f.insert(
"floor",
Builtin::new(|mut args, scope, super_selector| {
}
fn floor(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "number") {
Value::Dimension(n, u) => Ok(Value::Dimension(n.floor(), u)),
@ -89,11 +89,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
)
.into()),
}
}),
);
f.insert(
"abs",
Builtin::new(|mut args, scope, super_selector| {
}
fn abs(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
match arg!(args, scope, super_selector, 0, "number") {
Value::Dimension(n, u) => Ok(Value::Dimension(n.abs(), u)),
@ -106,11 +104,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
)
.into()),
}
}),
);
f.insert(
"comparable",
Builtin::new(|mut args, scope, super_selector| {
}
fn comparable(
mut args: CallArgs,
scope: &Scope,
super_selector: &Selector,
) -> SassResult<Value> {
args.max_args(2)?;
let unit1 = match arg!(args, scope, super_selector, 0, "number1") {
Value::Dimension(_, u) => u,
@ -140,13 +140,11 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
};
Ok(Value::bool(unit1.comparable(&unit2)))
}),
);
// TODO: write tests for this. how?
}
// TODO: write tests for this
#[cfg(feature = "random")]
f.insert(
"random",
Builtin::new(|mut args, scope, super_selector| {
fn random(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
args.max_args(1)?;
let limit = match arg!(args, scope, super_selector, 0, "limit" = Value::Null) {
Value::Dimension(n, _) => n,
@ -198,6 +196,14 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
Number::from(rng.gen_range(0, limit) + 1),
Unit::None,
))
}),
);
}
f.insert("percentage", Builtin::new(percentage));
f.insert("round", Builtin::new(round));
f.insert("ceil", Builtin::new(ceil));
f.insert("floor", Builtin::new(floor));
f.insert("abs", Builtin::new(abs));
f.insert("comparable", Builtin::new(comparable));
#[cfg(feature = "random")]
f.insert("random", Builtin::new(random));
}