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
use std::sync::Arc;
#[derive(Debug, PartialEq)]
pub enum KVal {
Null,
Bit(bool),
Bool(bool),
Byte(i8),
Str16(String),
Int(i64),
Float(f32),
Str256(String),
Double(f64),
Decimal(Decimal),
BigStr(String),
Binary(Vec<u8>),
BinaryArc(Arc<Vec<u8>>),
Binary8(Vec<u64>),
Binary4(Vec<u32>),
Binary2(Vec<u16>),
Undefined(u8),
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Decimal{
pub int : i128,
pub dot : u8,
}
impl Decimal{
pub fn to_f64(&self) -> f64 {
crate::enc_dec::decimal_lib::to_f64(self.int, self.dot)
}
pub fn to_string(&self) -> String{
crate::enc_dec::decimal_lib::to_string(self.int, self.dot)
}
pub fn new(int : i128, dot : u8) -> Decimal{
Decimal{ int, dot }
}
}
impl KVal {
pub fn as_f64(&self) -> Option<f64>{
use KVal::*;
match self{
Bit(b) => Some(*b as isize as f64),
Byte(b) => Some(*b as f64),
Int(i) => Some(*i as f64),
Float(f) => Some(*f as f64),
Double(f) => Some(*f),
_ => None,
}
}
pub fn as_f32(&self) -> Option<f32>{
use KVal::*;
match self{
Bit(b) => Some(*b as isize as f32),
Byte(b) => Some(*b as f32),
Int(i) => Some(*i as f32),
Float(f) => Some(*f),
Double(f) => Some(*f as f32),
_ => None,
}
}
pub fn as_i64(&self) -> Option<i64>{
use KVal::*;
match self {
Bit(b) => Some(*b as i64),
Byte(b) => Some(*b as i64),
Int(i) => Some(*i as i64),
_ => None,
}
}
pub fn as_string(&self) -> Option<String>{
use KVal::*;
match self{
Bit(_b) => Some(String::new()),
Str16(s) => Some(s.to_string()),
Str256(s) => Some(s.to_string()),
BigStr(s) => Some(s.to_string()),
_ => None,
}
}
pub fn as_bool(&self) -> Option<bool>{
match self{
KVal::Bool(b) => Some(*b),
KVal::Bit(b) => Some(*b),
_ => None,
}
}
pub fn as_null(&self) -> Option<()>{
match self{
KVal::Null => Some(()),
_ => None,
}
}
pub fn as_undefined(&self) -> Option<u8>{
match self{
KVal::Undefined(u) => Some(*u),
_ => None,
}
}
pub fn as_decimal(&self) -> Option<Decimal>{
match self{
KVal::Decimal(d) => Some(*d),
_ => None,
}
}
pub fn to_string(&self) -> String{
crate::string_compaction::to_string(self)
}
}