gitql_ast/types/
array.rs

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
58
59
60
use std::any::Any;

use super::base::DataType;
use super::boolean::BoolType;
use super::integer::IntType;

#[derive(Clone)]
pub struct ArrayType {
    pub base: Box<dyn DataType>,
}

impl DataType for ArrayType {
    fn literal(&self) -> String {
        format!("Array({})", self.base.literal())
    }

    fn equals(&self, other: &Box<dyn DataType>) -> bool {
        let array_type: Box<dyn DataType> = Box::new(self.clone());
        if other.is_any() || other.is_variant_contains(&array_type) {
            return true;
        }

        if let Some(other_array) = other.as_any().downcast_ref::<ArrayType>() {
            return self.base.equals(&other_array.base);
        }
        false
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn can_perform_index_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![Box::new(IntType)]
    }

    fn index_op_result_type(&self, _other: &Box<dyn DataType>) -> Box<dyn DataType> {
        self.base.clone()
    }

    fn can_perform_slice_op(&self) -> bool {
        true
    }

    fn can_perform_slice_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![Box::new(IntType)]
    }

    fn can_perform_contains_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![Box::new(self.clone()), self.base.clone()]
    }

    fn can_perform_logical_or_op_with(&self) -> Vec<Box<dyn DataType>> {
        vec![Box::new(self.clone())]
    }

    fn logical_or_op_result_type(&self, _other: &Box<dyn DataType>) -> Box<dyn DataType> {
        Box::new(BoolType)
    }
}