simplify declaration of math fns
This commit is contained in:
parent
2d094490ba
commit
fda95683ce
@ -6,13 +6,19 @@ use num_traits::{One, Signed, ToPrimitive, Zero};
|
|||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
use super::Builtin;
|
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::unit::Unit;
|
||||||
use crate::value::{Number, Value};
|
use crate::value::{Number, Value};
|
||||||
|
|
||||||
pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
||||||
f.insert(
|
fn percentage(
|
||||||
"percentage",
|
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)?;
|
||||||
let num = match arg!(args, scope, super_selector, 0, "number") {
|
let num = match arg!(args, scope, super_selector, 0, "number") {
|
||||||
Value::Dimension(n, Unit::None) => n * Number::from(100),
|
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))
|
Ok(Value::Dimension(num, Unit::Percent))
|
||||||
}),
|
}
|
||||||
);
|
|
||||||
f.insert(
|
fn round(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
|
||||||
"round",
|
|
||||||
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(n, u) => Ok(Value::Dimension(n.round(), u)),
|
Value::Dimension(n, u) => Ok(Value::Dimension(n.round(), u)),
|
||||||
@ -55,11 +59,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
|||||||
)
|
)
|
||||||
.into()),
|
.into()),
|
||||||
}
|
}
|
||||||
}),
|
}
|
||||||
);
|
|
||||||
f.insert(
|
fn ceil(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
|
||||||
"ceil",
|
|
||||||
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(n, u) => Ok(Value::Dimension(n.ceil(), u)),
|
Value::Dimension(n, u) => Ok(Value::Dimension(n.ceil(), u)),
|
||||||
@ -72,11 +74,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
|||||||
)
|
)
|
||||||
.into()),
|
.into()),
|
||||||
}
|
}
|
||||||
}),
|
}
|
||||||
);
|
|
||||||
f.insert(
|
fn floor(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
|
||||||
"floor",
|
|
||||||
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(n, u) => Ok(Value::Dimension(n.floor(), u)),
|
Value::Dimension(n, u) => Ok(Value::Dimension(n.floor(), u)),
|
||||||
@ -89,11 +89,9 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
|||||||
)
|
)
|
||||||
.into()),
|
.into()),
|
||||||
}
|
}
|
||||||
}),
|
}
|
||||||
);
|
|
||||||
f.insert(
|
fn abs(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
|
||||||
"abs",
|
|
||||||
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(n, u) => Ok(Value::Dimension(n.abs(), u)),
|
Value::Dimension(n, u) => Ok(Value::Dimension(n.abs(), u)),
|
||||||
@ -106,11 +104,13 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
|||||||
)
|
)
|
||||||
.into()),
|
.into()),
|
||||||
}
|
}
|
||||||
}),
|
}
|
||||||
);
|
|
||||||
f.insert(
|
fn comparable(
|
||||||
"comparable",
|
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)?;
|
||||||
let unit1 = match arg!(args, scope, super_selector, 0, "number1") {
|
let unit1 = match arg!(args, scope, super_selector, 0, "number1") {
|
||||||
Value::Dimension(_, u) => u,
|
Value::Dimension(_, u) => u,
|
||||||
@ -140,13 +140,11 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Ok(Value::bool(unit1.comparable(&unit2)))
|
Ok(Value::bool(unit1.comparable(&unit2)))
|
||||||
}),
|
}
|
||||||
);
|
|
||||||
// TODO: write tests for this. how?
|
// TODO: write tests for this
|
||||||
#[cfg(feature = "random")]
|
#[cfg(feature = "random")]
|
||||||
f.insert(
|
fn random(mut args: CallArgs, scope: &Scope, super_selector: &Selector) -> SassResult<Value> {
|
||||||
"random",
|
|
||||||
Builtin::new(|mut args, scope, super_selector| {
|
|
||||||
args.max_args(1)?;
|
args.max_args(1)?;
|
||||||
let limit = match arg!(args, scope, super_selector, 0, "limit" = Value::Null) {
|
let limit = match arg!(args, scope, super_selector, 0, "limit" = Value::Null) {
|
||||||
Value::Dimension(n, _) => n,
|
Value::Dimension(n, _) => n,
|
||||||
@ -198,6 +196,14 @@ pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
|||||||
Number::from(rng.gen_range(0, limit) + 1),
|
Number::from(rng.gen_range(0, limit) + 1),
|
||||||
Unit::None,
|
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));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user