1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use chrono::{NaiveDate, Utc};

use std::sync::Arc;

use super::{cel_type::*, value::*};
use crate::error::*;

pub(crate) fn date(args: Vec<CelValue>) -> Result<CelValue, CelError> {
    if args.is_empty() {
        return Ok(CelValue::Date(Utc::now().date_naive()));
    }

    let s: Arc<String> = assert_arg(args.first())?;
    Ok(CelValue::Date(NaiveDate::parse_from_str(&s, "%Y-%m-%d")?))
}

pub(crate) fn uuid(args: Vec<CelValue>) -> Result<CelValue, CelError> {
    let s: Arc<String> = assert_arg(args.first())?;
    Ok(CelValue::Uuid(
        s.parse()
            .map_err(|e| CelError::UuidError(format!("{e:?}")))?,
    ))
}

pub(crate) mod decimal {
    use rust_decimal::Decimal;

    use super::*;

    pub fn cast(args: Vec<CelValue>) -> Result<CelValue, CelError> {
        match args.first() {
            Some(CelValue::Decimal(d)) => Ok(CelValue::Decimal(*d)),
            Some(CelValue::String(s)) => Ok(CelValue::Decimal(
                s.parse()
                    .map_err(|e| CelError::DecimalError(format!("{e:?}")))?,
            )),
            Some(v) => Err(CelError::BadType(CelType::Decimal, CelType::from(v))),
            None => Err(CelError::MissingArgument),
        }
    }

    pub fn add(args: Vec<CelValue>) -> Result<CelValue, CelError> {
        let a: &Decimal = assert_arg(args.first())?;
        let b: &Decimal = assert_arg(args.get(1))?;
        Ok(CelValue::Decimal(a + b))
    }
}

fn assert_arg<'a, T: TryFrom<&'a CelValue, Error = CelError>>(
    arg: Option<&'a CelValue>,
) -> Result<T, CelError> {
    if let Some(v) = arg {
        T::try_from(v)
    } else {
        Err(CelError::MissingArgument)
    }
}