Skip to main content

drasi_core/evaluation/functions/
metadata.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::variable_value::{zoned_datetime::ZonedDateTime, VariableValue};
16use crate::models::validate_effective_from;
17use async_trait::async_trait;
18use drasi_query_ast::ast;
19
20use crate::evaluation::{ExpressionEvaluationContext, FunctionError, FunctionEvaluationError};
21
22use super::ScalarFunction;
23
24#[derive(Debug)]
25pub struct ElementId {}
26
27#[async_trait]
28impl ScalarFunction for ElementId {
29    async fn call(
30        &self,
31        _context: &ExpressionEvaluationContext,
32        expression: &ast::FunctionExpression,
33        args: Vec<VariableValue>,
34    ) -> Result<VariableValue, FunctionError> {
35        if args.len() != 1 {
36            return Err(FunctionError {
37                function_name: expression.name.to_string(),
38                error: FunctionEvaluationError::InvalidArgumentCount,
39            });
40        }
41        match &args[0] {
42            VariableValue::Element(e) => Ok(VariableValue::String(
43                e.get_reference().element_id.to_string(),
44            )),
45            _ => Err(FunctionError {
46                function_name: expression.name.to_string(),
47                error: FunctionEvaluationError::InvalidArgument(0),
48            }),
49        }
50    }
51}
52
53#[derive(Debug)]
54pub struct ChangeDateTime {}
55
56#[async_trait]
57impl ScalarFunction for ChangeDateTime {
58    async fn call(
59        &self,
60        _context: &ExpressionEvaluationContext,
61        expression: &ast::FunctionExpression,
62        args: Vec<VariableValue>,
63    ) -> Result<VariableValue, FunctionError> {
64        if args.len() != 1 {
65            return Err(FunctionError {
66                function_name: expression.name.to_string(),
67                error: FunctionEvaluationError::InvalidArgumentCount,
68            });
69        }
70        match &args[0] {
71            VariableValue::Element(e) => {
72                let ts = e.get_effective_from();
73                debug_assert!(
74                    validate_effective_from(ts).is_ok(),
75                    "effective_from value {ts} appears to be in the wrong unit (expected milliseconds)",
76                );
77                Ok(VariableValue::ZonedDateTime(
78                    ZonedDateTime::from_epoch_millis(ts as i64),
79                ))
80            }
81            _ => Err(FunctionError {
82                function_name: expression.name.to_string(),
83                error: FunctionEvaluationError::InvalidArgument(0),
84            }),
85        }
86    }
87}