grass/src/builtin/math.rs

200 lines
5.6 KiB
Rust
Raw Normal View History

2020-05-18 11:50:40 -04:00
use super::{Builtin, 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-04-30 15:48:54 -04:00
use crate::args::CallArgs;
use crate::error::SassResult;
2020-06-16 19:38:30 -04:00
use crate::parse::Parser;
use crate::unit::Unit;
use crate::value::{Number, Value};
2020-06-16 19:38:30 -04:00
fn percentage(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
2020-06-16 19:38:30 -04:00
let num = match parser.arg(&mut args, 0, "number")? {
Value::Dimension(n, Unit::None) => n * Number::from(100),
v @ Value::Dimension(..) => {
return Err((
2020-04-30 15:48:54 -04:00
format!(
"$number: Expected {} to have no units.",
2020-04-30 15:48:54 -04:00
v.to_css_string(args.span())?
),
args.span(),
)
.into())
2020-04-30 15:48:54 -04:00
}
v => {
return Err((
2020-04-30 15:48:54 -04:00
format!(
"$number: {} is not a number.",
v.to_css_string(args.span())?
),
args.span(),
)
.into())
2020-04-30 15:48:54 -04:00
}
};
Ok(Value::Dimension(num, Unit::Percent))
}
2020-06-16 19:38:30 -04:00
fn round(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
2020-06-16 19:38:30 -04:00
match parser.arg(&mut args, 0, "number")? {
Value::Dimension(n, u) => Ok(Value::Dimension(n.round(), u)),
v => Err((
format!(
"$number: {} is not a number.",
v.to_css_string(args.span())?
),
args.span(),
)
.into()),
}
}
2020-06-16 19:38:30 -04:00
fn ceil(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
2020-06-16 19:38:30 -04:00
match parser.arg(&mut args, 0, "number")? {
Value::Dimension(n, u) => Ok(Value::Dimension(n.ceil(), u)),
v => Err((
format!(
"$number: {} is not a number.",
v.to_css_string(args.span())?
),
args.span(),
)
.into()),
}
}
2020-06-16 19:38:30 -04:00
fn floor(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
2020-06-16 19:38:30 -04:00
match parser.arg(&mut args, 0, "number")? {
Value::Dimension(n, u) => Ok(Value::Dimension(n.floor(), u)),
v => Err((
format!(
"$number: {} is not a number.",
v.to_css_string(args.span())?
),
args.span(),
)
.into()),
2020-04-30 15:48:54 -04:00
}
}
2020-04-30 15:48:54 -04:00
2020-06-16 19:38:30 -04:00
fn abs(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
2020-06-16 19:38:30 -04:00
match parser.arg(&mut args, 0, "number")? {
Value::Dimension(n, u) => Ok(Value::Dimension(n.abs(), u)),
v => Err((
format!(
"$number: {} is not a number.",
v.to_css_string(args.span())?
),
args.span(),
)
.into()),
}
}
2020-06-16 19:38:30 -04:00
fn comparable(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(2)?;
2020-06-16 19:38:30 -04:00
let unit1 = match parser.arg(&mut args, 0, "number1")? {
Value::Dimension(_, u) => u,
v => {
return Err((
2020-04-30 15:48:54 -04:00
format!(
"$number1: {} is not a number.",
2020-04-30 15:48:54 -04:00
v.to_css_string(args.span())?
),
args.span(),
)
.into())
2020-04-30 15:48:54 -04:00
}
};
2020-06-16 19:38:30 -04:00
let unit2 = match parser.arg(&mut args, 1, "number2")? {
Value::Dimension(_, u) => u,
v => {
return Err((
2020-04-30 15:48:54 -04:00
format!(
"$number2: {} is not a number.",
2020-04-30 15:48:54 -04:00
v.to_css_string(args.span())?
),
args.span(),
)
.into())
2020-04-30 15:48:54 -04:00
}
};
2020-04-30 15:48:54 -04:00
Ok(Value::bool(unit1.comparable(&unit2)))
}
2020-04-30 15:48:54 -04:00
// TODO: write tests for this
#[cfg(feature = "random")]
2020-06-16 19:38:30 -04:00
fn random(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
2020-06-16 19:38:30 -04:00
let limit = match parser.default_arg(&mut args, 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-30 15:48:54 -04:00
}
v => {
return Err((
format!("$limit: {} is not a number.", v.to_css_string(args.span())?),
args.span(),
)
.into())
2020-04-30 15:48:54 -04:00
}
};
if limit.is_one() {
return Ok(Value::Dimension(Number::one(), Unit::None));
}
if limit.is_decimal() {
return Err((format!("$limit: {} is not an int.", limit), args.span()).into());
}
2020-04-30 15:48:54 -04:00
if limit.is_zero() || limit.is_negative() {
return Err((
format!("$limit: Must be greater than 0, was {}.", limit),
args.span(),
)
.into());
}
let limit = match limit.to_integer().to_u32() {
Some(n) => n,
None => {
2020-04-30 15:48:54 -04:00
return Err((
format!("max must be in range 0 < max \u{2264} 2^32, was {}", limit),
2020-04-30 15:48:54 -04:00
args.span(),
)
.into())
2020-04-30 15:48:54 -04:00
}
};
2020-04-30 15:48:54 -04:00
let mut rng = rand::thread_rng();
Ok(Value::Dimension(
Number::from(rng.gen_range(0, limit) + 1),
Unit::None,
))
}
2020-04-30 15:48:54 -04:00
2020-05-16 18:01:06 -04:00
pub(crate) fn declare(f: &mut GlobalFunctionMap) {
2020-04-30 15:48:54 -04:00
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));
2020-02-09 18:28:24 -05:00
}