-
Notifications
You must be signed in to change notification settings - Fork 557
/
value.rs
207 lines (178 loc) · 5.5 KB
/
value.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use crate::encoding;
use crate::errdata;
use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};
/// A primitive data type.
#[derive(Clone, Debug, Hash, PartialEq, Serialize, Deserialize)]
pub enum DataType {
/// A boolean: true or false.
Boolean,
/// A 64-bit signed integer.
Integer,
/// A 64-bit floating point number.
Float,
/// A UTF-8 encoded string.
String,
}
impl std::fmt::Display for DataType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(match self {
Self::Boolean => "BOOLEAN",
Self::Integer => "INTEGER",
Self::Float => "FLOAT",
Self::String => "STRING",
})
}
}
/// A primitive value.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Value {
/// An unknown value of unknown type.
Null,
/// A boolean.
Boolean(bool),
/// A 64-bit signed integer.
Integer(i64),
/// A 64-bit floating point number.
Float(f64),
/// A UTF-8 encoded string.
String(String),
}
impl encoding::Value for Value {}
// TODO: revisit and document the f64 handling here.
impl std::cmp::Eq for Value {}
impl std::hash::Hash for Value {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.datatype().hash(state);
match self {
Value::Null => self.hash(state),
Value::Boolean(v) => v.hash(state),
Value::Integer(v) => v.hash(state),
Value::Float(v) => v.to_be_bytes().hash(state),
Value::String(v) => v.hash(state),
}
}
}
impl PartialOrd for Value {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match (self, other) {
// For ordering purposes, NULL is ordered first.
//
// TODO: revisit this and make sure the NULL handling is sound and
// consistent with e.g. hash and eq handling.
(Self::Null, Self::Null) => Some(std::cmp::Ordering::Equal),
(Self::Null, _) => Some(std::cmp::Ordering::Less),
(_, Self::Null) => Some(std::cmp::Ordering::Greater),
(Self::Boolean(a), Self::Boolean(b)) => a.partial_cmp(b),
(Self::Float(a), Self::Float(b)) => a.partial_cmp(b),
(Self::Float(a), Self::Integer(b)) => a.partial_cmp(&(*b as f64)),
(Self::Integer(a), Self::Float(b)) => (*a as f64).partial_cmp(b),
(Self::Integer(a), Self::Integer(b)) => a.partial_cmp(b),
(Self::String(a), Self::String(b)) => a.partial_cmp(b),
(_, _) => None,
}
}
}
impl Value {
/// Returns the value's datatype, or None for null values.
pub fn datatype(&self) -> Option<DataType> {
match self {
Self::Null => None,
Self::Boolean(_) => Some(DataType::Boolean),
Self::Integer(_) => Some(DataType::Integer),
Self::Float(_) => Some(DataType::Float),
Self::String(_) => Some(DataType::String),
}
}
}
impl std::fmt::Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::Null => f.write_str("NULL"),
Self::Boolean(true) => f.write_str("TRUE"),
Self::Boolean(false) => f.write_str("FALSE"),
Self::Integer(integer) => integer.fmt(f),
Self::Float(float) => float.fmt(f),
Self::String(string) => f.write_str(string),
}
}
}
impl From<bool> for Value {
fn from(v: bool) -> Self {
Value::Boolean(v)
}
}
impl From<f64> for Value {
fn from(v: f64) -> Self {
Value::Float(v)
}
}
impl From<i64> for Value {
fn from(v: i64) -> Self {
Value::Integer(v)
}
}
impl From<String> for Value {
fn from(v: String) -> Self {
Value::String(v)
}
}
impl From<&str> for Value {
fn from(v: &str) -> Self {
Value::String(v.to_owned())
}
}
impl TryFrom<Value> for bool {
type Error = Error;
fn try_from(value: Value) -> Result<Self> {
let Value::Boolean(b) = value else { return errdata!("not boolean: {value}") };
Ok(b)
}
}
impl TryFrom<Value> for f64 {
type Error = Error;
fn try_from(value: Value) -> Result<Self> {
let Value::Float(f) = value else { return errdata!("not float: {value}") };
Ok(f)
}
}
impl TryFrom<Value> for i64 {
type Error = Error;
fn try_from(value: Value) -> Result<Self> {
let Value::Integer(i) = value else { return errdata!("not integer: {value}") };
Ok(i)
}
}
impl TryFrom<Value> for String {
type Error = Error;
fn try_from(value: Value) -> Result<Self> {
let Value::String(s) = value else { return errdata!("not string: {value}") };
Ok(s)
}
}
// TODO: reconsider use of Cow, it's unnecessary.
impl<'a> From<Value> for std::borrow::Cow<'a, Value> {
fn from(v: Value) -> Self {
std::borrow::Cow::Owned(v)
}
}
impl<'a> From<&'a Value> for std::borrow::Cow<'a, Value> {
fn from(v: &'a Value) -> Self {
std::borrow::Cow::Borrowed(v)
}
}
/// A row of values.
pub type Row = Vec<Value>;
/// A row iterator.
///
/// TODO: try to avoid boxing here.
pub type Rows = Box<dyn Iterator<Item = Result<Row>>>;
/// A column (in a result set, see schema::Column for table columns).
///
/// TODO: revisit column handling in result sets.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Column {
pub name: Option<String>,
}
/// A set of columns.
pub type Columns = Vec<Column>;