bouncer 1.0.1

Bouncer allows you handle your requests to third-party services with no worries, perhaps some worries.
Documentation
use crate::settings::Settings;
use crate::stats::Stats;

use redis::{Client, RedisResult, Script};
use std::fs::read_to_string;
use std::thread;

#[derive(Clone)]
pub struct Bouncer<'a> {
    pub key: u8,
    pub rate_limit: u8,
    pub wait_time: u8,
    pub block: &'a dyn Fn(),
}

impl<'a> Default for Bouncer<'a> {
    /// Default values for a Bouncer instance.
    fn default() -> Bouncer<'a> {
        Bouncer {
            key: 1,
            rate_limit: 5,
            wait_time: 2,
            block: &|| println!("Nothing to execute"),
        }
    }
}

impl<'a> Bouncer<'a> {
    /// Returns a Bouncer instance
    ///
    /// # Arguments
    ///
    /// * `block` - A block or a function that will execute a request to a third-party service.
    pub fn new(block: &'a dyn Fn()) -> Bouncer {
        let mut bouncer = Bouncer::default();
        bouncer.block = block;
        bouncer
    }

    /// Assigns a key to the process to be executed.
    ///
    /// # Arguments
    ///
    /// * `key` - A integer to identify the process.
    pub fn key(&'a mut self, key: u8) -> &'a mut Bouncer {
        self.key = key;
        self
    }

    /// Assigns a rate limit to the process to be executed.
    ///
    /// # Arguments
    ///
    /// * `rate_limit` - limit the number of calls.
    pub fn rate_limit(&'a mut self, rate_limit: u8) -> &'a mut Bouncer {
        self.rate_limit = rate_limit;
        self
    }

    /// Assigns a waiting time to the process to be executed.
    ///
    /// # Arguments
    ///
    /// * `wait_time` - A value in seconds to wait until the next request to be executed.
    pub fn wait_time(&'a mut self, wait_time: u8) -> &'a mut Bouncer {
        self.wait_time = wait_time;
        self
    }

    /// Executes the passed block.
    pub fn run(&self) -> RedisResult<Stats> {
        match Settings::new() {
            Ok(settings) => {
                let raw_script = read_to_string(settings.redis_script())?;
                let script = Script::new(raw_script.as_str());
                let client = Client::open(settings.redis_url())?;
                let mut connection = client.get_connection()?;
                loop {
                    let stats: Stats = script
                        .key(self.key)
                        .arg(self.rate_limit)
                        .arg(self.wait_time)
                        .invoke(&mut connection)
                        .unwrap();
                    if stats == Stats::default() {
                        (self.block)();
                        break Ok(stats);
                    } else {
                        thread::sleep(stats.wait)
                    }
                }
            }
            Err(error) => panic!("{}", error),
        }
    }
}