use std::fmt::{Debug, Display};
use std::sync::Arc;
use crate::core::error::{Error, Result};
pub trait DataValue: Debug + Send + Sync {
fn type_name(&self) -> &'static str;
fn to_string(&self) -> String;
fn clone_boxed(&self) -> Box<dyn DataValue>;
fn equals(&self, other: &dyn DataValue) -> bool;
fn as_any(&self) -> &dyn Any;
}
impl DataValue for i64 {
fn type_name(&self) -> &'static str {
"i64"
}
fn to_string(&self) -> String {
format!("{}", self)
}
fn clone_boxed(&self) -> Box<dyn DataValue> {
Box::new(*self)
}
fn equals(&self, other: &dyn DataValue) -> bool {
if let Some(other) = other.as_any().downcast_ref::<i64>() {
return self == other;
}
false
}
fn as_any(&self) -> &dyn Any {
self
}
}
impl DataValue for f64 {
fn type_name(&self) -> &'static str {
"f64"
}
fn to_string(&self) -> String {
format!("{}", self)
}
fn clone_boxed(&self) -> Box<dyn DataValue> {
Box::new(*self)
}
fn equals(&self, other: &dyn DataValue) -> bool {
if let Some(other) = other.as_any().downcast_ref::<f64>() {
return (self - other).abs() < f64::EPSILON;
}
false
}
fn as_any(&self) -> &dyn Any {
self
}
}
impl DataValue for bool {
fn type_name(&self) -> &'static str {
"bool"
}
fn to_string(&self) -> String {
format!("{}", self)
}
fn clone_boxed(&self) -> Box<dyn DataValue> {
Box::new(*self)
}
fn equals(&self, other: &dyn DataValue) -> bool {
if let Some(other) = other.as_any().downcast_ref::<bool>() {
return self == other;
}
false
}
fn as_any(&self) -> &dyn Any {
self
}
}
impl DataValue for String {
fn type_name(&self) -> &'static str {
"String"
}
fn to_string(&self) -> String {
self.clone()
}
fn clone_boxed(&self) -> Box<dyn DataValue> {
Box::new(self.clone())
}
fn equals(&self, other: &dyn DataValue) -> bool {
if let Some(other) = other.as_any().downcast_ref::<String>() {
return self == other;
}
false
}
fn as_any(&self) -> &dyn Any {
self
}
}
impl DataValue for Arc<str> {
fn type_name(&self) -> &'static str {
"Arc<str>"
}
fn to_string(&self) -> String {
format!("{}", self)
}
fn clone_boxed(&self) -> Box<dyn DataValue> {
Box::new(self.clone())
}
fn equals(&self, other: &dyn DataValue) -> bool {
if let Some(other) = other.as_any().downcast_ref::<Arc<str>>() {
return self == other;
}
false
}
fn as_any(&self) -> &dyn Any {
self
}
}
use std::any::Any;
pub trait DataValueExt: DataValue {}
impl<T: DataValue + 'static> DataValueExt for T {}
pub trait DisplayExt: Display {
fn display_with_format(&self, format: &str) -> String;
}
impl<T: Display> DisplayExt for T {
fn display_with_format(&self, format: &str) -> String {
match format {
"" => format!("{}", self),
_ => format!("{}", self), }
}
}