From c8a59ea501cce481f489ddd62dfd7c0af728ce6c Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 5 Apr 2020 13:42:53 -0400 Subject: [PATCH] implement builtin function random --- README.md | 2 +- src/builtin/math.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++ src/value/number.rs | 1 + 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e9cbf3d..0857084 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ The large features remaining are order of operations special case certain functions (min, max, calc, element, expression, progid, url) all builtin selector functions (274 tests) -content-exists, unique-id, random, min, min +content-exists, unique-id, min, min @extend (~600 tests) indented syntax (27 tests) a special parser for plain css diff --git a/src/builtin/math.rs b/src/builtin/math.rs index bfd77e8..4f01594 100644 --- a/src/builtin/math.rs +++ b/src/builtin/math.rs @@ -1,5 +1,10 @@ use std::collections::HashMap; +#[cfg(feature = "random")] +use num_traits::{One, Signed, ToPrimitive, Zero}; +#[cfg(feature = "random")] +use rand::Rng; + use super::Builtin; use crate::unit::Unit; use crate::value::{Number, Value}; @@ -75,4 +80,50 @@ pub(crate) fn register(f: &mut HashMap) { Ok(Value::bool(unit1.comparable(&unit2))) }), ); + // TODO: write tests for this. how? + #[cfg(feature = "random")] + f.insert( + "random".to_owned(), + Builtin::new(|mut args, scope, super_selector| { + max_args!(args, 1); + let limit = match arg!(args, scope, super_selector, 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, + )); + } + v => return Err(format!("$limit: {} is not a number.", v).into()), + }; + + 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).into()); + } + + if limit.is_zero() || limit.is_negative() { + return Err(format!("$limit: Must be greater than 0, was {}.", limit).into()); + } + + let limit = match limit.to_integer().to_u32() { + Some(n) => n, + None => { + return Err( + format!("max must be in range 0 < max ≤ 2^32, was {}", limit).into(), + ) + } + }; + + let mut rng = rand::thread_rng(); + Ok(Value::Dimension( + Number::from(rng.gen_range(0, limit)), + Unit::None, + )) + }), + ); } diff --git a/src/value/number.rs b/src/value/number.rs index d0f433c..4f1ecba 100644 --- a/src/value/number.rs +++ b/src/value/number.rs @@ -161,6 +161,7 @@ impl From for Number { from_integer!(u16); from_integer!(usize); from_integer!(i32); +from_integer!(u32); from_integer!(u8); impl Display for Number {