redis_json/
util.rs

1use redis::RedisError;
2use std::str::FromStr;
3
4use crate::error::Error;
5
6/// Parse value from redis::Value::Data
7pub fn from_str<T>(value: &redis::Value) -> Result<T, Error>
8where
9  T: FromStr,
10  Error: From<<T as FromStr>::Err>,
11{
12  match *value {
13    redis::Value::Data(ref bytes) => std::str::from_utf8(&bytes[..])
14      .map_err(|err| Error::Utf8(err))
15      .and_then(|value| <T>::from_str(value).map_err(Error::from)),
16    _ => Err(
17      RedisError::from((
18        redis::ErrorKind::TypeError,
19        "invalid response type for JSON",
20      ))
21      .into(),
22    ),
23  }
24}