-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvalue.rs
More file actions
182 lines (160 loc) · 4.89 KB
/
value.rs
File metadata and controls
182 lines (160 loc) · 4.89 KB
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
use crate::define::Result;
use crate::error::Error;
use rust_decimal::prelude::*;
use std::fmt;
#[derive(Clone, PartialEq, Debug)]
pub enum Value {
String(String),
Number(Decimal),
Bool(bool),
List(Vec<Value>),
Map(Vec<(Value, Value)>),
None,
}
#[cfg(not(tarpaulin_include))]
impl fmt::Display for Value {
// ⚡ Bolt Optimization: Avoid intermediate String allocations when formatting collections.
// Instead of using `format!` and `push_str` which cause unnecessary heap allocations and copying,
// we write directly to the `fmt::Formatter` via `write!`. This significantly improves formatting performance.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::String(val) => write!(f, "value string: {}", val),
Self::Number(val) => write!(f, "value number: {}", val),
Self::Bool(val) => write!(f, "value bool: {}", val),
Self::List(values) => {
write!(f, "value list: [")?;
for value in values {
write!(f, "{},", value)?;
}
write!(f, "]")
}
Self::Map(m) => {
write!(f, "value map: {{")?;
for (k, v) in m {
write!(f, "key: {},value: {}; ", k, v)?;
}
write!(f, "}}")
}
Self::None => write!(f, "None"),
}
}
}
impl From<&str> for Value {
fn from(value: &str) -> Self {
Value::String(value.to_string())
}
}
impl From<String> for Value {
fn from(value: String) -> Self {
Value::String(value)
}
}
impl From<bool> for Value {
fn from(value: bool) -> Self {
Value::Bool(value)
}
}
impl From<Vec<Value>> for Value {
fn from(value: Vec<Value>) -> Self {
Value::List(value)
}
}
impl From<Decimal> for Value {
fn from(value: Decimal) -> Self {
Value::Number(value)
}
}
impl Value {
pub fn decimal(self) -> Result<rust_decimal::Decimal> {
match self {
Self::Number(val) => Ok(val),
_ => Err(Error::ShouldBeNumber()),
}
}
pub fn string(self) -> Result<String> {
match self {
Self::String(val) => Ok(val),
_ => Err(Error::ShouldBeString()),
}
}
pub fn bool(self) -> Result<bool> {
match self {
Self::Bool(val) => Ok(val),
_ => Err(Error::ShouldBeBool()),
}
}
pub fn integer(self) -> Result<i64> {
match self {
Self::Number(val) => {
if val.scale() == 0 {
val.to_i64().ok_or(Error::InvalidInteger)
} else {
Err(Error::InvalidInteger)
}
}
_ => Err(Error::InvalidInteger),
}
}
pub fn float(self) -> Result<f64> {
match self {
Self::Number(val) => val.to_f64().ok_or(Error::InvalidFloat),
_ => Err(Error::InvalidFloat),
}
}
pub fn list(self) -> Result<Vec<Value>> {
match self {
Self::List(list) => Ok(list),
_ => Err(Error::ShouldBeList()),
}
}
}
macro_rules! impl_value_from_for_number {
($([$number_type:tt, $method_name: ident]),+) => {
$(
impl From<$number_type> for Value {
fn from(value: $number_type) -> Self {
Value::Number(Decimal::$method_name(value).unwrap_or_default())
}
}
)+
};
}
impl_value_from_for_number!(
[i128, from_i128],
[i32, from_i32],
[i64, from_i64],
[i16, from_i16],
[i8, from_i8],
[u128, from_u128],
[u64, from_u64],
[u32, from_u32],
[u16, from_u16],
[u8, from_u8],
[f64, from_f64],
[f32, from_f32]
);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_value_integer() {
assert_eq!(Value::from(10).integer().unwrap(), 10);
assert_eq!(Value::from(-5).integer().unwrap(), -5);
// Test that integer() fails when scale is not 0
let dec = Decimal::from_str("10.5").unwrap();
assert!(Value::Number(dec).integer().is_err());
// Even if the value represents an integer, but has a non-zero scale, it should fail to parse
// according to strict behavior checking `val.scale() == 0`
let dec_with_scale = Decimal::from_str("10.0").unwrap();
assert!(Value::Number(dec_with_scale).integer().is_err());
}
#[test]
fn test_value_float() {
assert_eq!(Value::from(10).float().unwrap(), 10.0);
assert_eq!(Value::from(-5).float().unwrap(), -5.0);
let dec = Decimal::from_str("10.5").unwrap();
assert_eq!(Value::Number(dec).float().unwrap(), 10.5);
let dec_with_scale = Decimal::from_str("10.0").unwrap();
assert_eq!(Value::Number(dec_with_scale).float().unwrap(), 10.0);
}
}