#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(
anonymous_parameters,
missing_copy_implementations,
missing_debug_implementations,
missing_docs,
nonstandard_style,
rust_2018_idioms,
single_use_lifetimes,
trivial_casts,
trivial_numeric_casts,
unreachable_pub,
unused_extern_crates,
unused_qualifications,
variant_size_differences
)]
use salak::*;
#[cfg(feature = "pool")]
#[cfg_attr(docsrs, doc(cfg(feature = "pool")))]
pub mod pool;
#[cfg(feature = "postgresql")]
#[cfg_attr(docsrs, doc(cfg(feature = "postgresql")))]
pub mod postgresql;
#[cfg(feature = "redis_default")]
#[cfg_attr(docsrs, doc(cfg(feature = "redis_default")))]
pub mod redis_default;
#[cfg(feature = "redis_cluster")]
#[cfg_attr(docsrs, doc(cfg(feature = "redis_cluster")))]
pub mod redis_cluster;
pub const DEFAULT_NAMESPACE: &str = "primary";
pub trait Buildable: Sized + DefaultSourceFromEnvironment {
type Product;
type Customizer: Default;
fn build(
namespace: &str,
env: &impl Environment,
customize: Self::Customizer,
) -> Result<Self::Product, PropertyError> {
let config = if namespace.is_empty() || namespace == DEFAULT_NAMESPACE {
env.require(Self::prefix())
} else {
env.require(&format!("{}.{}", Self::prefix(), namespace))
};
Self::build_with_key(config?, env, customize)
}
fn build_with_key(
self,
env: &impl Environment,
customize: Self::Customizer,
) -> Result<Self::Product, PropertyError>;
}
pub trait Factory: Environment {
fn build<T: Buildable>(&self) -> Result<T::Product, PropertyError> {
self.build_by_namespace::<T>(DEFAULT_NAMESPACE)
}
fn build_by_namespace<T: Buildable>(
&self,
namespace: &str,
) -> Result<T::Product, PropertyError> {
self.build_by_namespace_and_customizer::<T>(namespace, T::Customizer::default())
}
fn build_by_customizer<T: Buildable>(
&self,
customizer: T::Customizer,
) -> Result<T::Product, PropertyError> {
self.build_by_namespace_and_customizer::<T>(DEFAULT_NAMESPACE, customizer)
}
fn build_by_namespace_and_customizer<T: Buildable>(
&self,
namespace: &str,
customizer: T::Customizer,
) -> Result<T::Product, PropertyError>;
}
impl Factory for Salak {
fn build_by_namespace_and_customizer<T: Buildable>(
&self,
namespace: &str,
customizer: T::Customizer,
) -> Result<T::Product, PropertyError> {
T::build(namespace, self, customizer)
}
}
#[doc(hidden)]
#[derive(Debug)]
pub struct WrapEnum<T>(pub(crate) T);