From 116bb43bd549fd553fa9009f0a014159a1da055d Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 9 Feb 2020 16:10:32 -0500 Subject: [PATCH] Implement builtin function `round()` --- src/builtin/math.rs | 9 +++++++-- tests/math.rs | 10 ++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/builtin/math.rs b/src/builtin/math.rs index 25a1f7b..5ea9bb6 100644 --- a/src/builtin/math.rs +++ b/src/builtin/math.rs @@ -6,11 +6,16 @@ use crate::value::{Number, Value}; pub(crate) fn register(f: &mut BTreeMap) { decl!(f "percentage", |args, _| { - let arg = dbg!(arg!(args, 0, "number").eval()); - let num = match arg { + 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)) }); + 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()`") + } + }); } \ No newline at end of file diff --git a/tests/math.rs b/tests/math.rs index 68b1e32..65fe25a 100644 --- a/tests/math.rs +++ b/tests/math.rs @@ -18,3 +18,13 @@ test!( "a {\n color: percentage(2);\n}\n", "a {\n color: 200%;\n}\n" ); +test!( + rounds_down, + "a {\n color: round(10.4px);\n}\n", + "a {\n color: 10px;\n}\n" +); +test!( + rounds_up, + "a {\n color: round(10.6px);\n}\n", + "a {\n color: 11px;\n}\n" +);