gitql_ast/types/
composite.rs

1use std::any::Any;
2use std::collections::HashMap;
3
4use super::base::DataType;
5
6#[derive(Clone)]
7pub struct CompositeType {
8    pub name: String,
9    pub members: HashMap<String, Box<dyn DataType>>,
10}
11
12impl CompositeType {
13    pub fn new(name: String, members: HashMap<String, Box<dyn DataType>>) -> Self {
14        CompositeType { name, members }
15    }
16
17    pub fn empty(name: String) -> Self {
18        CompositeType {
19            name,
20            members: HashMap::default(),
21        }
22    }
23
24    pub fn add_member(mut self, name: String, data_type: Box<dyn DataType>) -> Self {
25        self.members.insert(name, data_type);
26        self
27    }
28}
29
30impl DataType for CompositeType {
31    fn literal(&self) -> String {
32        self.name.to_string()
33    }
34
35    fn equals(&self, other: &Box<dyn DataType>) -> bool {
36        if other.is_any() {
37            return true;
38        }
39
40        let composite_type: Box<dyn DataType> = Box::new(self.clone());
41        if other.is_variant_contains(&composite_type) {
42            return true;
43        }
44
45        if let Some(other_composite) = other.as_any().downcast_ref::<CompositeType>() {
46            if self.name.ne(&other_composite.name) {
47                return false;
48            }
49
50            return self.members.eq(&other_composite.members);
51        }
52
53        false
54    }
55
56    fn as_any(&self) -> &dyn Any {
57        self
58    }
59}