Skip to content

Commit 462a1f8

Browse files
committed
Progress
1 parent 2715fba commit 462a1f8

27 files changed

Lines changed: 60 additions & 226 deletions

File tree

src/data/value/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use {
66
thousands::Separable,
77
};
88

9-
pub trait Cast<Output> {
9+
pub trait Cast<Output: Sized> {
1010
fn cast(self) -> Result<Output> {
1111
Err(Error::Value(ValueError::UnimplementedCast))
1212
}

src/data/value/declare.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use {
2+
super::methods::{BinaryOperations, UnaryOperations},
23
crate::{BigEndian, Cast},
34
enum_dispatch::enum_dispatch,
45
serde::{Deserialize, Serialize},
56
std::fmt::Debug,
67
};
78

9+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd)]
810
pub struct Null;
911

1012
//#[enum_dispatch(Value)]
@@ -14,14 +16,17 @@ pub struct Null;
1416
macro_rules! value_types {
1517
( $($representation:ident: $type:ty),* ) => {
1618
//#[enum_dispatch(Value)]
17-
pub trait Valued: BigEndian $(+ Cast<$type>)* {}
19+
pub trait Valued: BigEndian + BinaryOperations + UnaryOperations $(+ Cast<$type>)* {}
1820
//$(impl Valued for $type {})*
1921

20-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd, Eq)]
22+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd)]
2123
#[enum_dispatch(Valued)]
2224
pub enum Value {
2325
$($representation($type)),*
2426
}
27+
impl Value {
28+
pub const NULL: Self = Self::Null(Null);
29+
}
2530
}
2631
}
2732

src/data/value/literal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl<'a> TryFrom<&'a AstValue> for Value {
1919
)
2020
.map_err(|_| ValueError::FailedToParseNumber.into()),
2121
AstValue::SingleQuotedString(value) => Ok(Value::Str(value.clone())),
22-
AstValue::Null => Ok(Value::Null),
22+
AstValue::NULL => Ok(Value::NULL),
2323
_ => Err(ValueError::UnimplementedLiteralType.into()),
2424
}
2525
}

src/data/value/methods/aggregate.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ use {
1414
impl Value {
1515
pub fn aggregate_count(self, other: Value) -> Result<Value> {
1616
Ok(Value::Internal(match (self, other) {
17-
(Value::Null, Value::Null) => 0,
17+
(Value::Null(_), Value::Null(_)) => 0,
1818
(Value::Internal(self_val), Value::Internal(other_val)) => self_val + other_val,
19-
(Value::Internal(val), Value::Null) | (Value::Null, Value::Internal(val)) => val,
19+
(Value::Internal(val), Value::Null(_)) | (Value::Null(_), Value::Internal(val)) => val,
2020
(Value::Internal(val), _) | (_, Value::Internal(val)) => val + 1,
2121
(_, _) => 2,
2222
}))
2323
}
2424
pub fn aggregate_min(self, other: Value) -> Result<Value> {
2525
Ok(
2626
if matches!(self.partial_cmp(&other), Some(Ordering::Less))
27-
|| matches!(other, Value::Null)
27+
|| matches!(other, Value::Null(_))
2828
{
2929
self
3030
} else {
@@ -35,7 +35,7 @@ impl Value {
3535
pub fn aggregate_max(self, other: Value) -> Result<Value> {
3636
Ok(
3737
if matches!(self.partial_cmp(&other), Some(Ordering::Greater))
38-
|| matches!(other, Value::Null)
38+
|| matches!(other, Value::Null(_))
3939
{
4040
self
4141
} else {

src/data/value/methods/binary.rs

Lines changed: 6 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,15 @@
1-
#![allow(clippy::should_implement_trait)] // TODO
2-
31
use {
4-
super::ValueCore,
5-
crate::{Convert, ConvertFrom, Result, Value, ValueError},
2+
crate::{Result, Value, Cast},
3+
std::ops::*,
64
};
75

8-
// These were using references, they now consume their variables. See ::recipe.
9-
macro_rules! natural_binary_op {
10-
($name: ident, $trait: ident, $op: tt) => {
11-
pub fn $name<Core>(self, other: Self) -> Result<Self>
12-
where
13-
Core: ValueCore + $trait<Output = Core>,
14-
{
15-
let (left, right) = (Core::convert_from(self)?, Core::convert_from(other)?);
16-
let result = left $op right;
17-
Ok(result.into())
18-
}
19-
};
20-
}
21-
macro_rules! natural_binary_ops {
22-
($(($name: ident, $trait: ident, $op: tt, $generic_name: ident)),+) => {
23-
use std::ops::{$($trait),+};
24-
impl Value {
25-
$(
26-
natural_binary_op!($name, $trait, $op);
27-
generic!($name, $generic_name);
28-
)+
29-
}
30-
}
31-
}
32-
33-
macro_rules! boolean_binary_op {
34-
($name: ident, $op: tt) => {
35-
pub fn $name(self, other: Self) -> Result<Self>
36-
{
37-
let (left, right): (bool, bool) = (self.convert()?, other.convert()?);
38-
let result = left $op right;
39-
Ok(result.into())
40-
}
41-
};
42-
}
43-
macro_rules! boolean_binary_ops {
44-
($(($name: ident, $op: tt)),+) => {
45-
impl Value {
46-
$(boolean_binary_op!($name, $op);)+
47-
}
48-
}
49-
}
50-
51-
macro_rules! comparative_binary_op {
52-
($name: ident, $op: tt) => {
53-
pub fn $name(self, other: Self) -> Result<Self> {
54-
Ok(Value::Bool(self $op other))
55-
}
56-
};
6+
pub trait BinaryOperations:
7+
Sized + Add + Sub + Mul + Div + Rem + Eq + Ord + BitAnd + BitOr + BitXor
8+
{
579
}
58-
macro_rules! comparative_binary_ops {
59-
($(($name: ident, $op: tt)),+) => {
60-
impl Value {
61-
$(comparative_binary_op!($name, $op);)+
62-
}
63-
}
64-
}
65-
66-
macro_rules! generic {
67-
($name: ident, $generic_name: ident) => {
68-
pub fn $generic_name(self, other: Self) -> Result<Self> {
69-
if matches!(self, Value::Null) || matches!(other, Value::Null) {
70-
Ok(Value::Null)
71-
} else if !i64::convert_from(self.clone()).is_err()
72-
&& !i64::convert_from(other.clone()).is_err()
73-
{
74-
self.$name::<i64>(other)
75-
} else if !f64::convert_from(self.clone()).is_err()
76-
&& !f64::convert_from(other.clone()).is_err()
77-
{
78-
self.$name::<f64>(other)
79-
} else {
80-
Err(ValueError::OnlySupportsNumeric(
81-
if f64::convert_from(self.clone()).is_err() {
82-
self
83-
} else {
84-
other
85-
},
86-
stringify!($name),
87-
)
88-
.into())
89-
}
90-
}
91-
};
92-
}
93-
94-
natural_binary_ops!(
95-
(add, Add, +, generic_add),
96-
(subtract, Sub, -, generic_subtract),
97-
(multiply, Mul, *, generic_multiply),
98-
(divide, Div, /, generic_divide),
99-
(modulus, Rem, %, generic_modulus)
100-
);
101-
102-
boolean_binary_ops!(
103-
(and, &),
104-
(or, |),
105-
(xor, ^)
106-
);
107-
108-
comparative_binary_ops!(
109-
(eq, ==),
110-
(not_eq, !=),
111-
(gt, >),
112-
(gt_eq, >=),
113-
(lt, <),
114-
(lt_eq, <=)
115-
);
11610

11711
impl Value {
11812
pub fn string_concat(self, other: Self) -> Result<Self> {
119-
Ok(format!(
120-
"{}{}",
121-
String::convert_from(self)?,
122-
String::convert_from(other)?
123-
)
124-
.into())
13+
Ok(format!("{}{}", self.cast()?, other.cast()?).into())
12514
}
12615
}

src/data/value/methods/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,6 @@ impl Value {
146146
})
147147
}
148148
pub fn function_try_convert(arguments: Vec<Self>) -> Result<Self> {
149-
Ok(Value::function_convert(arguments).unwrap_or(Value::Null))
149+
Ok(Value::function_convert(arguments).unwrap_or(Value::NULL))
150150
}
151151
}

src/data/value/methods/mod.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,5 @@ mod function;
44
mod timestamp;
55
mod unary;
66
mod utility;
7-
use {
8-
crate::{ConvertFrom, Value},
9-
std::convert::Into,
10-
};
117

12-
pub trait ValueCore: Into<Value> + ConvertFrom<Value> {}
13-
impl ValueCore for bool {}
14-
impl ValueCore for i64 {}
15-
impl ValueCore for f64 {}
16-
impl ValueCore for String {}
8+
pub use {binary::BinaryOperations, unary::UnaryOperations};

src/data/value/methods/timestamp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use {
1313
macro_rules! protect_null {
1414
($protect: expr) => {
1515
match $protect {
16-
Value::Null => return Ok(Value::Null),
16+
Value::Null(_) => return Ok(Value::NULL),
1717
other => other,
1818
}
1919
};

src/data/value/methods/unary.rs

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,12 @@
11
use {
2-
super::ValueCore,
3-
crate::{Convert, ConvertFrom, Result, Value, ValueError},
4-
std::ops::Neg,
2+
crate::{Result, Value},
3+
std::ops::*,
54
};
65

7-
macro_rules! generic {
8-
($name: ident, $generic_name: ident) => {
9-
pub fn $generic_name(self) -> Result<Self> {
10-
if !i64::convert_from(self.clone()).is_err() {
11-
// TODO: Improve
12-
self.$name::<i64>()
13-
} else if !f64::convert_from(self.clone()).is_err() {
14-
self.$name::<f64>()
15-
} else {
16-
Err(ValueError::OnlySupportsNumeric(self, stringify!($name)).into())
17-
}
18-
}
19-
};
20-
}
6+
pub trait UnaryOperations: Not + Neg {}
217

22-
#[allow(clippy::should_implement_trait)] // TODO
238
impl Value {
24-
pub fn unary_plus<Core>(self) -> Result<Self>
25-
where
26-
Core: ValueCore + Clone,
27-
{
28-
let core = Core::convert_from(self)?;
29-
let result = core;
30-
Ok(result.into())
31-
}
32-
pub fn unary_minus<Core>(self) -> Result<Self>
33-
where
34-
Core: ValueCore + Neg<Output = Core>,
35-
{
36-
let core = Core::convert_from(self)?;
37-
let result = -core;
38-
Ok(result.into())
39-
}
40-
41-
generic!(unary_plus, generic_unary_plus);
42-
generic!(unary_minus, generic_unary_minus);
43-
44-
pub fn not(self) -> Result<Self> {
45-
let boolean: bool = self.convert()?;
46-
let result = !boolean;
47-
Ok(result.into())
48-
}
499
pub fn is_null(self) -> Result<Self> {
50-
Ok(Value::Bool(matches!(self, Value::Null)))
10+
Ok(Value::Bool(matches!(self, Value::Null(_))))
5111
}
5212
}

src/data/value/methods/utility.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ use {
55

66
macro_rules! protect_null {
77
($protect: expr) => {
8-
if matches!($protect, Value::Null) {
8+
if matches!($protect, Value::Null(_)) {
99
return Ok($protect);
1010
}
1111
};
1212
}
1313

1414
impl Value {
1515
pub fn if_null(self, alternative: Self) -> Self {
16-
if !matches!(self, Value::Null) {
16+
if !matches!(self, Value::Null(_)) {
1717
self
1818
} else {
1919
alternative
2020
}
2121
}
2222
pub fn null_if(self, evaluate: Self) -> Result<Self> {
23-
Ok(if self == evaluate { Value::Null } else { self })
23+
Ok(if self == evaluate { Value::NULL } else { self })
2424
}
2525
pub fn iif(self, case_true: Self, case_false: Self) -> Result<Self> {
2626
Ok(if self.convert()? {
@@ -93,7 +93,7 @@ impl Value {
9393
}
9494

9595
pub fn round(self, places: Value) -> Result<Value> {
96-
if matches!(self, Value::Null) {
96+
if matches!(self, Value::Null(_)) {
9797
return Ok(self);
9898
}
9999
let value: f64 = self.convert()?;

0 commit comments

Comments
 (0)