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
use crate::errors::YdbError;
use crate::types::{Bytes, Value, ValueOptional};
use crate::{ValueList, ValueStruct};
use itertools::Itertools;
use std::any::type_name;
use std::collections::HashMap;
use std::time::Duration;
macro_rules! simple_convert {
($native_type:ty, $ydb_value_kind_first:path $(,$ydb_value_kind:path)* $(,)?) => {
impl From<$native_type> for Value {
fn from(value: $native_type)->Self {
$ydb_value_kind_first(value)
}
}
impl TryFrom<Value> for $native_type {
type Error = YdbError;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
$ydb_value_kind_first(val) => Ok(val.into()),
$($ydb_value_kind(val) => Ok(val.into()),)*
value => Err(YdbError::Convert(format!(
"failed to convert from {} to {}",
value.kind_static(),
type_name::<Self>(),
))),
}
}
}
impl TryFrom<Value> for Option<$native_type> {
type Error = YdbError;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::Optional(opt_val) => {
if let Err(err) = <$native_type as TryFrom<Value>>::try_from(opt_val.t) {
return Err(err);
};
match opt_val.value {
Some(val) => {
let res_val: $native_type = val.try_into()?;
Ok(Some(res_val))
}
None => Ok(None),
}
}
value => Ok(Some(value.try_into()?)),
}
}
}
};
}
simple_convert!(i8, Value::Int8);
simple_convert!(u8, Value::Uint8);
simple_convert!(i16, Value::Int16, Value::Int8, Value::Uint8);
simple_convert!(u16, Value::Uint16, Value::Uint8);
simple_convert!(
i32,
Value::Int32,
Value::Int16,
Value::Uint16,
Value::Int8,
Value::Uint8,
);
simple_convert!(u32, Value::Uint32, Value::Uint16, Value::Uint8);
simple_convert!(
i64,
Value::Int64,
Value::Int32,
Value::Uint32,
Value::Int16,
Value::Uint16,
Value::Int8,
Value::Uint8,
);
simple_convert!(
u64,
Value::Uint64,
Value::Uint32,
Value::Uint16,
Value::Uint8,
);
simple_convert!(
String,
Value::Utf8,
Value::Json,
Value::JsonDocument,
Value::Yson
);
simple_convert!(
Bytes,
Value::String,
Value::Utf8,
Value::Json,
Value::JsonDocument,
Value::Yson
);
simple_convert!(f32, Value::Float);
simple_convert!(f64, Value::Double, Value::Float);
simple_convert!(Duration, Value::Timestamp, Value::Date, Value::DateTime);
impl From<&str> for Value {
fn from(v: &str) -> Self {
return v.to_string().into();
}
}
impl<T: Into<Value> + Default> From<Option<T>> for Value {
fn from(from_value: Option<T>) -> Self {
let t = T::default().into();
let value = match from_value {
Some(val) => Some(val.into()),
None => None,
};
return Value::Optional(Box::new(ValueOptional { t, value }));
}
}
impl<T: Into<Value> + Default> FromIterator<T> for Value {
fn from_iter<T2: IntoIterator<Item = T>>(iter: T2) -> Self {
let t: Value = T::default().into();
let values: Vec<Value> = iter.into_iter().map(|item| item.into()).collect();
return Value::List(Box::new(ValueList { t, values }));
}
}
impl<T> TryFrom<Value> for Vec<T>
where
T: TryFrom<Value, Error = YdbError>,
{
type Error = YdbError;
fn try_from(value: Value) -> Result<Self, Self::Error> {
let value = match value {
Value::List(inner) => inner,
value => {
return Err(YdbError::from_str(format!(
"can't convert from {} to Vec",
value.kind_static()
)));
}
};
let list_item_type = value.t.kind_static();
if TryInto::<T>::try_into(value.t).is_err() {
let vec_item_type = type_name::<i32>();
return Err(YdbError::from_str(format!(
"can't convert list item type '{}' to vec item type '{}'",
list_item_type, vec_item_type
)));
};
let res: Vec<T> = value
.values
.into_iter()
.map(|item| item.try_into())
.try_collect()?;
return Ok(res);
}
}
impl From<HashMap<String, Value>> for Value {
fn from(from_val: HashMap<String, Value>) -> Self {
let mut value_struct = ValueStruct::with_capacity(from_val.len());
from_val
.into_iter()
.for_each(|(key, val)| value_struct.insert(key, val));
return Value::Struct(value_struct);
}
}
impl TryFrom<Value> for HashMap<String, Value> {
type Error = YdbError;
fn try_from(from_value: Value) -> Result<Self, Self::Error> {
let kind_name = from_value.kind_static();
let value_struct = match from_value {
Value::Struct(value_struct) => value_struct,
_ => {
return Err(YdbError::from_str(format!(
"failed convert {} to HashMap",
kind_name
)))
}
};
return Ok(value_struct.into());
}
}