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
use crate::{
ffi::{
boxed::{AscBox, AscRef},
buf::AscTypedArray,
str::AscString,
value::{AscArray, AscEntity, AscEntityValue, AscEntityValueData, AscMapEntry},
},
num::{BigDecimal, BigInt},
};
use indexmap::IndexMap;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub enum Value {
String(String),
Int(i32),
BigDecimal(BigDecimal),
Bool(bool),
Array(Vec<Value>),
#[default]
Null,
Bytes(Vec<u8>),
BigInt(BigInt),
}
pub type Entity = IndexMap<String, Value>;
impl Value {
pub(crate) fn from_raw(raw: &'static AscRef<AscEntityValue>) -> Self {
match raw.data() {
AscEntityValueData::String(value) => Self::String(value.to_string_lossy()),
AscEntityValueData::Int(value) => Self::Int(value),
AscEntityValueData::BigDecimal(value) => Self::BigDecimal(BigDecimal::from_raw(value)),
AscEntityValueData::Bool(value) => Self::Bool(value),
AscEntityValueData::Array(value) => Self::Array(
value
.as_slice()
.iter()
.map(|value| Value::from_raw(value.as_asc_ref()))
.collect(),
),
AscEntityValueData::Null(()) => Self::Null,
AscEntityValueData::Bytes(value) => Self::Bytes(value.as_slice().to_owned()),
AscEntityValueData::BigInt(value) => Self::BigInt(BigInt::from_raw(value)),
}
}
pub(crate) fn to_raw(&self) -> AscBox<AscEntityValue> {
match self {
Self::String(value) => AscEntityValue::string(AscString::new(value)),
Self::Int(value) => AscEntityValue::int(*value),
Self::BigDecimal(value) => AscEntityValue::bigdecimal(value.as_raw().to_owned()),
Self::Bool(value) => AscEntityValue::bool(*value),
Self::Array(value) => {
AscEntityValue::array(AscArray::new(value.iter().map(Value::to_raw).collect()))
}
Self::Null => AscEntityValue::null(()),
Self::Bytes(value) => AscEntityValue::bytes(AscTypedArray::from_bytes(value)),
Self::BigInt(value) => AscEntityValue::bigint(value.as_raw().to_owned()),
}
}
pub fn as_string(&self) -> Option<&str> {
match self {
Self::String(value) => Some(value),
_ => None,
}
}
pub fn as_int(&self) -> Option<i32> {
match self {
Self::Int(value) => Some(*value),
_ => None,
}
}
pub fn as_big_decimal(&self) -> Option<&BigDecimal> {
match self {
Self::BigDecimal(value) => Some(value),
_ => None,
}
}
pub fn as_bool(&self) -> Option<bool> {
match self {
Self::Bool(value) => Some(*value),
_ => None,
}
}
pub fn as_array(&self) -> Option<&[Value]> {
match self {
Self::Array(value) => Some(value),
_ => None,
}
}
pub fn as_null(&self) -> Option<()> {
match self {
Self::Null => Some(()),
_ => None,
}
}
pub fn as_bytes(&self) -> Option<&Vec<u8>> {
match self {
Self::Bytes(value) => Some(value),
_ => None,
}
}
pub fn as_big_int(&self) -> Option<&BigInt> {
match self {
Self::BigInt(value) => Some(value),
_ => None,
}
}
}
pub(crate) trait EntityExt: Sized {
fn from_raw(raw: &'static AscRef<AscEntity>) -> Self;
fn to_raw(&self) -> AscBox<AscEntity>;
}
impl EntityExt for Entity {
fn from_raw(raw: &'static AscRef<AscEntity>) -> Self {
raw.entries()
.iter()
.map(|entry| {
let entry = entry.as_asc_ref();
(
entry.key().to_string_lossy(),
Value::from_raw(entry.value().as_asc_ref()),
)
})
.collect()
}
fn to_raw(&self) -> AscBox<AscEntity> {
AscEntity::new(
self.iter()
.map(|(key, value)| AscMapEntry::new(AscString::new(key), value.to_raw()))
.collect(),
)
}
}