2020-04-30 15:00:57 -04:00
|
|
|
use super::GlobalFunctionMap;
|
2020-02-02 21:09:29 -05:00
|
|
|
|
2020-04-05 13:42:53 -04:00
|
|
|
#[cfg(feature = "random")]
|
|
|
|
use num_traits::{One, Signed, ToPrimitive, Zero};
|
|
|
|
#[cfg(feature = "random")]
|
|
|
|
use rand::Rng;
|
|
|
|
|
2020-02-09 16:05:07 -05:00
|
|
|
use super::Builtin;
|
2020-03-19 16:24:31 -04:00
|
|
|
use crate::unit::Unit;
|
2020-02-09 16:05:07 -05:00
|
|
|
use crate::value::{Number, Value};
|
|
|
|
|
2020-04-30 15:00:57 -04:00
|
|
|
pub(crate) fn register(f: &mut GlobalFunctionMap) {
|
2020-03-16 10:35:38 -04:00
|
|
|
f.insert(
|
2020-04-30 15:00:57 -04:00
|
|
|
"percentage",
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-04-22 06:17:52 -04:00
|
|
|
args.max_args(1)?;
|
2020-04-04 18:17:04 -04:00
|
|
|
let num = match arg!(args, scope, super_selector, 0, "number") {
|
2020-03-16 10:35:38 -04:00
|
|
|
Value::Dimension(n, Unit::None) => n * Number::from(100),
|
|
|
|
v @ Value::Dimension(..) => {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"$number: Expected {} to have no units.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
v => {
|
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"$number: {} is not a number.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into())
|
2020-03-16 10:35:38 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(Value::Dimension(num, Unit::Percent))
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
f.insert(
|
2020-04-30 15:00:57 -04:00
|
|
|
"round",
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-04-22 06:17:52 -04:00
|
|
|
args.max_args(1)?;
|
2020-04-04 18:17:04 -04:00
|
|
|
match arg!(args, scope, super_selector, 0, "number") {
|
2020-03-16 10:35:38 -04:00
|
|
|
Value::Dimension(n, u) => Ok(Value::Dimension(n.round(), u)),
|
2020-04-12 19:37:12 -04:00
|
|
|
v => Err((
|
|
|
|
format!(
|
|
|
|
"$number: {} is not a number.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into()),
|
2020-03-16 10:35:38 -04:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
f.insert(
|
2020-04-30 15:00:57 -04:00
|
|
|
"ceil",
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-04-22 06:17:52 -04:00
|
|
|
args.max_args(1)?;
|
2020-04-04 18:17:04 -04:00
|
|
|
match arg!(args, scope, super_selector, 0, "number") {
|
2020-03-16 10:35:38 -04:00
|
|
|
Value::Dimension(n, u) => Ok(Value::Dimension(n.ceil(), u)),
|
2020-04-12 19:37:12 -04:00
|
|
|
v => Err((
|
|
|
|
format!(
|
|
|
|
"$number: {} is not a number.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into()),
|
2020-03-16 10:35:38 -04:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
f.insert(
|
2020-04-30 15:00:57 -04:00
|
|
|
"floor",
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-04-22 06:17:52 -04:00
|
|
|
args.max_args(1)?;
|
2020-04-04 18:17:04 -04:00
|
|
|
match arg!(args, scope, super_selector, 0, "number") {
|
2020-03-16 10:35:38 -04:00
|
|
|
Value::Dimension(n, u) => Ok(Value::Dimension(n.floor(), u)),
|
2020-04-12 19:37:12 -04:00
|
|
|
v => Err((
|
|
|
|
format!(
|
|
|
|
"$number: {} is not a number.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into()),
|
2020-03-16 10:35:38 -04:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
f.insert(
|
2020-04-30 15:00:57 -04:00
|
|
|
"abs",
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-04-22 06:17:52 -04:00
|
|
|
args.max_args(1)?;
|
2020-04-04 18:17:04 -04:00
|
|
|
match arg!(args, scope, super_selector, 0, "number") {
|
2020-03-16 10:35:38 -04:00
|
|
|
Value::Dimension(n, u) => Ok(Value::Dimension(n.abs(), u)),
|
2020-04-12 19:37:12 -04:00
|
|
|
v => Err((
|
|
|
|
format!(
|
|
|
|
"$number: {} is not a number.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into()),
|
2020-03-16 10:35:38 -04:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
f.insert(
|
2020-04-30 15:00:57 -04:00
|
|
|
"comparable",
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-04-22 06:17:52 -04:00
|
|
|
args.max_args(2)?;
|
2020-04-04 18:17:04 -04:00
|
|
|
let unit1 = match arg!(args, scope, super_selector, 0, "number1") {
|
2020-03-16 10:35:38 -04:00
|
|
|
Value::Dimension(_, u) => u,
|
2020-04-12 19:37:12 -04:00
|
|
|
v => {
|
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"$number1: {} is not a number.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
2020-03-16 10:35:38 -04:00
|
|
|
};
|
2020-04-04 18:17:04 -04:00
|
|
|
let unit2 = match arg!(args, scope, super_selector, 1, "number2") {
|
2020-03-16 10:35:38 -04:00
|
|
|
Value::Dimension(_, u) => u,
|
2020-04-12 19:37:12 -04:00
|
|
|
v => {
|
|
|
|
return Err((
|
|
|
|
format!(
|
|
|
|
"$number2: {} is not a number.",
|
|
|
|
v.to_css_string(args.span())?
|
|
|
|
),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
2020-03-16 10:35:38 -04:00
|
|
|
};
|
2020-02-15 08:16:17 -05:00
|
|
|
|
2020-03-16 10:35:38 -04:00
|
|
|
Ok(Value::bool(unit1.comparable(&unit2)))
|
|
|
|
}),
|
|
|
|
);
|
2020-04-05 13:42:53 -04:00
|
|
|
// TODO: write tests for this. how?
|
|
|
|
#[cfg(feature = "random")]
|
|
|
|
f.insert(
|
2020-04-30 15:00:57 -04:00
|
|
|
"random",
|
2020-04-05 13:42:53 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-04-22 06:17:52 -04:00
|
|
|
args.max_args(1)?;
|
2020-04-05 13:42:53 -04:00
|
|
|
let limit = match arg!(args, scope, super_selector, 0, "limit" = Value::Null) {
|
|
|
|
Value::Dimension(n, _) => n,
|
|
|
|
Value::Null => {
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
return Ok(Value::Dimension(
|
|
|
|
Number::from(rng.gen_range(0.0, 1.0)),
|
|
|
|
Unit::None,
|
|
|
|
));
|
|
|
|
}
|
2020-04-12 19:37:12 -04:00
|
|
|
v => {
|
|
|
|
return Err((
|
|
|
|
format!("$limit: {} is not a number.", v.to_css_string(args.span())?),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into())
|
|
|
|
}
|
2020-04-05 13:42:53 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if limit.is_one() {
|
|
|
|
return Ok(Value::Dimension(Number::one(), Unit::None));
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit.is_decimal() {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err((format!("$limit: {} is not an int.", limit), args.span()).into());
|
2020-04-05 13:42:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if limit.is_zero() || limit.is_negative() {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err((
|
|
|
|
format!("$limit: Must be greater than 0, was {}.", limit),
|
|
|
|
args.span(),
|
|
|
|
)
|
|
|
|
.into());
|
2020-04-05 13:42:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let limit = match limit.to_integer().to_u32() {
|
|
|
|
Some(n) => n,
|
|
|
|
None => {
|
2020-04-12 19:37:12 -04:00
|
|
|
return Err((
|
2020-04-21 18:22:26 -04:00
|
|
|
format!("max must be in range 0 < max \u{2264} 2^32, was {}", limit),
|
2020-04-12 19:37:12 -04:00
|
|
|
args.span(),
|
2020-04-05 13:42:53 -04:00
|
|
|
)
|
2020-04-12 19:37:12 -04:00
|
|
|
.into())
|
2020-04-05 13:42:53 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
Ok(Value::Dimension(
|
2020-04-05 18:30:17 -04:00
|
|
|
Number::from(rng.gen_range(0, limit) + 1),
|
2020-04-05 13:42:53 -04:00
|
|
|
Unit::None,
|
|
|
|
))
|
|
|
|
}),
|
|
|
|
);
|
2020-02-09 18:28:24 -05:00
|
|
|
}
|