vrl/stdlib/
random_bool.rs

1use crate::compiler::prelude::*;
2use rand::random;
3
4#[allow(clippy::unnecessary_wraps)] // match other VRL function implementations
5fn random_bool() -> Resolved {
6    let b: bool = random();
7
8    Ok(Value::Boolean(b))
9}
10
11#[derive(Clone, Copy, Debug)]
12pub struct RandomBool;
13
14impl Function for RandomBool {
15    fn identifier(&self) -> &'static str {
16        "random_bool"
17    }
18
19    fn usage(&self) -> &'static str {
20        "Returns a random boolean."
21    }
22
23    fn category(&self) -> &'static str {
24        Category::Random.as_ref()
25    }
26
27    fn return_kind(&self) -> u16 {
28        kind::BOOLEAN
29    }
30
31    fn parameters(&self) -> &'static [Parameter] {
32        &[]
33    }
34
35    fn examples(&self) -> &'static [Example] {
36        &[example! {
37            title: "Random boolean",
38            source: "is_boolean(random_bool())",
39            result: Ok("true"),
40        }]
41    }
42
43    fn compile(
44        &self,
45        _state: &state::TypeState,
46        _ctx: &mut FunctionCompileContext,
47        _arguments: ArgumentList,
48    ) -> Compiled {
49        Ok(RandomBoolFn {}.as_expr())
50    }
51}
52
53#[derive(Debug, Clone)]
54struct RandomBoolFn {}
55
56impl FunctionExpression for RandomBoolFn {
57    fn resolve(&self, _ctx: &mut Context) -> Resolved {
58        random_bool()
59    }
60
61    fn type_def(&self, _state: &state::TypeState) -> TypeDef {
62        TypeDef::boolean().infallible()
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    // tests are handled by examples
69}