zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
pub mod redis_client;
pub mod redis_client_cluster;
pub mod redis_client_sentinel;
pub mod redis_client_standalone;
pub mod redis_pubsub_async;
pub mod redis_pubsub_standalone_async;
pub mod with_pubsub_async;
pub mod with_r2d2;

pub use redis_client::*;
pub use redis_client_cluster::*;
pub use redis_client_sentinel::*;
pub use redis_client_standalone::*;
pub use redis_pubsub_async::*;
pub use redis_pubsub_standalone_async::*;
pub use with_pubsub_async::*;
pub use with_r2d2::*;

use std::fmt;

use secrecy::ExposeSecret;
use secrecy::Secret;

#[derive(serde::Deserialize, Clone)]
pub struct RedisSettings {
    connect_mode: Option<String>, // Disabled, Standalone, Sentinel, Cluster
    server: Option<String>,
    username: Option<String>,
    password: Option<Secret<String>>,
    hostname: Option<String>,
    port: Option<u32>,
    db: Option<u32>,
    max_pool_size: Option<u32>,
    connection_timeout_secs: Option<u32>,
}

impl Default for RedisSettings {
    fn default() -> Self {
        Self {
            connect_mode: Some("Disabled".to_string()),
            server: None,
            username: None,
            password: None,
            hostname: Some("127.0.0.1".to_string()),
            port: Some(6379),
            db: Some(0),
            max_pool_size: Some(30),
            connection_timeout_secs: Some(10),
        }
    }
}

impl RedisSettings {
    pub fn is_disable(&self) -> bool {
        if let Some(mode) = &self.connect_mode {
            if mode != "Disabled" {
                return false;
            }
        }

        true
    }

    pub fn connection_string(&self) -> String {
        format!("{}", self)
    }

    pub fn get(settings: &Option<RedisSettings>) -> Self {
        let _default = Self::default();

        if settings.is_some() {
            let mut s = settings.clone().unwrap();

            if s.connect_mode.is_none() {
                s.connect_mode = _default.connect_mode;
            }

            if s.server.is_none() {
                s.server = _default.server;
            }

            if s.username.is_none() {
                s.username = _default.username;
            }

            if s.password.is_none() {
                s.password = _default.password;
            }

            if s.hostname.is_none() {
                s.hostname = _default.hostname;
            }

            if s.port.is_none() {
                s.port = _default.port;
            }

            if s.db.is_none() {
                s.db = _default.db;
            }

            if s.max_pool_size.is_none() {
                s.max_pool_size = _default.max_pool_size;
            }

            if s.connection_timeout_secs.is_none() {
                s.connection_timeout_secs = _default.connection_timeout_secs;
            }

            s
        } else {
            _default
        }
    }
}

// redis://[<username>][:<password>@]<hostname>[:port][/<db>]
impl fmt::Display for RedisSettings {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(s) = &self.server {
            f.write_str(s)?;
            return Ok(());
        }

        f.write_str("redis://")?;

        if let Some(u) = &self.username {
            f.write_str(u)?;
        }

        if let Some(p) = &self.password {
            f.write_str(&(":".to_owned() + p.expose_secret() + "@"))?;
        }

        if let Some(h) = &self.hostname {
            f.write_str(h)?;
        } else {
            f.write_str("127.0.0.1")?;
        }

        if let Some(p) = self.port {
            write!(f, ":{}", p)?;
        } else {
            f.write_str(":6379")?;
        }

        if let Some(d) = self.db {
            write!(f, "/{}", d)?;
        } else {
            f.write_str("/0")?;
        }

        Ok(())
    }
}