use crate::config::SettingsConfigBuilder;
use crate::config::SettingsSchema;
use crate::error::Result;
use crate::storage::StorageBackend;
use crate::sub_settings::SubSettingsConfig;
use std::path::PathBuf;
use super::SettingsManager;
pub struct SettingsManagerBuilder<
S: StorageBackend = crate::storage::JsonStorage,
Schema: SettingsSchema = (),
> {
config_builder: SettingsConfigBuilder<S, Schema>,
sub_settings: Vec<SubSettingsConfig>,
}
impl<S: StorageBackend, Schema: SettingsSchema> SettingsManagerBuilder<S, Schema> {
pub fn new(
app_name: impl Into<String>,
app_version: impl Into<String>,
) -> SettingsManagerBuilder {
SettingsManagerBuilder {
config_builder: SettingsConfigBuilder::new(app_name, app_version),
sub_settings: Vec::new(),
}
}
}
impl<S: StorageBackend, Schema: SettingsSchema> SettingsManagerBuilder<S, Schema> {
#[must_use]
pub fn with_config_dir(mut self, path: impl Into<PathBuf>) -> Self {
self.config_builder = self.config_builder.with_config_dir(path);
self
}
#[must_use]
pub fn with_settings_file(mut self, filename: impl Into<String>) -> Self {
self.config_builder = self.config_builder.settings_file(filename);
self
}
#[must_use]
pub fn with_credentials(mut self) -> Self {
self.config_builder = self.config_builder.with_credentials();
self
}
#[must_use]
pub fn with_credential_config(mut self, config: crate::config::CredentialConfig) -> Self {
self.config_builder = self.config_builder.with_credential_config(config);
self
}
#[cfg(all(feature = "keychain", feature = "encrypted-file"))]
#[must_use]
pub fn with_env_credentials(mut self) -> Self {
self.config_builder = self.config_builder.with_env_credentials();
self
}
#[cfg(all(feature = "keychain", feature = "encrypted-file"))]
#[must_use]
pub fn with_custom_env_credentials(mut self, var_name: impl Into<String>) -> Self {
self.config_builder = self.config_builder.with_custom_env_credentials(var_name);
self
}
#[cfg(all(feature = "keychain", feature = "encrypted-file"))]
#[must_use]
pub fn with_file_credentials(mut self, path: impl Into<std::path::PathBuf>) -> Self {
self.config_builder = self.config_builder.with_file_credentials(path);
self
}
#[cfg(all(feature = "keychain", feature = "encrypted-file"))]
#[must_use]
pub fn with_password_credentials(mut self, password: impl Into<String>) -> Self {
self.config_builder = self.config_builder.with_password_credentials(password);
self
}
#[must_use]
pub fn with_env_prefix(mut self, prefix: impl Into<String>) -> Self {
self.config_builder = self.config_builder.with_env_prefix(prefix);
self
}
#[must_use]
pub fn env_overrides_secrets(mut self, allow: bool) -> Self {
self.config_builder = self.config_builder.env_overrides_secrets(allow);
self
}
#[must_use]
pub fn with_migrator<F>(mut self, migrator: F) -> Self
where
F: Fn(serde_json::Value) -> serde_json::Value + Send + Sync + 'static,
{
self.config_builder = self.config_builder.with_migrator(migrator);
self
}
#[cfg(feature = "hot-reload")]
#[must_use]
pub fn with_hot_reload(mut self) -> Self {
self.config_builder = self.config_builder.with_hot_reload();
self
}
#[cfg(feature = "hot-reload")]
#[must_use]
pub fn with_hot_reload_config(mut self, config: crate::config::HotReloadConfig) -> Self {
self.config_builder = self.config_builder.with_hot_reload_config(config);
self
}
#[cfg(feature = "backup")]
#[must_use]
pub fn with_external_config(mut self, config: crate::backup::ExternalConfig) -> Self {
self.config_builder = self.config_builder.with_external_config(config);
self
}
#[must_use]
pub fn with_schema<NewSchema: SettingsSchema>(self) -> SettingsManagerBuilder<S, NewSchema> {
SettingsManagerBuilder {
config_builder: self.config_builder.with_schema::<NewSchema>(),
sub_settings: self.sub_settings,
}
}
#[must_use]
pub fn with_storage<NewS: StorageBackend + Default>(
self,
) -> SettingsManagerBuilder<NewS, Schema> {
SettingsManagerBuilder {
config_builder: self.config_builder.with_storage::<NewS>(),
sub_settings: self.sub_settings,
}
}
#[cfg(feature = "profiles")]
#[must_use]
pub fn with_profiles(mut self) -> Self {
self.config_builder = self.config_builder.with_profiles();
self
}
#[must_use]
pub fn with_sub_settings(mut self, config: SubSettingsConfig) -> Self {
self.sub_settings.push(config);
self
}
pub fn build(self) -> Result<SettingsManager<S, Schema>>
where
S: Default + 'static,
{
let config = self.config_builder.build();
let manager = SettingsManager::new(config)?;
for sub_config in self.sub_settings {
manager.register_sub_settings(sub_config)?;
}
Ok(manager)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_builder_with_credentials_config() {
let manager = SettingsManager::builder("my-app", "1.0.0")
.with_config_dir("/tmp/my-app")
.with_credential_config(crate::config::CredentialConfig::Default)
.build()
.unwrap();
#[cfg(any(feature = "keychain", feature = "encrypted-file"))]
{
assert!(manager.credentials.is_some());
}
#[cfg(not(any(feature = "keychain", feature = "encrypted-file")))]
{
let _ = manager;
}
}
}