grass/src/builtin/math.rs

40 lines
1.4 KiB
Rust
Raw Normal View History

use std::collections::BTreeMap;
2020-02-02 21:09:29 -05:00
use super::Builtin;
use crate::units::Unit;
use crate::value::{Number, Value};
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
decl!(f "percentage", |args, _| {
2020-02-09 16:10:32 -05:00
let num = match arg!(args, 0, "number").eval() {
Value::Dimension(n, Unit::None) => n * Number::from(100),
_ => todo!("expected unitless number in builtin function `percentage()`")
};
Some(Value::Dimension(num, Unit::Percent))
});
2020-02-09 16:10:32 -05:00
decl!(f "round", |args, _| {
match arg!(args, 0, "number").eval() {
Value::Dimension(n, u) => Some(Value::Dimension(n.round(), u)),
_ => todo!("expected number in builtin function `round()`")
}
});
decl!(f "ceil", |args, _| {
match arg!(args, 0, "number").eval() {
Value::Dimension(n, u) => Some(Value::Dimension(n.ceil(), u)),
_ => todo!("expected number in builtin function `ceil()`")
}
});
decl!(f "floor", |args, _| {
match arg!(args, 0, "number").eval() {
Value::Dimension(n, u) => Some(Value::Dimension(n.floor(), u)),
_ => todo!("expected number in builtin function `floor()`")
}
});
2020-02-14 12:30:48 -05:00
decl!(f "abs", |args, _| {
match arg!(args, 0, "number").eval() {
Value::Dimension(n, u) => Some(Value::Dimension(n.abs(), u)),
_ => todo!("expected number in builtin function `abs()`")
}
});
2020-02-09 18:28:24 -05:00
}