whos_your_daddy_common 0.1.2

Common source code for the Who's Your Daddy projects, like the Enumerator and Presenter.
Documentation
//! Configuration helpers for establishing database connectivity.
//!
//! This module defines `DatabaseConfig` and helpers to load its values from
//! environment variables. It is intended to be shared across crates that need
//! database configuration.

use serde::{Deserialize, Serialize};
use std::env;

use crate::error::Error::ConfigError;



/// Name of the environment variable containing the database host name.
const ENV_DB_HOST: &str = "DB_HOST";

/// Name of the environment variable containing the database name.
const ENV_DB_NAME: &str = "DB_NAME";

/// Name of the environment variable containing the database password.
const ENV_DB_PASSWORD: &str = "DB_PASSWORD";

/// Name of the environment variable containing the database port number.
const ENV_DB_PORT: &str = "DB_PORT";

/// Name of the environment variable containing the database user.
const ENV_DB_USER: &str = "DB_USER";

/// The `DatabaseConfig` structure defines the configuration data needed for
/// this application to interact with the backing database.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct DatabaseConfig {
    /// The `host` field specifies the host name where the PostgreSQL server
    /// is running.
    pub host: String,

    /// The `name` field defines the database's name.
    pub name: String,

    /// The `password` field contains the password for the user account for
    /// communicating with the PostgreSQL server.
    pub password: String,

    /// The `port` field defines the port number used to communicate with the
    /// database.
    pub port: i32,

    /// The `user` field contains the user name for the account for
    /// communicating with the PostgreSQL server.
    pub user: String,
}

/// This is the `Default` trait implementation to construct a DatabaseConfig
/// structure using default values.
impl Default for DatabaseConfig {
    fn default() -> Self {
        DatabaseConfig {
            host: String::from("localhost"),
            name: String::from("postgres"),
            password: String::from("postgres"),
            port: 5432,
            user: String::from("postgres"),
        }
    }
}

impl DatabaseConfig {
    /// This function attempts to collect configuration data for the
    /// `DatabaseConfig` struct from the environment.
    ///
    /// # Returns
    /// This function returns a `Result` variant:
    /// - `Ok()` containing the config data.
    /// - `Err()` describing the error.
    pub fn from_env() -> Result<Self, crate::error::Error> {
        // Try to get the database server host name from the environment config.
        let env_host = match env::var(ENV_DB_HOST) {
            Ok(value) => value,
            Err(e) => return Err(ConfigError(e.to_string())),
        };

        // Try to get the database name from the environment config.
        let env_name = match env::var(ENV_DB_NAME) {
            Ok(value) => value,
            Err(e) => return Err(ConfigError(e.to_string())),
        };

        // Try to get the database account password from the environment config.
        let env_pw = match env::var(ENV_DB_PASSWORD) {
            Ok(value) => value,
            Err(e) => return Err(ConfigError(e.to_string())),
        };

        // Try to get the database connection port number from the environment
        // config.
        let env_port = match env::var(ENV_DB_PORT) {
            Ok(value) => match value.parse::<i32>() {
                Ok(int_value) => int_value,
                Err(e) => return Err(ConfigError(e.to_string())),
            },
            Err(e) => return Err(ConfigError(e.to_string())),
        };

        // Try to get the database account username from the environment config.
        let env_user = match env::var(ENV_DB_USER) {
            Ok(value) => value,
            Err(e) => return Err(ConfigError(e.to_string())),
        };

        Ok(Self {
            host: env_host,
            name: env_name,
            password: env_pw,
            port: env_port,
            user: env_user,
        })
    } // end from_env
} // DatabaseConfig