gitql_core/values/
interval.rs

1use std::any::Any;
2use std::cmp::Ordering;
3
4use gitql_ast::types::interval::IntervalType;
5use gitql_ast::types::DataType;
6use gitql_ast::Interval;
7
8use super::base::Value;
9use super::boolean::BoolValue;
10
11#[derive(Clone)]
12pub struct IntervalValue {
13    pub interval: Interval,
14}
15
16impl IntervalValue {
17    pub fn new(interval: Interval) -> Self {
18        IntervalValue { interval }
19    }
20}
21
22impl Value for IntervalValue {
23    fn literal(&self) -> String {
24        self.interval.to_string()
25    }
26
27    fn equals(&self, other: &Box<dyn Value>) -> bool {
28        if let Some(other_date) = other.as_any().downcast_ref::<IntervalValue>() {
29            return self.interval == other_date.interval;
30        }
31        false
32    }
33
34    fn compare(&self, other: &Box<dyn Value>) -> Option<Ordering> {
35        if let Some(other_interval) = other.as_any().downcast_ref::<IntervalValue>() {
36            return self.interval.partial_cmp(&other_interval.interval);
37        }
38        None
39    }
40
41    fn data_type(&self) -> Box<dyn DataType> {
42        Box::new(IntervalType)
43    }
44
45    fn as_any(&self) -> &dyn Any {
46        self
47    }
48
49    fn eq_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
50        if let Some(other_interval) = other.as_any().downcast_ref::<IntervalValue>() {
51            let is_equals = self.interval == other_interval.interval;
52            return Ok(Box::new(BoolValue::new(is_equals)));
53        }
54        Err("Unexpected type to perform `=` with".to_string())
55    }
56
57    fn bang_eq_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
58        if let Some(other_interval) = other.as_any().downcast_ref::<IntervalValue>() {
59            let is_not_equals = self.interval != other_interval.interval;
60            return Ok(Box::new(BoolValue::new(is_not_equals)));
61        }
62        Err("Unexpected type to perform `!=` with".to_string())
63    }
64
65    fn gt_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
66        if let Some(other_interval) = other.as_any().downcast_ref::<IntervalValue>() {
67            let result = self.interval.gt(&other_interval.interval);
68            return Ok(Box::new(BoolValue::new(result)));
69        }
70        Err("Unexpected type to perform `>` with".to_string())
71    }
72
73    fn gte_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
74        if let Some(other_interval) = other.as_any().downcast_ref::<IntervalValue>() {
75            let result = self.interval.ge(&other_interval.interval);
76            return Ok(Box::new(BoolValue::new(result)));
77        }
78        Err("Unexpected type to perform `>=` with".to_string())
79    }
80
81    fn lt_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
82        if let Some(other_interval) = other.as_any().downcast_ref::<IntervalValue>() {
83            let result = self.interval.lt(&other_interval.interval);
84            return Ok(Box::new(BoolValue::new(result)));
85        }
86        Err("Unexpected type to perform `<` with".to_string())
87    }
88
89    fn lte_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
90        if let Some(other_interval) = other.as_any().downcast_ref::<IntervalValue>() {
91            let result = self.interval.le(&other_interval.interval);
92            return Ok(Box::new(BoolValue::new(result)));
93        }
94        Err("Unexpected type to perform `<=` with".to_string())
95    }
96
97    fn add_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
98        if let Some(other_interval) = other.as_any().downcast_ref::<IntervalValue>() {
99            let interval = self.interval.add(&other_interval.interval)?;
100            return Ok(Box::new(IntervalValue::new(interval)));
101        }
102        Err("Unexpected type to perform `+` with".to_string())
103    }
104
105    fn sub_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
106        if let Some(other_interval) = other.as_any().downcast_ref::<IntervalValue>() {
107            let interval = self.interval.sub(&other_interval.interval)?;
108            return Ok(Box::new(IntervalValue::new(interval)));
109        }
110        Err("Unexpected type to perform `-` with".to_string())
111    }
112
113    fn mul_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
114        if let Some(value) = other.as_int() {
115            let interval = self.interval.mul(value)?;
116            return Ok(Box::new(IntervalValue::new(interval)));
117        }
118        Err("Unexpected type to perform `*` with".to_string())
119    }
120
121    fn div_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
122        if let Some(value) = other.as_int() {
123            let interval = self.interval.div(value)?;
124            return Ok(Box::new(IntervalValue::new(interval)));
125        }
126        Err("Unexpected type to perform `/` with".to_string())
127    }
128}