@@ -5,7 +5,7 @@ use std::{
55
66use anyhow:: anyhow;
77use ecow:: { EcoString , EcoVec } ;
8- use redis:: { FromRedisValue , RedisResult , ToRedisArgs } ;
8+ use redis:: { FromRedisValue , ToRedisArgs , ToSingleRedisArg } ;
99use serde:: de:: { self , Deserialize , Deserializer , MapAccess , SeqAccess , Visitor } ;
1010use serde:: ser:: { Serialize , SerializeMap , SerializeSeq , Serializer } ;
1111use serde_json:: Value as JValue ;
@@ -51,36 +51,38 @@ impl ToRedisArgs for Value {
5151 }
5252}
5353
54+ // Note: Redis docs say: "This should be implemented only for types that are
55+ // serialized into exactly one value, otherwise the compiler can't ensure
56+ // the correctness of some commands."
57+ // This currently holds for the implementation of Value, but we should keep an eye out.
58+ impl ToSingleRedisArg for Value { }
59+
5460impl FromRedisValue for Value {
55- fn from_redis_value ( v : & redis:: Value ) -> RedisResult < Self > {
61+ fn from_redis_value ( v : redis:: Value ) -> Result < Self , redis :: ParsingError > {
5662 match v {
5763 redis:: Value :: BulkString ( bytes) => {
58- let s = std:: str:: from_utf8 ( bytes) . map_err ( |_ | {
59- redis:: RedisError :: from ( ( redis :: ErrorKind :: TypeError , "Invalid UTF-8" ) )
64+ let s = std:: str:: from_utf8 ( & bytes) . map_err ( |e | {
65+ redis:: ParsingError :: from ( format ! ( "Invalid UTF-8 in BulkString: {:?}" , e ) )
6066 } ) ?;
6167
62- serde_json5:: from_str ( s) . map_err ( |_e| {
63- redis:: RedisError :: from ( (
64- redis:: ErrorKind :: TypeError ,
65- "Response type not deserializable to Value with serde_json5" ,
66- format ! (
67- "(response was {:?})" ,
68- redis:: Value :: BulkString ( bytes. clone( ) )
69- ) ,
68+ serde_json5:: from_str ( s) . map_err ( |e| {
69+ redis:: ParsingError :: from ( format ! (
70+ "BulkString not deserializable to Value with serde_json5: {}" ,
71+ e
7072 ) )
7173 } )
7274 }
7375 redis:: Value :: Array ( values) => {
7476 let list: Result < Vec < Value > , _ > =
75- values. iter ( ) . map ( Value :: from_redis_value ) . collect ( ) ;
77+ values. iter ( ) . map ( Value :: from_redis_value_ref ) . collect ( ) ;
7678 Ok ( Value :: List ( list?. into ( ) ) )
7779 }
7880 redis:: Value :: Nil => Ok ( Value :: Unit ) ,
79- redis:: Value :: Int ( i) => Ok ( Value :: Int ( * i) ) ,
81+ redis:: Value :: Int ( i) => Ok ( Value :: Int ( i) ) ,
8082 redis:: Value :: SimpleString ( s) => Ok ( Value :: Str ( s. clone ( ) . into ( ) ) ) ,
81- _ => Err ( redis:: RedisError :: from ( (
82- redis :: ErrorKind :: TypeError ,
83- "Response type not deserializable to Value" ,
83+ _ => Err ( redis:: ParsingError :: from ( std :: format! (
84+ "Unsupported Redis value type for Value deserialization: {:?}" ,
85+ v
8486 ) ) ) ,
8587 }
8688 }
0 commit comments