-
Notifications
You must be signed in to change notification settings - Fork 64
/
response.rs
976 lines (878 loc) · 30 KB
/
response.rs
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
use crate::{
error::{Error, ErrorKind},
types::{Key, Value, QUEUED},
};
use bytes::Bytes;
use bytes_utils::Str;
use std::{
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
hash::{BuildHasher, Hash},
};
#[cfg(feature = "i-cluster")]
use crate::types::cluster::ClusterInfo;
#[cfg(feature = "i-geo")]
use crate::types::geo::GeoPosition;
#[cfg(feature = "i-slowlog")]
use crate::types::SlowlogEntry;
#[cfg(feature = "i-memory")]
use crate::types::{DatabaseMemoryStats, MemoryStats};
#[allow(unused_imports)]
use std::any::type_name;
macro_rules! debug_type(
($($arg:tt)*) => {
#[cfg(feature="network-logs")]
log::trace!($($arg)*);
}
);
macro_rules! check_single_bulk_reply(
($v:expr) => {
if $v.is_single_element_vec() {
return Self::from_value($v.pop_or_take());
}
};
($t:ty, $v:expr) => {
if $v.is_single_element_vec() {
return $t::from_value($v.pop_or_take());
}
}
);
macro_rules! to_signed_number(
($t:ty, $v:expr) => {
match $v {
Value::Double(f) => Ok(f as $t),
Value::Integer(i) => Ok(i as $t),
Value::String(s) => s.parse::<$t>().map_err(|e| e.into()),
Value::Array(mut a) => if a.len() == 1 {
match a.pop().unwrap() {
Value::Integer(i) => Ok(i as $t),
Value::String(s) => s.parse::<$t>().map_err(|e| e.into()),
#[cfg(feature = "default-nil-types")]
Value::Null => Ok(0),
#[cfg(not(feature = "default-nil-types"))]
Value::Null => Err(Error::new(ErrorKind::NotFound, "Cannot convert nil to number.")),
_ => Err(Error::new_parse("Cannot convert to number."))
}
}else{
Err(Error::new_parse("Cannot convert array to number."))
}
#[cfg(feature = "default-nil-types")]
Value::Null => Ok(0),
#[cfg(not(feature = "default-nil-types"))]
Value::Null => Err(Error::new(ErrorKind::NotFound, "Cannot convert nil to number.")),
_ => Err(Error::new_parse("Cannot convert to number.")),
}
}
);
macro_rules! to_unsigned_number(
($t:ty, $v:expr) => {
match $v {
Value::Double(f) => if f.is_sign_negative() {
Err(Error::new_parse("Cannot convert from negative number."))
}else{
Ok(f as $t)
},
Value::Integer(i) => if i < 0 {
Err(Error::new_parse("Cannot convert from negative number."))
}else{
Ok(i as $t)
},
Value::String(s) => s.parse::<$t>().map_err(|e| e.into()),
Value::Array(mut a) => if a.len() == 1 {
match a.pop().unwrap() {
Value::Integer(i) => if i < 0 {
Err(Error::new_parse("Cannot convert from negative number."))
}else{
Ok(i as $t)
},
#[cfg(feature = "default-nil-types")]
Value::Null => Ok(0),
#[cfg(not(feature = "default-nil-types"))]
Value::Null => Err(Error::new(ErrorKind::NotFound, "Cannot convert nil to number.")),
Value::String(s) => s.parse::<$t>().map_err(|e| e.into()),
_ => Err(Error::new_parse("Cannot convert to number."))
}
}else{
Err(Error::new_parse("Cannot convert array to number."))
},
#[cfg(feature = "default-nil-types")]
Value::Null => Ok(0),
#[cfg(not(feature = "default-nil-types"))]
Value::Null => Err(Error::new(ErrorKind::NotFound, "Cannot convert nil to number.")),
_ => Err(Error::new_parse("Cannot convert to number.")),
}
}
);
macro_rules! impl_signed_number (
($t:ty) => {
impl FromValue for $t {
fn from_value(value: Value) -> Result<$t, Error> {
check_single_bulk_reply!(value);
to_signed_number!($t, value)
}
}
}
);
macro_rules! impl_unsigned_number (
($t:ty) => {
impl FromValue for $t {
fn from_value(value: Value) -> Result<$t, Error> {
check_single_bulk_reply!(value);
to_unsigned_number!($t, value)
}
}
}
);
/// A trait used to [convert](Value::convert) various forms of [Value](Value) into different types.
///
/// ## Examples
///
/// ```rust
/// # use fred::types::Value;
/// # use std::collections::HashMap;
/// let foo: usize = Value::String("123".into()).convert()?;
/// let foo: i64 = Value::String("123".into()).convert()?;
/// let foo: String = Value::String("123".into()).convert()?;
/// let foo: Vec<u8> = Value::Bytes(vec![102, 111, 111].into()).convert()?;
/// let foo: Vec<u8> = Value::String("foo".into()).convert()?;
/// let foo: Vec<String> = Value::Array(vec!["a".into(), "b".into()]).convert()?;
/// let foo: HashMap<String, u16> =
/// Value::Array(vec!["a".into(), 1.into(), "b".into(), 2.into()]).convert()?;
/// let foo: (String, i64) = Value::Array(vec!["a".into(), 1.into()]).convert()?;
/// let foo: Vec<(String, i64)> =
/// Value::Array(vec!["a".into(), 1.into(), "b".into(), 2.into()]).convert()?;
/// // ...
/// ```
///
/// ## Bulk Values
///
/// This interface can also convert single-element vectors to scalar values in certain scenarios. This is often
/// useful with commands that conditionally return bulk values, or where the number of elements in the response
/// depends on the number of arguments (`MGET`, etc).
///
/// For example:
///
/// ```rust
/// # use fred::types::Value;
/// let _: String = Value::Array(vec![]).convert()?; // error
/// let _: String = Value::Array(vec!["a".into()]).convert()?; // "a"
/// let _: String = Value::Array(vec!["a".into(), "b".into()]).convert()?; // error
/// let _: Option<String> = Value::Array(vec![]).convert()?; // None
/// let _: Option<String> = Value::Array(vec!["a".into()]).convert()?; // Some("a")
/// let _: Option<String> = Value::Array(vec!["a".into(), "b".into()]).convert()?; // error
/// ```
///
/// ## The `default-nil-types` Feature Flag
///
/// By default a `nil` value cannot be converted directly into any of the scalar types (`u8`, `String`, `Bytes`,
/// etc). In practice this often requires callers to use an `Option` or `Vec` container with commands that can return
/// `nil`.
///
/// The `default-nil-types` feature flag can enable some further type conversion branches that treat `nil` values as
/// default values for the relevant type. For `Value::Null` these include:
///
/// * `impl FromValue` for `String` or `Str` returns an empty string.
/// * `impl FromValue` for `Bytes` or `Vec<T>` returns an empty array.
/// * `impl FromValue` for any integer or float type returns `0`
/// * `impl FromValue` for `bool` returns `false`
/// * `impl FromValue` for map or set types return an empty map or set.
pub trait FromValue: Sized {
fn from_value(value: Value) -> Result<Self, Error>;
#[doc(hidden)]
fn from_values(values: Vec<Value>) -> Result<Vec<Self>, Error> {
values.into_iter().map(|v| Self::from_value(v)).collect()
}
#[doc(hidden)]
// FIXME if/when specialization is stable
fn from_owned_bytes(_: Vec<u8>) -> Option<Vec<Self>> {
None
}
#[doc(hidden)]
fn is_tuple() -> bool {
false
}
}
impl FromValue for Value {
fn from_value(value: Value) -> Result<Self, Error> {
Ok(value)
}
}
impl FromValue for () {
fn from_value(_: Value) -> Result<Self, Error> {
Ok(())
}
}
impl_signed_number!(i8);
impl_signed_number!(i16);
impl_signed_number!(i32);
impl_signed_number!(i64);
impl_signed_number!(i128);
impl_signed_number!(isize);
impl FromValue for u8 {
fn from_value(value: Value) -> Result<Self, Error> {
check_single_bulk_reply!(value);
to_unsigned_number!(u8, value)
}
fn from_owned_bytes(d: Vec<u8>) -> Option<Vec<Self>> {
Some(d)
}
}
impl_unsigned_number!(u16);
impl_unsigned_number!(u32);
impl_unsigned_number!(u64);
impl_unsigned_number!(u128);
impl_unsigned_number!(usize);
impl FromValue for String {
fn from_value(value: Value) -> Result<Self, Error> {
debug_type!("FromValue(String): {:?}", value);
check_single_bulk_reply!(value);
value
.into_string()
.ok_or(Error::new_parse("Could not convert to string."))
}
}
impl FromValue for Str {
fn from_value(value: Value) -> Result<Self, Error> {
debug_type!("FromValue(Str): {:?}", value);
check_single_bulk_reply!(value);
value
.into_bytes_str()
.ok_or(Error::new_parse("Could not convert to string."))
}
}
impl FromValue for f64 {
fn from_value(value: Value) -> Result<Self, Error> {
debug_type!("FromValue(f64): {:?}", value);
check_single_bulk_reply!(value);
value.as_f64().ok_or(Error::new_parse("Could not convert to double."))
}
}
impl FromValue for f32 {
fn from_value(value: Value) -> Result<Self, Error> {
debug_type!("FromValue(f32): {:?}", value);
check_single_bulk_reply!(value);
value
.as_f64()
.map(|f| f as f32)
.ok_or(Error::new_parse("Could not convert to float."))
}
}
impl FromValue for bool {
fn from_value(value: Value) -> Result<Self, Error> {
debug_type!("FromValue(bool): {:?}", value);
check_single_bulk_reply!(value);
if let Some(val) = value.as_bool() {
Ok(val)
} else {
// it's not obvious how to convert the value to a bool in this block, so we go with a
// tried and true approach that i'm sure we'll never regret - JS semantics
Ok(match value {
Value::String(s) => !s.is_empty(),
Value::Bytes(b) => !b.is_empty(),
// everything else should be covered by `as_bool` above
_ => return Err(Error::new_parse("Could not convert to bool.")),
})
}
}
}
impl<T> FromValue for Option<T>
where
T: FromValue,
{
fn from_value(value: Value) -> Result<Option<T>, Error> {
debug_type!("FromValue(Option<{}>): {:?}", type_name::<T>(), value);
if let Some(0) = value.array_len() {
Ok(None)
} else if value.is_null() {
Ok(None)
} else {
Ok(Some(T::from_value(value)?))
}
}
}
impl FromValue for Bytes {
fn from_value(value: Value) -> Result<Self, Error> {
debug_type!("FromValue(Bytes): {:?}", value);
check_single_bulk_reply!(value);
value.into_bytes().ok_or(Error::new_parse("Cannot parse into bytes."))
}
}
impl<T> FromValue for Vec<T>
where
T: FromValue,
{
fn from_value(value: Value) -> Result<Vec<T>, Error> {
debug_type!("FromValue(Vec<{}>): {:?}", type_name::<T>(), value);
match value {
Value::Bytes(bytes) => T::from_owned_bytes(bytes.to_vec()).ok_or(Error::new_parse("Cannot convert from bytes")),
Value::String(string) => {
// hacky way to check if T is bytes without consuming `string`
if T::from_owned_bytes(Vec::new()).is_some() {
T::from_owned_bytes(string.into_inner().to_vec())
.ok_or(Error::new_parse("Could not convert string to bytes."))
} else {
Ok(vec![T::from_value(Value::String(string))?])
}
},
Value::Array(values) => {
if !values.is_empty() {
if let Value::Array(_) = &values[0] {
values.into_iter().map(|x| T::from_value(x)).collect()
} else {
T::from_values(values)
}
} else {
Ok(vec![])
}
},
Value::Map(map) => {
// not being able to use collect() here is unfortunate
let out = Vec::with_capacity(map.len() * 2);
map.inner().into_iter().try_fold(out, |mut out, (key, value)| {
if T::is_tuple() {
// try to convert to a 2-element tuple since that's a common use case from `HGETALL`, etc
out.push(T::from_value(Value::Array(vec![key.into(), value]))?);
} else {
out.push(T::from_value(key.into())?);
out.push(T::from_value(value)?);
}
Ok(out)
})
},
Value::Integer(i) => Ok(vec![T::from_value(Value::Integer(i))?]),
Value::Double(f) => Ok(vec![T::from_value(Value::Double(f))?]),
Value::Boolean(b) => Ok(vec![T::from_value(Value::Boolean(b))?]),
Value::Queued => Ok(vec![T::from_value(Value::from_static_str(QUEUED))?]),
Value::Null => Ok(Vec::new()),
}
}
}
impl<T, const N: usize> FromValue for [T; N]
where
T: FromValue,
{
fn from_value(value: Value) -> Result<[T; N], Error> {
debug_type!("FromValue([{}; {}]): {:?}", type_name::<T>(), N, value);
// use the `from_value` impl for Vec<T>
let value: Vec<T> = value.convert()?;
let len = value.len();
value
.try_into()
.map_err(|_| Error::new_parse(format!("Failed to convert to array. Expected {}, found {}.", N, len)))
}
}
impl<K, V, S> FromValue for HashMap<K, V, S>
where
K: FromKey + Eq + Hash,
V: FromValue,
S: BuildHasher + Default,
{
fn from_value(value: Value) -> Result<Self, Error> {
debug_type!(
"FromValue(HashMap<{}, {}>): {:?}",
type_name::<K>(),
type_name::<V>(),
value
);
let as_map = if value.is_array() || value.is_map() || value.is_null() {
value
.into_map()
.map_err(|_| Error::new_parse("Cannot convert to map."))?
} else {
return Err(Error::new_parse("Cannot convert to map."));
};
as_map
.inner()
.into_iter()
.map(|(k, v)| Ok((K::from_key(k)?, V::from_value(v)?)))
.collect()
}
}
impl<V, S> FromValue for HashSet<V, S>
where
V: FromValue + Hash + Eq,
S: BuildHasher + Default,
{
fn from_value(value: Value) -> Result<Self, Error> {
debug_type!("FromValue(HashSet<{}>): {:?}", type_name::<V>(), value);
value.into_set()?.into_iter().map(|v| V::from_value(v)).collect()
}
}
impl<K, V> FromValue for BTreeMap<K, V>
where
K: FromKey + Ord,
V: FromValue,
{
fn from_value(value: Value) -> Result<Self, Error> {
debug_type!(
"FromValue(BTreeMap<{}, {}>): {:?}",
type_name::<K>(),
type_name::<V>(),
value
);
let as_map = if value.is_array() || value.is_map() || value.is_null() {
value
.into_map()
.map_err(|_| Error::new_parse("Cannot convert to map."))?
} else {
return Err(Error::new_parse("Cannot convert to map."));
};
as_map
.inner()
.into_iter()
.map(|(k, v)| Ok((K::from_key(k)?, V::from_value(v)?)))
.collect()
}
}
impl<V> FromValue for BTreeSet<V>
where
V: FromValue + Ord,
{
fn from_value(value: Value) -> Result<Self, Error> {
debug_type!("FromValue(BTreeSet<{}>): {:?}", type_name::<V>(), value);
value.into_set()?.into_iter().map(|v| V::from_value(v)).collect()
}
}
// adapted from mitsuhiko
macro_rules! impl_from_value_tuple {
() => ();
($($name:ident,)+) => (
#[doc(hidden)]
impl<$($name: FromValue),*> FromValue for ($($name,)*) {
fn is_tuple() -> bool {
true
}
#[allow(non_snake_case, unused_variables)]
fn from_value(v: Value) -> Result<($($name,)*), Error> {
if let Value::Array(mut values) = v {
let mut n = 0;
$(let $name = (); n += 1;)*
debug_type!("FromValue({}-tuple): {:?}", n, values);
if values.len() != n {
return Err(Error::new_parse(format!("Invalid tuple dimension. Expected {}, found {}.", n, values.len())));
}
// since we have ownership over the values we have some freedom in how to implement this
values.reverse();
Ok(($({let $name = (); values
.pop()
.ok_or(Error::new_parse("Expected value, found none."))?
.convert()?
},)*))
}else{
Err(Error::new_parse("Could not convert to tuple."))
}
}
#[allow(non_snake_case, unused_variables)]
fn from_values(mut values: Vec<Value>) -> Result<Vec<($($name,)*)>, Error> {
let mut n = 0;
$(let $name = (); n += 1;)*
debug_type!("FromValue({}-tuple): {:?}", n, values);
if values.len() % n != 0 {
return Err(Error::new_parse(format!("Invalid tuple dimension. Expected {}, found {}.", n, values.len())));
}
let mut out = Vec::with_capacity(values.len() / n);
// this would be cleaner if there were an owned `chunks` variant
for chunk in values.chunks_exact_mut(n) {
match chunk {
[$($name),*] => out.push(($($name.take().convert()?),*),),
_ => unreachable!(),
}
}
Ok(out)
}
}
impl_from_value_peel!($($name,)*);
)
}
macro_rules! impl_from_value_peel {
($name:ident, $($other:ident,)*) => (impl_from_value_tuple!($($other,)*);)
}
impl_from_value_tuple! { T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, }
macro_rules! impl_from_str_from_key (
($t:ty) => {
impl FromKey for $t {
fn from_key(value: Key) -> Result<$t, Error> {
value
.as_str()
.and_then(|k| k.parse::<$t>().ok())
.ok_or(Error::new_parse("Cannot parse key from bytes."))
}
}
}
);
#[cfg(feature = "serde-json")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde-json")))]
impl FromValue for serde_json::Value {
fn from_value(value: Value) -> Result<Self, Error> {
let value = match value {
Value::Null => serde_json::Value::Null,
Value::Queued => QUEUED.into(),
Value::String(s) => {
// check for nested json. this is particularly useful with JSON.GET
serde_json::from_str(&s).ok().unwrap_or_else(|| s.to_string().into())
},
Value::Bytes(b) => {
let val = Value::String(Str::from_inner(b)?);
Self::from_value(val)?
},
Value::Integer(i) => i.into(),
Value::Double(f) => f.into(),
Value::Boolean(b) => b.into(),
Value::Array(v) => {
let mut out = Vec::with_capacity(v.len());
for value in v.into_iter() {
out.push(Self::from_value(value)?);
}
serde_json::Value::Array(out)
},
Value::Map(v) => {
let mut out = serde_json::Map::with_capacity(v.len());
for (key, value) in v.inner().into_iter() {
let key = key
.into_string()
.ok_or(Error::new_parse("Cannot convert key to string."))?;
let value = Self::from_value(value)?;
out.insert(key, value);
}
serde_json::Value::Object(out)
},
};
Ok(value)
}
}
#[cfg(feature = "i-geo")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-geo")))]
impl FromValue for GeoPosition {
fn from_value(value: Value) -> Result<Self, Error> {
GeoPosition::try_from(value)
}
}
#[cfg(feature = "i-slowlog")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-slowlog")))]
impl FromValue for SlowlogEntry {
fn from_value(value: Value) -> Result<Self, Error> {
SlowlogEntry::try_from(value)
}
}
#[cfg(feature = "i-cluster")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-cluster")))]
impl FromValue for ClusterInfo {
fn from_value(value: Value) -> Result<Self, Error> {
ClusterInfo::try_from(value)
}
}
#[cfg(feature = "i-memory")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-memory")))]
impl FromValue for MemoryStats {
fn from_value(value: Value) -> Result<Self, Error> {
MemoryStats::try_from(value)
}
}
#[cfg(feature = "i-memory")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-memory")))]
impl FromValue for DatabaseMemoryStats {
fn from_value(value: Value) -> Result<Self, Error> {
DatabaseMemoryStats::try_from(value)
}
}
impl FromValue for Key {
fn from_value(value: Value) -> Result<Self, Error> {
let key = match value {
Value::Boolean(b) => b.into(),
Value::Integer(i) => i.into(),
Value::Double(f) => f.into(),
Value::String(s) => s.into(),
Value::Bytes(b) => b.into(),
Value::Queued => Key::from_static_str(QUEUED),
Value::Map(_) | Value::Array(_) => return Err(Error::new_parse("Cannot convert aggregate type to key.")),
Value::Null => return Err(Error::new(ErrorKind::NotFound, "Cannot convert nil to key.")),
};
Ok(key)
}
}
/// A trait used to convert [Key](crate::types::Key) values to various types.
///
/// See the [convert](crate::types::Key::convert) documentation for more information.
pub trait FromKey: Sized {
fn from_key(value: Key) -> Result<Self, Error>;
}
impl_from_str_from_key!(u8);
impl_from_str_from_key!(u16);
impl_from_str_from_key!(u32);
impl_from_str_from_key!(u64);
impl_from_str_from_key!(u128);
impl_from_str_from_key!(usize);
impl_from_str_from_key!(i8);
impl_from_str_from_key!(i16);
impl_from_str_from_key!(i32);
impl_from_str_from_key!(i64);
impl_from_str_from_key!(i128);
impl_from_str_from_key!(isize);
impl_from_str_from_key!(f32);
impl_from_str_from_key!(f64);
impl FromKey for () {
fn from_key(_: Key) -> Result<Self, Error> {
Ok(())
}
}
impl FromKey for Value {
fn from_key(value: Key) -> Result<Self, Error> {
Ok(Value::Bytes(value.into_bytes()))
}
}
impl FromKey for Key {
fn from_key(value: Key) -> Result<Self, Error> {
Ok(value)
}
}
impl FromKey for String {
fn from_key(value: Key) -> Result<Self, Error> {
value
.into_string()
.ok_or(Error::new_parse("Cannot parse key as string."))
}
}
impl FromKey for Str {
fn from_key(value: Key) -> Result<Self, Error> {
Ok(Str::from_inner(value.into_bytes())?)
}
}
impl FromKey for Vec<u8> {
fn from_key(value: Key) -> Result<Self, Error> {
Ok(value.into_bytes().to_vec())
}
}
impl FromKey for Bytes {
fn from_key(value: Key) -> Result<Self, Error> {
Ok(value.into_bytes())
}
}
#[cfg(test)]
mod tests {
use crate::types::Value;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
#[cfg(not(feature = "default-nil-types"))]
use crate::error::Error;
#[test]
fn should_convert_signed_numeric_types() {
let _foo: i8 = Value::String("123".into()).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: i8 = Value::Integer(123).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: i16 = Value::String("123".into()).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: i16 = Value::Integer(123).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: i32 = Value::String("123".into()).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: i32 = Value::Integer(123).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: i64 = Value::String("123".into()).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: i64 = Value::Integer(123).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: i128 = Value::String("123".into()).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: i128 = Value::Integer(123).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: isize = Value::String("123".into()).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: isize = Value::Integer(123).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: f32 = Value::String("123.5".into()).convert().unwrap();
assert_eq!(_foo, 123.5);
let _foo: f64 = Value::String("123.5".into()).convert().unwrap();
assert_eq!(_foo, 123.5);
}
#[test]
fn should_convert_unsigned_numeric_types() {
let _foo: u8 = Value::String("123".into()).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: u8 = Value::Integer(123).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: u16 = Value::String("123".into()).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: u16 = Value::Integer(123).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: u32 = Value::String("123".into()).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: u32 = Value::Integer(123).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: u64 = Value::String("123".into()).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: u64 = Value::Integer(123).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: u128 = Value::String("123".into()).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: u128 = Value::Integer(123).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: usize = Value::String("123".into()).convert().unwrap();
assert_eq!(_foo, 123);
let _foo: usize = Value::Integer(123).convert().unwrap();
assert_eq!(_foo, 123);
}
#[test]
#[cfg(not(feature = "default-nil-types"))]
fn should_return_not_found_with_null_number_types() {
let result: Result<u8, Error> = Value::Null.convert();
assert!(result.unwrap_err().is_not_found());
let result: Result<u16, Error> = Value::Null.convert();
assert!(result.unwrap_err().is_not_found());
let result: Result<u32, Error> = Value::Null.convert();
assert!(result.unwrap_err().is_not_found());
let result: Result<u64, Error> = Value::Null.convert();
assert!(result.unwrap_err().is_not_found());
let result: Result<u128, Error> = Value::Null.convert();
assert!(result.unwrap_err().is_not_found());
let result: Result<usize, Error> = Value::Null.convert();
assert!(result.unwrap_err().is_not_found());
let result: Result<i8, Error> = Value::Null.convert();
assert!(result.unwrap_err().is_not_found());
let result: Result<i16, Error> = Value::Null.convert();
assert!(result.unwrap_err().is_not_found());
let result: Result<i32, Error> = Value::Null.convert();
assert!(result.unwrap_err().is_not_found());
let result: Result<i64, Error> = Value::Null.convert();
assert!(result.unwrap_err().is_not_found());
let result: Result<i128, Error> = Value::Null.convert();
assert!(result.unwrap_err().is_not_found());
let result: Result<isize, Error> = Value::Null.convert();
assert!(result.unwrap_err().is_not_found());
}
#[test]
#[cfg(feature = "default-nil-types")]
fn should_return_zero_with_null_number_types() {
assert_eq!(0, Value::Null.convert::<u8>().unwrap());
assert_eq!(0, Value::Null.convert::<u16>().unwrap());
assert_eq!(0, Value::Null.convert::<u32>().unwrap());
assert_eq!(0, Value::Null.convert::<u64>().unwrap());
assert_eq!(0, Value::Null.convert::<u128>().unwrap());
assert_eq!(0, Value::Null.convert::<usize>().unwrap());
assert_eq!(0, Value::Null.convert::<i8>().unwrap());
assert_eq!(0, Value::Null.convert::<i16>().unwrap());
assert_eq!(0, Value::Null.convert::<i32>().unwrap());
assert_eq!(0, Value::Null.convert::<i64>().unwrap());
assert_eq!(0, Value::Null.convert::<i128>().unwrap());
assert_eq!(0, Value::Null.convert::<isize>().unwrap());
assert_eq!(0.0, Value::Null.convert::<f32>().unwrap());
assert_eq!(0.0, Value::Null.convert::<f64>().unwrap());
}
#[test]
#[cfg(feature = "default-nil-types")]
fn should_convert_null_to_false() {
assert!(!Value::Null.convert::<bool>().unwrap());
}
#[test]
#[should_panic]
#[cfg(not(feature = "default-nil-types"))]
fn should_not_convert_null_to_false() {
assert!(!Value::Null.convert::<bool>().unwrap());
}
#[test]
fn should_convert_strings() {
let _foo: String = Value::String("foo".into()).convert().unwrap();
assert_eq!(_foo, "foo".to_owned());
}
#[test]
fn should_convert_numbers_to_bools() {
let foo: bool = Value::Integer(0).convert().unwrap();
assert!(!foo);
let foo: bool = Value::Integer(1).convert().unwrap();
assert!(foo);
let foo: bool = Value::String("0".into()).convert().unwrap();
assert!(!foo);
let foo: bool = Value::String("1".into()).convert().unwrap();
assert!(foo);
}
#[test]
fn should_convert_bytes() {
let foo: Vec<u8> = Value::Bytes("foo".as_bytes().to_vec().into()).convert().unwrap();
assert_eq!(foo, "foo".as_bytes().to_vec());
let foo: Vec<u8> = Value::String("foo".into()).convert().unwrap();
assert_eq!(foo, "foo".as_bytes().to_vec());
let foo: Vec<u8> = Value::Array(vec![102.into(), 111.into(), 111.into()])
.convert()
.unwrap();
assert_eq!(foo, "foo".as_bytes().to_vec());
}
#[test]
fn should_convert_arrays() {
let foo: Vec<String> = Value::Array(vec!["a".into(), "b".into()]).convert().unwrap();
assert_eq!(foo, vec!["a".to_owned(), "b".to_owned()]);
}
#[test]
fn should_convert_hash_maps() {
let foo: HashMap<String, u16> = Value::Array(vec!["a".into(), 1.into(), "b".into(), 2.into()])
.convert()
.unwrap();
let mut expected = HashMap::new();
expected.insert("a".to_owned(), 1);
expected.insert("b".to_owned(), 2);
assert_eq!(foo, expected);
}
#[test]
fn should_convert_hash_sets() {
let foo: HashSet<String> = Value::Array(vec!["a".into(), "b".into()]).convert().unwrap();
let mut expected = HashSet::new();
expected.insert("a".to_owned());
expected.insert("b".to_owned());
assert_eq!(foo, expected);
}
#[test]
fn should_convert_btree_maps() {
let foo: BTreeMap<String, u16> = Value::Array(vec!["a".into(), 1.into(), "b".into(), 2.into()])
.convert()
.unwrap();
let mut expected = BTreeMap::new();
expected.insert("a".to_owned(), 1);
expected.insert("b".to_owned(), 2);
assert_eq!(foo, expected);
}
#[test]
fn should_convert_btree_sets() {
let foo: BTreeSet<String> = Value::Array(vec!["a".into(), "b".into()]).convert().unwrap();
let mut expected = BTreeSet::new();
expected.insert("a".to_owned());
expected.insert("b".to_owned());
assert_eq!(foo, expected);
}
#[test]
fn should_convert_tuples() {
let foo: (String, i64) = Value::Array(vec!["a".into(), 1.into()]).convert().unwrap();
assert_eq!(foo, ("a".to_owned(), 1));
}
#[test]
fn should_convert_array_tuples() {
let foo: Vec<(String, i64)> = Value::Array(vec!["a".into(), 1.into(), "b".into(), 2.into()])
.convert()
.unwrap();
assert_eq!(foo, vec![("a".to_owned(), 1), ("b".to_owned(), 2)]);
}
#[test]
fn should_handle_single_element_vector_to_scalar() {
assert!(Value::Array(vec![]).convert::<String>().is_err());
assert_eq!(Value::Array(vec!["foo".into()]).convert::<String>(), Ok("foo".into()));
assert!(Value::Array(vec!["foo".into(), "bar".into()])
.convert::<String>()
.is_err());
assert_eq!(Value::Array(vec![]).convert::<Option<String>>(), Ok(None));
assert_eq!(
Value::Array(vec!["foo".into()]).convert::<Option<String>>(),
Ok(Some("foo".into()))
);
assert!(Value::Array(vec!["foo".into(), "bar".into()])
.convert::<Option<String>>()
.is_err());
}
#[test]
fn should_convert_null_to_empty_array() {
assert_eq!(Vec::<String>::new(), Value::Null.convert::<Vec<String>>().unwrap());
assert_eq!(Vec::<u8>::new(), Value::Null.convert::<Vec<u8>>().unwrap());
}
#[test]
fn should_convert_to_fixed_arrays() {
let foo: [i64; 2] = Value::Array(vec![1.into(), 2.into()]).convert().unwrap();
assert_eq!(foo, [1, 2]);
assert!(Value::Array(vec![1.into(), 2.into()]).convert::<[i64; 3]>().is_err());
assert!(Value::Array(vec![]).convert::<[i64; 3]>().is_err());
}
}