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
//! Alternate conversions from [`Value`]
use crate::{FromRedisValue, ParsingError, Value};
/// Tries to parse a [`Value`] as `Vec<T>`, and that failing as `T`, yielding it as one-item `Vec`
pub(super) fn try_vec_then_singleton<T: FromRedisValue>(v: Value) -> Result<Vec<T>, ParsingError> {
// If it's an array, try parsing as array
//
// We cannot use `Vec::<T>::from_redis_value` as that would consume `v`, which is still needed
// when trying to parse as `T` later.
//
// We do not want to use `Vec::<T>::from_redis_value_ref` as that would mean more allocations,
// if the conversion works.
let ret = if let Value::Array(arr) = v {
arr.into_iter()
.map(|item| T::from_redis_value(item))
.collect::<Result<Vec<_>, ParsingError>>()?
} else {
// Fallback to trying to parse as singleton
vec![T::from_redis_value(v)?]
};
Ok(ret)
}
/// Tries to parse a [`Value`] as `T` (yielding it as one-item `Vec`), and that failing as `Vec<T>`
pub(super) fn try_singleton_then_vec<T: FromRedisValue>(v: Value) -> Result<Vec<T>, ParsingError> {
// Trying to parse as `T`
if let Ok(item) = T::from_redis_value_ref(&v) {
return Ok(vec![item]);
}
// Parsing as `T` failed, so we try parsing as `Vec<T>`
Vec::<T>::from_redis_value(v)
}
/// Recursive descend helper for [`flattened_array`]
fn flattened_array_rec_desc<T: FromRedisValue>(
v: Value,
collector: &mut Vec<T>,
) -> Result<(), ParsingError> {
match v {
Value::Array(elements) => {
for element in elements {
flattened_array_rec_desc(element, collector)?;
}
}
_ => {
collector.push(T::from_redis_value(v)?);
}
}
Ok(())
}
/// Tries to parse a [`Value`] as `T`s descending recursively into arrays
pub(super) fn flattened_array<T: FromRedisValue>(v: Value) -> Result<Vec<T>, ParsingError> {
let mut collector = Vec::new();
flattened_array_rec_desc(v, &mut collector)?;
Ok(collector)
}
#[cfg(test)]
mod test {
use super::*;
use crate::Value::*;
use rstest::rstest;
/// Tries to assure that `try_vec_then_singleton` can convert an Array
#[test]
fn try_vec_then_singleton_array() {
// The value to test with
let value = Array(vec![Int(4711), Nil, Int(42)]);
// The actual conversion
let converted = try_vec_then_singleton::<Option<i64>>(value).unwrap();
// Check the resulting value
assert_eq!(*converted, vec![Some(4711), None, Some(42)]);
}
/// Tries to assure that `try_vec_then_singleton` can convert a basic value
#[test]
fn try_vec_then_singleton_basic() {
// The value to test with
let value = Int(4711);
// The actual conversion
let converted = try_vec_then_singleton::<Option<i64>>(value).unwrap();
// Check the resulting value
assert_eq!(*converted, vec![Some(4711)]);
}
/// Tries to assure that if both `Vec<T>` and `T` work `try_vec_then_singleton` picks correctly
#[test]
fn try_vec_then_singleton_ambiguous() {
// The value to test with
let value = Array(vec![BulkString("foo".into()), BulkString("bar".into())]);
// The actual conversion
let converted = try_vec_then_singleton::<Option<Vec<String>>>(value).unwrap();
// Check the resulting value
assert_eq!(
*converted,
vec![Some(vec!["foo".to_string()]), Some(vec!["bar".to_string()])]
);
}
/// Tries to assure that `try_vec_then_singleton` fails for unconvertable values
#[test]
fn try_vec_then_singleton_other_fails() {
// The value to test with
let value = BulkString("foo".into());
// The actual conversion should fail as a `str` should not convert to `i64` or `Vec<i64>`.
let err = try_vec_then_singleton::<Option<i64>>(value).unwrap_err();
// Check the resulting value
assert!(err.description.contains("not convert"));
}
/// Tries to assure that `try_singleton_then_vec` can convert an Array
#[test]
fn try_singleton_then_vec_array() {
// The value to test with
let value = Array(vec![Int(4711), Nil, Int(42)]);
// The actual conversion
let converted = try_singleton_then_vec::<Option<i64>>(value).unwrap();
// Check the resulting value
assert_eq!(*converted, vec![Some(4711), None, Some(42)]);
}
/// Tries to assure that `try_singleton_then_vec` can convert a basic value
#[test]
fn try_singleton_then_vec_basic() {
// The value to test with
let value = Int(4711);
// The actual conversion
let converted = try_singleton_then_vec::<Option<i64>>(value).unwrap();
// Check the resulting value
assert_eq!(*converted, vec![Some(4711)]);
}
/// Tries to assure that if both `Vec<T>` and `T` work `try_singleton_then_vec` picks correctly
#[test]
fn try_singleton_then_vec_ambiguous() {
// The value to test with
let value = Array(vec![BulkString("foo".into()), BulkString("bar".into())]);
// The actual conversion
let converted = try_singleton_then_vec::<Option<Vec<String>>>(value).unwrap();
// Check the resulting value
assert_eq!(
*converted,
vec![Some(vec!["foo".to_string(), "bar".to_string()])]
);
}
/// Tries to assure that `try_singleton_then_vec` fails for unconvertable values
#[test]
fn try_singleton_then_vec_other_fails() {
// The value to test with
let value = BulkString("foo".into());
// The actual conversion should fail as a `str` should not convert to `i64` or `Vec<i64>`.
let err = try_singleton_then_vec::<Option<i64>>(value).unwrap_err();
// Check the resulting value
assert!(err.description.contains("not convert"));
}
#[rstest]
#[case::direct_item(Int(42), vec![42])]
#[case::empty_array(Array(vec![]), vec![])]
#[case::flat_array(Array(vec![Int(42), Double(4711.)]), vec![42, 4711])]
#[case::nested_array(Array(vec![Int(23), Array(vec![Array(vec![Int(42)]), Int(151)])]), vec![23, 42, 151])]
fn flattened_conversion_success(#[case] input: Value, #[case] expected: Vec<u64>) {
let result = flattened_array::<u64>(input).unwrap();
assert_eq!(*result, expected);
}
#[rstest]
#[case::direct_item(BulkString("foo".into()))]
#[case::flat_array(Array(vec![Int(42), BulkString("foo".into()), Int(4711)]))]
#[case::nested_array(Array(vec![Int(23), Array(vec![Int(42), BulkString("foo".into()), Int(151)]), Int(4711)]))]
fn flattened_conversion_failure(#[case] input: Value) {
let err = flattened_array::<u64>(input).unwrap_err();
assert!(err.description.contains("convert"));
}
}