drasi_core/evaluation/functions/drasi/
min.rs

1// Copyright 2024 The Drasi Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::evaluation::functions::ScalarFunction;
16use crate::evaluation::{FunctionError, FunctionEvaluationError};
17use async_trait::async_trait;
18use drasi_query_ast::ast;
19
20use crate::evaluation::{variable_value::VariableValue, ExpressionEvaluationContext};
21
22#[derive(Clone)]
23pub struct DrasiMin {}
24
25#[async_trait]
26impl ScalarFunction for DrasiMin {
27    async fn call(
28        &self,
29        _context: &ExpressionEvaluationContext,
30        expression: &ast::FunctionExpression,
31        args: Vec<VariableValue>,
32    ) -> Result<VariableValue, FunctionError> {
33        if args.len() != 1 {
34            return Err(FunctionError {
35                function_name: expression.name.to_string(),
36                error: FunctionEvaluationError::InvalidArgumentCount,
37            });
38        }
39        match &args[0] {
40            VariableValue::List(l) => {
41                let mut result = l[0].clone();
42                for element in l {
43                    if element == &VariableValue::Null {
44                        continue;
45                    }
46                    if element < &result {
47                        result = element.clone();
48                    }
49                }
50                Ok(result.clone())
51            }
52            VariableValue::Null => Ok(VariableValue::Null),
53            VariableValue::String(s) => Ok(VariableValue::String(s.clone())),
54            VariableValue::Float(f) => Ok(VariableValue::Float(f.clone())),
55            VariableValue::Integer(i) => Ok(VariableValue::Integer(i.clone())),
56            VariableValue::Bool(b) => Ok(VariableValue::Bool(*b)),
57            VariableValue::Object(o) => Ok(VariableValue::Object(o.clone())),
58            VariableValue::Date(d) => Ok(VariableValue::Date(*d)),
59            VariableValue::LocalTime(t) => Ok(VariableValue::LocalTime(*t)),
60            VariableValue::LocalDateTime(dt) => Ok(VariableValue::LocalDateTime(*dt)),
61            VariableValue::ZonedTime(t) => Ok(VariableValue::ZonedTime(*t)),
62            VariableValue::ZonedDateTime(dt) => Ok(VariableValue::ZonedDateTime(dt.clone())),
63            VariableValue::Duration(d) => Ok(VariableValue::Duration(d.clone())),
64            _ => Err(FunctionError {
65                function_name: expression.name.to_string(),
66                error: FunctionEvaluationError::InvalidArgument(0),
67            }),
68        }
69    }
70}