2020-03-03 19:51:02 -05:00
|
|
|
use std::collections::HashMap;
|
2020-02-02 21:09:29 -05:00
|
|
|
|
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-03-03 19:51:02 -05:00
|
|
|
pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
|
2020-03-16 10:35:38 -04:00
|
|
|
f.insert(
|
|
|
|
"percentage".to_owned(),
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-03-16 10:35:38 -04:00
|
|
|
max_args!(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(..) => {
|
|
|
|
return Err(format!("$number: Expected {} to have no units.", v).into())
|
|
|
|
}
|
|
|
|
v => return Err(format!("$number: {} is not a number.", v).into()),
|
|
|
|
};
|
|
|
|
Ok(Value::Dimension(num, Unit::Percent))
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
f.insert(
|
|
|
|
"round".to_owned(),
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-03-16 10:35:38 -04:00
|
|
|
max_args!(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)),
|
|
|
|
v => Err(format!("$number: {} is not a number.", v).into()),
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
f.insert(
|
|
|
|
"ceil".to_owned(),
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-03-16 10:35:38 -04:00
|
|
|
max_args!(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)),
|
|
|
|
v => Err(format!("$number: {} is not a number.", v).into()),
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
f.insert(
|
|
|
|
"floor".to_owned(),
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-03-16 10:35:38 -04:00
|
|
|
max_args!(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)),
|
|
|
|
v => Err(format!("$number: {} is not a number.", v).into()),
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
f.insert(
|
|
|
|
"abs".to_owned(),
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-03-16 10:35:38 -04:00
|
|
|
max_args!(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)),
|
|
|
|
v => Err(format!("$number: {} is not a number.", v).into()),
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
f.insert(
|
|
|
|
"comparable".to_owned(),
|
2020-04-04 18:17:04 -04:00
|
|
|
Builtin::new(|mut args, scope, super_selector| {
|
2020-03-16 10:35:38 -04:00
|
|
|
max_args!(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,
|
|
|
|
v => return Err(format!("$number1: {} is not a number.", v).into()),
|
|
|
|
};
|
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,
|
|
|
|
v => return Err(format!("$number2: {} is not a number.", v).into()),
|
|
|
|
};
|
2020-02-15 08:16:17 -05:00
|
|
|
|
2020-03-16 10:35:38 -04:00
|
|
|
Ok(Value::bool(unit1.comparable(&unit2)))
|
|
|
|
}),
|
|
|
|
);
|
2020-02-09 18:28:24 -05:00
|
|
|
}
|