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
use internal::stringpool::{StringPool, StringRef};
use std::fmt;
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Value {
Null,
Int(i32),
Str(String),
}
impl Value {
pub fn is_null(&self) -> bool {
match *self {
Value::Null => true,
_ => false,
}
}
pub fn is_int(&self) -> bool {
match *self {
Value::Int(_) => true,
_ => false,
}
}
pub fn as_int(&self) -> Option<i32> {
match *self {
Value::Null => None,
Value::Int(number) => Some(number),
Value::Str(_) => None,
}
}
pub fn is_str(&self) -> bool {
match *self {
Value::Str(_) => true,
_ => false,
}
}
pub fn as_str(&self) -> Option<&str> {
match *self {
Value::Null => None,
Value::Int(_) => None,
Value::Str(ref string) => Some(string.as_str()),
}
}
pub(crate) fn from_bool(boolean: bool) -> Value {
if boolean {
Value::Int(1)
} else {
Value::Int(0)
}
}
pub(crate) fn to_bool(&self) -> bool {
match *self {
Value::Null => false,
Value::Int(number) => number != 0,
Value::Str(ref string) => !string.is_empty(),
}
}
}
impl fmt::Display for Value {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
Value::Null => "NULL".fmt(formatter),
Value::Int(number) => number.fmt(formatter),
Value::Str(ref string) => format!("{:?}", string).fmt(formatter),
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ValueRef {
Null,
Int(i32),
Str(StringRef),
}
impl ValueRef {
pub fn create(value: Value, string_pool: &mut StringPool) -> ValueRef {
match value {
Value::Null => ValueRef::Null,
Value::Int(number) => ValueRef::Int(number),
Value::Str(string) => ValueRef::Str(string_pool.incref(string)),
}
}
pub fn remove(self, string_pool: &mut StringPool) {
match self {
ValueRef::Null | ValueRef::Int(_) => {}
ValueRef::Str(string_ref) => string_pool.decref(string_ref),
}
}
pub fn to_value(&self, string_pool: &StringPool) -> Value {
match *self {
ValueRef::Null => Value::Null,
ValueRef::Int(number) => Value::Int(number),
ValueRef::Str(string_ref) => {
Value::Str(string_pool.get(string_ref).to_string())
}
}
}
}
#[cfg(test)]
mod tests {
use super::{Value, ValueRef};
use internal::codepage::CodePage;
use internal::stringpool::StringPool;
#[test]
fn format_value() {
assert_eq!(format!("{}", Value::Null), "NULL".to_string());
assert_eq!(format!("{}", Value::Int(42)), "42".to_string());
assert_eq!(format!("{}", Value::Int(-137)), "-137".to_string());
assert_eq!(format!("{}", Value::Str("Hello, world!".to_string())),
"\"Hello, world!\"".to_string());
assert_eq!(format!("{:>6}", Value::Null), " NULL".to_string());
assert_eq!(format!("[{:<4}]", Value::Int(42)), "[42 ]".to_string());
assert_eq!(format!("foo{:~>8}", Value::Str("bar".to_string())),
"foo~~~\"bar\"".to_string());
}
#[test]
fn create_value_ref() {
let mut string_pool = StringPool::new(CodePage::default());
let value = Value::Null;
let value_ref = ValueRef::create(value.clone(), &mut string_pool);
assert_eq!(value_ref.to_value(&string_pool), value);
let value = Value::Int(1234567);
let value_ref = ValueRef::create(value.clone(), &mut string_pool);
assert_eq!(value_ref.to_value(&string_pool), value);
let value = Value::Str("Hello, world!".to_string());
let value_ref = ValueRef::create(value.clone(), &mut string_pool);
assert_eq!(value_ref.to_value(&string_pool), value);
}
}