From 40e1e554c7b0fd64835533b4b997661353a591a1 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 9 Feb 2020 16:14:24 -0500 Subject: [PATCH] Implement builtin functions `ceil()` and `floor()` --- src/builtin/math.rs | 12 ++++++++++++ src/value/number.rs | 12 ++++++++++++ tests/math.rs | 20 ++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/src/builtin/math.rs b/src/builtin/math.rs index 5ea9bb6..b9a1ec7 100644 --- a/src/builtin/math.rs +++ b/src/builtin/math.rs @@ -18,4 +18,16 @@ pub(crate) fn register(f: &mut BTreeMap) { _ => 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()`") + } + }); } \ No newline at end of file diff --git a/src/value/number.rs b/src/value/number.rs index 7ba24be..c456430 100644 --- a/src/value/number.rs +++ b/src/value/number.rs @@ -30,6 +30,18 @@ impl Number { val: self.val.round(), } } + + pub fn ceil(self) -> Self { + Number { + val: self.val.ceil(), + } + } + + pub fn floor(self) -> Self { + Number { + val: self.val.floor(), + } + } } impl fmt::LowerHex for Number { diff --git a/tests/math.rs b/tests/math.rs index 65fe25a..4218c2d 100644 --- a/tests/math.rs +++ b/tests/math.rs @@ -28,3 +28,23 @@ test!( "a {\n color: round(10.6px);\n}\n", "a {\n color: 11px;\n}\n" ); +test!( + floor_below_pt_5, + "a {\n color: floor(10.4px);\n}\n", + "a {\n color: 10px;\n}\n" +); +test!( + floor_above_pt_5, + "a {\n color: floor(10.6px);\n}\n", + "a {\n color: 10px;\n}\n" +); +test!( + ceil_below_pt_5, + "a {\n color: ceil(10.4px);\n}\n", + "a {\n color: 11px;\n}\n" +); +test!( + ceil_above_pt_5, + "a {\n color: ceil(10.6px);\n}\n", + "a {\n color: 11px;\n}\n" +);