gitql_ast/types/
date.rs

1use std::any::Any;
2
3use crate::expression::Expr;
4use crate::expression::StringExpr;
5use crate::format_checker::is_valid_date_format;
6use crate::types::array::ArrayType;
7use crate::types::datetime::DateTimeType;
8use crate::types::integer::IntType;
9
10use super::base::DataType;
11
12#[derive(Clone)]
13pub struct DateType;
14
15impl DataType for DateType {
16    fn literal(&self) -> String {
17        "Date".to_string()
18    }
19
20    fn equals(&self, other: &Box<dyn DataType>) -> bool {
21        other.is_any() || other.is_date() || other.is_variant_with(|t| t.is_date())
22    }
23
24    fn as_any(&self) -> &dyn Any {
25        self
26    }
27
28    fn can_perform_add_op_with(&self) -> Vec<Box<dyn DataType>> {
29        vec![Box::new(IntType)]
30    }
31
32    fn add_op_result_type(&self, _other: &Box<dyn DataType>) -> Box<dyn DataType> {
33        Box::new(DateType)
34    }
35
36    fn can_perform_sub_op_with(&self) -> Vec<Box<dyn DataType>> {
37        vec![Box::new(IntType)]
38    }
39
40    fn sub_op_result_type(&self, _other: &Box<dyn DataType>) -> Box<dyn DataType> {
41        Box::new(DateType)
42    }
43
44    fn can_perform_eq_op_with(&self) -> Vec<Box<dyn DataType>> {
45        vec![Box::new(DateType)]
46    }
47
48    fn can_perform_group_eq_op_with(&self) -> Vec<Box<dyn DataType>> {
49        vec![Box::new(ArrayType::new(Box::new(DateType)))]
50    }
51
52    fn can_perform_bang_eq_op_with(&self) -> Vec<Box<dyn DataType>> {
53        vec![Box::new(DateType)]
54    }
55
56    fn can_perform_group_bang_eq_op_with(&self) -> Vec<Box<dyn DataType>> {
57        vec![Box::new(ArrayType::new(Box::new(DateType)))]
58    }
59
60    fn can_perform_gt_op_with(&self) -> Vec<Box<dyn DataType>> {
61        vec![Box::new(DateType)]
62    }
63
64    fn can_perform_group_gt_op_with(&self) -> Vec<Box<dyn DataType>> {
65        vec![Box::new(ArrayType::new(Box::new(DateType)))]
66    }
67
68    fn can_perform_gte_op_with(&self) -> Vec<Box<dyn DataType>> {
69        vec![Box::new(DateType)]
70    }
71
72    fn can_perform_group_gte_op_with(&self) -> Vec<Box<dyn DataType>> {
73        vec![Box::new(ArrayType::new(Box::new(DateType)))]
74    }
75
76    fn can_perform_lt_op_with(&self) -> Vec<Box<dyn DataType>> {
77        vec![Box::new(DateType)]
78    }
79
80    fn can_perform_group_lt_op_with(&self) -> Vec<Box<dyn DataType>> {
81        vec![Box::new(ArrayType::new(Box::new(DateType)))]
82    }
83
84    fn can_perform_lte_op_with(&self) -> Vec<Box<dyn DataType>> {
85        vec![Box::new(DateType)]
86    }
87
88    fn can_perform_group_lte_op_with(&self) -> Vec<Box<dyn DataType>> {
89        vec![Box::new(ArrayType::new(Box::new(DateType)))]
90    }
91
92    fn has_implicit_cast_from(&self, expr: &Box<dyn Expr>) -> bool {
93        if let Some(string_expr) = expr.as_any().downcast_ref::<StringExpr>() {
94            return is_valid_date_format(&string_expr.value);
95        }
96        false
97    }
98
99    fn can_perform_explicit_cast_op_to(&self) -> Vec<Box<dyn DataType>> {
100        vec![Box::new(DateTimeType)]
101    }
102}