Skip to content

Commit b1f80e0

Browse files
author
Will Nelson
committed
Fix data option deserialization
1 parent fe05696 commit b1f80e0

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

resp/src/data/de.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,19 @@ impl<'de> de::Deserializer<'de> for Data<'de> {
207207
self.deserialize_map(visitor)
208208
}
209209

210+
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
211+
where
212+
V: de::Visitor<'de>,
213+
{
214+
match self {
215+
Self::Null => visitor.visit_none(),
216+
_ => visitor.visit_some(self),
217+
}
218+
}
219+
210220
forward_to_deserialize_any! {
211221
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
212-
bytes byte_buf option unit unit_struct newtype_struct seq tuple
222+
bytes byte_buf unit unit_struct newtype_struct seq tuple
213223
tuple_struct enum identifier ignored_any
214224
}
215225
}

src/model/stream/read.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub struct ReadResponse<'a>(
3939
mod test {
4040
use std::borrow::Cow;
4141

42-
use redust_resp::{array, from_data, Data};
42+
use redust_resp::{array, from_bytes, from_data, Data};
4343

4444
use crate::model::stream::{
4545
read::{Field, Key, Value},
@@ -49,7 +49,7 @@ mod test {
4949
use super::ReadResponse;
5050

5151
#[test]
52-
fn stream_read() {
52+
fn stream_read_some() {
5353
let data = array![array![
5454
Data::BulkString(b"foo"[..].into()),
5555
array![array![
@@ -61,10 +61,21 @@ mod test {
6161
]]
6262
]];
6363

64-
let resp: ReadResponse = from_data(data).expect("read data");
64+
let resp = from_data::<Option<ReadResponse>>(data)
65+
.expect("read data")
66+
.expect("some");
6567
assert_eq!(
6668
resp.0[&Key(b"foo"[..].into())].0[&Id(1, 0)][&Field(b"abc"[..].into())],
6769
Value(Cow::from(&b"def"[..]))
6870
);
6971
}
72+
73+
#[test]
74+
fn stream_read_none() {
75+
let bytes = b"*-1\r\n";
76+
77+
let (resp, rem) = from_bytes::<Option<ReadResponse>>(bytes).expect("read data");
78+
assert_eq!(resp, None);
79+
assert_eq!(rem, []);
80+
}
7081
}

0 commit comments

Comments
 (0)