gitql_core/values/
base.rsuse std::any::Any;
use std::cmp::Ordering;
use std::fmt;
use dyn_clone::DynClone;
use gitql_ast::types::base::DataType;
use gitql_ast::Interval;
use super::array::ArrayValue;
use super::boolean::BoolValue;
use super::composite::CompositeValue;
use super::date::DateValue;
use super::datetime::DateTimeValue;
use super::float::FloatValue;
use super::integer::IntValue;
use super::interval::IntervalValue;
use super::null::NullValue;
use super::range::RangeValue;
use super::text::TextValue;
use super::time::TimeValue;
dyn_clone::clone_trait_object!(Value);
pub trait Value: DynClone {
fn literal(&self) -> String;
#[allow(clippy::borrowed_box)]
fn equals(&self, other: &Box<dyn Value>) -> bool;
#[allow(clippy::borrowed_box)]
fn compare(&self, other: &Box<dyn Value>) -> Option<Ordering>;
fn data_type(&self) -> Box<dyn DataType>;
fn as_any(&self) -> &dyn Any;
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn add_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn sub_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn mul_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn div_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn rem_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn caret_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn or_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn and_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn xor_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn logical_or_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn logical_and_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn logical_xor_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn shl_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn shr_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn index_op(&self, index: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn slice_op(
&self,
start: &Option<Box<dyn Value>>,
end: &Option<Box<dyn Value>>,
) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn eq_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn bang_eq_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn null_safe_eq_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn gt_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn gte_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn lt_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn lte_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
fn not_op(&self) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
fn neg_op(&self) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
fn bang_op(&self) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn contains_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn like_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn glob_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn regexp_op(&self, other: &Box<dyn Value>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
#[allow(unused_variables)]
#[allow(clippy::borrowed_box)]
fn cast_op(&self, target_type: &Box<dyn DataType>) -> Result<Box<dyn Value>, String> {
Err("Unsupported operator for this type".to_string())
}
}
impl dyn Value {
pub fn is_text(&self) -> bool {
self.as_any().downcast_ref::<TextValue>().is_some()
}
pub fn as_text(&self) -> Option<String> {
if let Some(text_value) = self.as_any().downcast_ref::<TextValue>() {
return Some(text_value.value.to_string());
}
None
}
pub fn is_int(&self) -> bool {
self.as_any().downcast_ref::<IntValue>().is_some()
}
pub fn as_int(&self) -> Option<i64> {
if let Some(int_value) = self.as_any().downcast_ref::<IntValue>() {
return Some(int_value.value);
}
None
}
pub fn is_float(&self) -> bool {
self.as_any().downcast_ref::<FloatValue>().is_some()
}
pub fn as_float(&self) -> Option<f64> {
if let Some(float_value) = self.as_any().downcast_ref::<FloatValue>() {
return Some(float_value.value);
}
None
}
pub fn is_number(&self) -> bool {
self.is_int() || self.is_float()
}
pub fn is_bool(&self) -> bool {
self.as_any().downcast_ref::<BoolValue>().is_some()
}
pub fn as_bool(&self) -> Option<bool> {
if let Some(bool_value) = self.as_any().downcast_ref::<BoolValue>() {
return Some(bool_value.value);
}
None
}
pub fn is_date(&self) -> bool {
self.as_any().downcast_ref::<DateValue>().is_some()
}
pub fn as_date(&self) -> Option<i64> {
if let Some(date_value) = self.as_any().downcast_ref::<DateValue>() {
return Some(date_value.timestamp);
}
None
}
pub fn is_time(&self) -> bool {
self.as_any().downcast_ref::<TimeValue>().is_some()
}
pub fn as_time(&self) -> Option<String> {
if let Some(time_value) = self.as_any().downcast_ref::<DateValue>() {
return Some(time_value.timestamp.to_string());
}
None
}
pub fn is_date_time(&self) -> bool {
self.as_any().downcast_ref::<DateTimeValue>().is_some()
}
pub fn as_date_time(&self) -> Option<i64> {
if let Some(date_time_value) = self.as_any().downcast_ref::<DateTimeValue>() {
return Some(date_time_value.value);
}
None
}
pub fn is_interval(&self) -> bool {
self.as_any().downcast_ref::<IntervalValue>().is_some()
}
pub fn as_interval(&self) -> Option<Interval> {
if let Some(interval_value) = self.as_any().downcast_ref::<IntervalValue>() {
return Some(interval_value.interval.clone());
}
None
}
pub fn is_array(&self) -> bool {
self.as_any().downcast_ref::<ArrayValue>().is_some()
}
pub fn as_array(&self) -> Option<Vec<Box<dyn Value>>> {
if let Some(array_value) = self.as_any().downcast_ref::<ArrayValue>() {
return Some(array_value.values.clone());
}
None
}
pub fn is_range(&self) -> bool {
self.as_any().downcast_ref::<RangeValue>().is_some()
}
pub fn as_range(&self) -> Option<(Box<dyn Value>, Box<dyn Value>)> {
if let Some(range_value) = self.as_any().downcast_ref::<RangeValue>() {
return Some((range_value.start.clone(), range_value.end.clone()));
}
None
}
pub fn is_null(&self) -> bool {
self.as_any().downcast_ref::<NullValue>().is_some()
}
pub fn is_composite(&self) -> bool {
self.as_any().downcast_ref::<CompositeValue>().is_some()
}
}
impl fmt::Display for Box<dyn Value> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.literal())
}
}
impl PartialEq for Box<dyn Value> {
fn eq(&self, other: &Self) -> bool {
self.equals(other)
}
}
impl PartialOrd for Box<dyn Value> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.compare(other)
}
}