drasi_core/evaluation/functions/numeric/
random.rs1use async_trait::async_trait;
16use drasi_query_ast::ast;
17use rand::prelude::*;
18
19use crate::evaluation::functions::ScalarFunction;
20use crate::evaluation::variable_value::float::Float;
21use crate::evaluation::variable_value::VariableValue;
22use crate::evaluation::{ExpressionEvaluationContext, FunctionError, FunctionEvaluationError};
23
24#[derive(Debug)]
25pub struct Rand {}
26
27#[async_trait]
28impl ScalarFunction for Rand {
29 async fn call(
30 &self,
31 _context: &ExpressionEvaluationContext,
32 expression: &ast::FunctionExpression,
33 args: Vec<VariableValue>,
34 ) -> Result<VariableValue, FunctionError> {
35 if !args.is_empty() {
36 return Err(FunctionError {
37 function_name: expression.name.to_string(),
38 error: FunctionEvaluationError::InvalidArgumentCount,
39 });
40 }
41 let mut rng = rand::thread_rng();
42 Ok(VariableValue::Float(match Float::from_f64(rng.gen()) {
43 Some(f) => f,
44 None => {
45 return Err(FunctionError {
46 function_name: expression.name.to_string(),
47 error: FunctionEvaluationError::OverflowError,
48 })
49 }
50 }))
51 }
52}