gitql_ast/types/
boolean.rs

1use std::any::Any;
2
3use crate::expression::Expr;
4use crate::expression::StringExpr;
5use crate::types::array::ArrayType;
6use crate::types::integer::IntType;
7
8use super::base::DataType;
9
10#[derive(Clone)]
11pub struct BoolType;
12
13impl DataType for BoolType {
14    fn literal(&self) -> String {
15        "Boolean".to_string()
16    }
17
18    fn equals(&self, other: &Box<dyn DataType>) -> bool {
19        other.is_any() || other.is_bool() || other.is_variant_with(|t| t.is_bool())
20    }
21
22    fn as_any(&self) -> &dyn Any {
23        self
24    }
25
26    fn can_perform_bang_op(&self) -> bool {
27        true
28    }
29
30    fn bang_op_result_type(&self) -> Box<dyn DataType> {
31        Box::new(BoolType)
32    }
33
34    fn can_perform_logical_or_op_with(&self) -> Vec<Box<dyn DataType>> {
35        vec![Box::new(BoolType)]
36    }
37
38    fn logical_or_op_result_type(&self, _other: &Box<dyn DataType>) -> Box<dyn DataType> {
39        Box::new(self.clone())
40    }
41
42    fn can_perform_logical_and_op_with(&self) -> Vec<Box<dyn DataType>> {
43        vec![Box::new(BoolType)]
44    }
45
46    fn logical_and_op_result_type(&self, _other: &Box<dyn DataType>) -> Box<dyn DataType> {
47        Box::new(self.clone())
48    }
49
50    fn can_perform_logical_xor_op_with(&self) -> Vec<Box<dyn DataType>> {
51        vec![Box::new(BoolType)]
52    }
53
54    fn logical_xor_op_result_type(&self, _other: &Box<dyn DataType>) -> Box<dyn DataType> {
55        Box::new(self.clone())
56    }
57
58    fn can_perform_eq_op_with(&self) -> Vec<Box<dyn DataType>> {
59        vec![Box::new(BoolType)]
60    }
61
62    fn can_perform_group_eq_op_with(&self) -> Vec<Box<dyn DataType>> {
63        vec![Box::new(ArrayType::new(Box::new(BoolType)))]
64    }
65
66    fn can_perform_bang_eq_op_with(&self) -> Vec<Box<dyn DataType>> {
67        vec![Box::new(BoolType)]
68    }
69
70    fn can_perform_group_bang_eq_op_with(&self) -> Vec<Box<dyn DataType>> {
71        vec![Box::new(ArrayType::new(Box::new(BoolType)))]
72    }
73
74    fn can_perform_gt_op_with(&self) -> Vec<Box<dyn DataType>> {
75        vec![Box::new(BoolType)]
76    }
77
78    fn can_perform_group_gt_op_with(&self) -> Vec<Box<dyn DataType>> {
79        vec![Box::new(ArrayType::new(Box::new(BoolType)))]
80    }
81
82    fn can_perform_gte_op_with(&self) -> Vec<Box<dyn DataType>> {
83        vec![Box::new(BoolType)]
84    }
85
86    fn can_perform_group_gte_op_with(&self) -> Vec<Box<dyn DataType>> {
87        vec![Box::new(ArrayType::new(Box::new(BoolType)))]
88    }
89
90    fn can_perform_lt_op_with(&self) -> Vec<Box<dyn DataType>> {
91        vec![Box::new(BoolType)]
92    }
93
94    fn can_perform_group_lt_op_with(&self) -> Vec<Box<dyn DataType>> {
95        vec![Box::new(ArrayType::new(Box::new(BoolType)))]
96    }
97
98    fn can_perform_lte_op_with(&self) -> Vec<Box<dyn DataType>> {
99        vec![Box::new(BoolType)]
100    }
101
102    fn can_perform_group_lte_op_with(&self) -> Vec<Box<dyn DataType>> {
103        vec![Box::new(ArrayType::new(Box::new(BoolType)))]
104    }
105
106    fn not_op_result_type(&self) -> Box<dyn DataType> {
107        Box::new(self.clone())
108    }
109
110    fn has_implicit_cast_from(&self, expr: &Box<dyn Expr>) -> bool {
111        if let Some(string_expr) = expr.as_any().downcast_ref::<StringExpr>() {
112            const BOOLEANS_VALUES_LITERAL: [&str; 12] = [
113                // String representations for the “true” state
114                "t", "true", "y", "yes", "on", "1",
115                // String representations for the false state
116                "f", "false", "n", "no", "off", "0",
117            ];
118            return BOOLEANS_VALUES_LITERAL.contains(&string_expr.value.as_str());
119        }
120        false
121    }
122
123    fn can_perform_explicit_cast_op_to(&self) -> Vec<Box<dyn DataType>> {
124        vec![Box::new(IntType)]
125    }
126}