fhttp_core/preprocessing/
random_numbers.rs1#[cfg(test)]
2use std::cell::RefCell;
3use std::ops::Range;
4
5use anyhow::{anyhow, Result};
6
7use crate::preprocessing::evaluation::BaseEvaluation;
8
9#[cfg(test)]
10thread_local!(
11 pub static RANDOM_INT_CALLS: RefCell<Vec<(i32, i32)>> = const { RefCell::new(vec![]) }
12);
13
14#[cfg(not(test))]
15#[allow(unused)]
16pub fn random_int(min: i32, max: i32) -> i32 {
17 use rand::{thread_rng, Rng};
18
19 thread_rng().gen_range(min..max)
20}
21
22#[cfg(test)]
23#[allow(unused)]
24pub fn random_int(min: i32, max: i32) -> i32 {
25 RANDOM_INT_CALLS.with(|c| {
26 c.borrow_mut().push((min, max));
27 });
28 7i32
29}
30
31pub fn parse_min_max(min: Option<&str>, max: Option<&str>) -> Result<(i32, i32)> {
32 let ret_min = min
33 .map(|m| m.parse::<i32>())
34 .unwrap_or(Ok(0))
35 .map_err(|_| anyhow!("min param out of bounds: {}..{}", i32::MIN, i32::MAX))?;
36 let ret_max = max
37 .map(|m| m.parse::<i32>())
38 .unwrap_or(Ok(i32::MAX))
39 .map_err(|_| anyhow!("max param out of bounds: {}..{}", i32::MIN, i32::MAX))?;
40
41 if ret_max < ret_min {
42 Err(anyhow!("min cannot be greater than max"))
43 } else {
44 Ok((ret_min, ret_max))
45 }
46}
47
48pub struct RandomNumberEval<'a> {
49 pub min: Option<&'a str>,
50 pub max: Option<&'a str>,
51 pub base_eval: BaseEvaluation,
52}
53
54impl<'a> RandomNumberEval<'a> {
55 pub fn new(
56 min: Option<&'a str>,
57 max: Option<&'a str>,
58 range: Range<usize>,
59 backslashes: Range<usize>,
60 ) -> Self {
61 RandomNumberEval {
62 min,
63 max,
64 base_eval: BaseEvaluation::new(range, backslashes),
65 }
66 }
67}
68
69impl AsRef<BaseEvaluation> for RandomNumberEval<'_> {
70 fn as_ref(&self) -> &BaseEvaluation {
71 &self.base_eval
72 }
73}