Skip to content

Commit

Permalink
fix: Comparions in Date, Time and DateTime values
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Nov 8, 2024
1 parent c06ffc9 commit 0ba8b76
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
49 changes: 49 additions & 0 deletions crates/gitql-core/src/values/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::any::Any;
use std::cmp::Ordering;

use super::base::Value;
use super::boolean::BoolValue;

use chrono::DateTime;
use gitql_ast::types::base::DataType;
Expand Down Expand Up @@ -41,4 +42,52 @@ impl Value for DateValue {
fn as_any(&self) -> &dyn Any {
self
}

fn eq_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<DateValue>() {
let are_equals = self.value == other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `=` with".to_string())
}

fn bang_eq_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<DateValue>() {
let are_equals = self.value != other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `!=` with".to_string())
}

fn gt_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<DateValue>() {
let are_equals = self.value > other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `>` with".to_string())
}

fn gte_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<DateValue>() {
let are_equals = self.value >= other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `>=` with".to_string())
}

fn lt_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<DateValue>() {
let are_equals = self.value < other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `<` with".to_string())
}

fn lte_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<DateValue>() {
let are_equals = self.value <= other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `<=` with".to_string())
}
}
49 changes: 49 additions & 0 deletions crates/gitql-core/src/values/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::any::Any;
use std::cmp::Ordering;

use super::base::Value;
use super::boolean::BoolValue;

use chrono::DateTime;
use gitql_ast::types::base::DataType;
Expand Down Expand Up @@ -41,4 +42,52 @@ impl Value for DateTimeValue {
fn as_any(&self) -> &dyn Any {
self
}

fn eq_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<DateTimeValue>() {
let are_equals = self.value == other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `=` with".to_string())
}

fn bang_eq_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<DateTimeValue>() {
let are_equals = self.value != other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `!=` with".to_string())
}

fn gt_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<DateTimeValue>() {
let are_equals = self.value > other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `>` with".to_string())
}

fn gte_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<DateTimeValue>() {
let are_equals = self.value >= other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `>=` with".to_string())
}

fn lt_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<DateTimeValue>() {
let are_equals = self.value < other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `<` with".to_string())
}

fn lte_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<DateTimeValue>() {
let are_equals = self.value <= other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `<=` with".to_string())
}
}
49 changes: 49 additions & 0 deletions crates/gitql-core/src/values/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use gitql_ast::types::base::DataType;
use gitql_ast::types::time::TimeType;

use super::base::Value;
use super::boolean::BoolValue;

#[derive(Clone)]
pub struct TimeValue {
Expand Down Expand Up @@ -37,4 +38,52 @@ impl Value for TimeValue {
fn as_any(&self) -> &dyn Any {
self
}

fn eq_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<TimeValue>() {
let are_equals = self.value == other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `=` with".to_string())
}

fn bang_eq_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<TimeValue>() {
let are_equals = self.value != other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `!=` with".to_string())
}

fn gt_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<TimeValue>() {
let are_equals = self.value > other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `>` with".to_string())
}

fn gte_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<TimeValue>() {
let are_equals = self.value >= other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `>=` with".to_string())
}

fn lt_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<TimeValue>() {
let are_equals = self.value < other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `<` with".to_string())
}

fn lte_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
if let Some(other_text) = other.as_any().downcast_ref::<TimeValue>() {
let are_equals = self.value <= other_text.value;
return Ok(Box::new(BoolValue { value: are_equals }));
}
Err("Unexpected type to perform `<=` with".to_string())
}
}

0 comments on commit 0ba8b76

Please sign in to comment.