zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
use std::fmt;

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

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

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

            true
        } else {
            true
        }
    }

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

    pub fn mode(&self) -> String {
        self.connect_mode.clone().unwrap()
    }

    pub fn get(settings: &Option<Settings>) -> 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.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://keesh:Cc@127.0.0.1:6379/0
// redis://[<username>][:<password>@]<hostname>[:port][/<db>]
impl fmt::Display for Settings {
    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 + "@"))?;
        }

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

        Ok(())
    }
}