bouncer 1.0.1

Bouncer allows you handle your requests to third-party services with no worries, perhaps some worries.
Documentation
use crate::error::Error;

use redis::RedisResult;
use serde::Deserialize;
use std::time::Duration;
#[derive(Debug, Deserialize, PartialEq)]
pub struct Stats {
    old: String,
    current: String,
    since: u8,
    #[serde(deserialize_with = "wait_deserializer")]
    pub wait: Duration,
}

use serde::Deserializer;
pub fn wait_deserializer<'de, D>(d: D) -> Result<Duration, D::Error>
where
    D: Deserializer<'de>,
{
    Deserialize::deserialize(d).map(|x: Option<_>| x.unwrap_or(Duration::from_millis(100)))
}

impl Default for Stats {
    fn default() -> Stats {
        Stats {
            current: 0.to_string(),
            old: 0.to_string(),
            since: 0,
            wait: Duration::from_millis(0),
        }
    }
}

use redis::{FromRedisValue, Value};
use std::result::Result;
use std::str::from_utf8;
impl FromRedisValue for Stats {
    fn from_redis_value(v: &Value) -> RedisResult<Stats> {
        let rv: RedisResult<Stats> = match *v {
            Value::Data(ref d) => {
                let result = serde_json::from_str(from_utf8(d)?);
                let v: Stats = match result {
                    Ok(value) => value,
                    Err(_) => Stats::default(),
                };
                Ok(v)
            }
            _ => Result::Err(Error::IncompatibleJson(v))?,
        };
        match rv {
            Ok(value) => Ok(value),
            Err(_) => Result::Err(Error::InvalidJson(v))?,
        }
    }
}