pub struct SettingsConfigBuilder<S: StorageBackend = JsonStorage, Schema: SettingsSchema = ()> { /* private fields */ }Expand description
Core configuration types and traits for settings management.
Builder for creating SettingsConfig with a fluent API.
This is the recommended way to create a settings manager.
§Type Parameters
Schema: Settings schema type (defaults to()for dynamic usage)
§Examples
Type-Safe (With Schema):
use rcman::{SettingsConfig, SettingsSchema, SettingMetadata, settings};
use serde::{Serialize, Deserialize};
use std::collections::HashMap;
#[derive(Default, Serialize, Deserialize)]
struct MySettings { theme: String }
impl SettingsSchema for MySettings {
fn get_metadata() -> HashMap<String, SettingMetadata> {
settings! { "ui.theme" => SettingMetadata::text("dark").meta_str("label", "Theme") }
}
}
let config = SettingsConfig::builder("my-app", "1.0.0")
.with_schema::<MySettings>()
.with_config_dir("~/.config/my-app")
.build();Dynamic (Without Schema):
use rcman::SettingsConfig;
let config = SettingsConfig::builder("my-app", "1.0.0")
.with_config_dir("~/.config/my-app")
.build();Implementations§
Source§impl<S: StorageBackend, Schema: SettingsSchema> SettingsConfigBuilder<S, Schema>
impl<S: StorageBackend, Schema: SettingsSchema> SettingsConfigBuilder<S, Schema>
Sourcepub fn with_pretty_json(self, pretty: bool) -> Self
pub fn with_pretty_json(self, pretty: bool) -> Self
Use compact JSON (no pretty printing)
Note: This method is only available when using JsonStorage.
§Example
use rcman::SettingsConfig;
let config = SettingsConfig::builder("my-app", "1.0.0")
.build();Sourcepub fn with_config_dir(self, path: impl Into<PathBuf>) -> Self
pub fn with_config_dir(self, path: impl Into<PathBuf>) -> Self
Set the configuration directory
Supports ~ expansion for home directory.
Sourcepub fn settings_file(self, filename: impl Into<String>) -> Self
pub fn settings_file(self, filename: impl Into<String>) -> Self
Set the settings filename (default: “settings.{ext}”)
Sourcepub fn with_credentials(self) -> Self
pub fn with_credentials(self) -> Self
When enabled, settings marked as secret: true in metadata
will be stored in the OS keychain instead of the settings file.
Sourcepub fn with_external_config(self, config: ExternalConfig) -> Self
pub fn with_external_config(self, config: ExternalConfig) -> Self
Register an external configuration file for backup
External configs are files managed outside of rcman (like rclone.conf) that can be included in backups.
§Example
use rcman::SettingsConfig;
use rcman::backup::ExternalConfig;
let config = SettingsConfig::builder("my-app", "1.0.0")
.with_external_config(ExternalConfig::new("rclone", "/path/to/rclone.conf")
.display_name("Rclone Configuration"))
.build();Sourcepub fn with_env_prefix(self, prefix: impl Into<String>) -> Self
pub fn with_env_prefix(self, prefix: impl Into<String>) -> Self
Enable environment variable overrides
When set, settings can be overridden by environment variables.
The format is: {PREFIX}_{CATEGORY}_{KEY} (all uppercase, dots become underscores)
§Example
use rcman::SettingsConfig;
let config = SettingsConfig::builder("my-app", "1.0.0")
.with_env_prefix("MYAPP")
.build();
// Now MYAPP_UI_THEME=dark will override the "ui.theme" settingSourcepub fn env_overrides_secrets(self, allow: bool) -> Self
pub fn env_overrides_secrets(self, allow: bool) -> Self
Allow environment variables to override secret settings
By default, secrets stored in the OS keychain are NOT affected by env vars. Enable this for Docker/CI environments where secrets are passed via env.
§Example
use rcman::SettingsConfig;
let config = SettingsConfig::builder("my-app", "1.0.0")
.with_env_prefix("MYAPP")
.env_overrides_secrets(true) // MYAPP_API_KEY will override keychain
.build();Sourcepub fn with_env_source(self, source: Arc<dyn EnvSource>) -> Self
pub fn with_env_source(self, source: Arc<dyn EnvSource>) -> Self
Set a custom environment variable source
Useful for testing or injecting env vars procedurally.
Sourcepub fn with_migrator<F>(self, migrator: F) -> Self
pub fn with_migrator<F>(self, migrator: F) -> Self
Set a migration function for schema changes (lazy migration)
The migrator function is called automatically when loading settings. If the function modifies the value, the migrated version is saved back.
Use this to upgrade old data formats to new ones transparently.
§Example
use rcman::SettingsConfig;
use serde_json::json;
let config = SettingsConfig::builder("my-app", "1.0.0")
.with_migrator(|mut value| {
// Migrate v1 to v2: rename "color" to "theme"
if let Some(obj) = value.as_object_mut() {
if let Some(ui) = obj.get_mut("ui").and_then(|v| v.as_object_mut()) {
if let Some(color) = ui.remove("color") {
ui.insert("theme".to_string(), color);
}
}
}
value
})
.build();Sourcepub fn with_profiles(self) -> Self
pub fn with_profiles(self) -> Self
Enable profiles for main settings
When enabled, the main settings file is stored per-profile, allowing completely different configurations (e.g., “work” vs “personal”).
§Example
use rcman::SettingsManager;
let manager = SettingsManager::builder("my-app", "1.0.0")
.with_profiles() // Enable profiles for main settings
.build()?;
// Now you can switch profiles
manager.switch_profile("work")?;Sourcepub fn with_schema<NewSchema: SettingsSchema>(
self,
) -> SettingsConfigBuilder<S, NewSchema>
pub fn with_schema<NewSchema: SettingsSchema>( self, ) -> SettingsConfigBuilder<S, NewSchema>
Specify the schema type for compile-time type safety.
This binds your settings struct to the manager, enabling:
- Type-safe
settings()method returning your struct - Compile-time validation of setting keys
- Better IDE autocomplete and refactoring support
§Example
use rcman::{SettingsConfig, SettingsSchema, SettingMetadata, settings};
use serde::{Serialize, Deserialize};
use std::collections::HashMap;
#[derive(Default, Serialize, Deserialize)]
struct AppSettings {
theme: String,
font_size: f64,
}
impl SettingsSchema for AppSettings {
fn get_metadata() -> HashMap<String, SettingMetadata> {
settings! {
"ui.theme" => SettingMetadata::text("dark").meta_str("label", "Theme"),
"ui.font_size" => SettingMetadata::number(14.0).meta_str("label", "Font Size")
}
}
}
let config = SettingsConfig::builder("my-app", "1.0.0")
.with_schema::<AppSettings>() // Bind the schema
.build();Sourcepub fn with_storage<NewS: StorageBackend + Default>(
self,
) -> SettingsConfigBuilder<NewS, Schema>
pub fn with_storage<NewS: StorageBackend + Default>( self, ) -> SettingsConfigBuilder<NewS, Schema>
Specify the storage backend type.
This transforms the builder to use the specified storage backend. The settings filename will automatically be updated to match the format.
§Example
use rcman::{SettingsConfig, JsonStorage};
let config = SettingsConfig::builder("my-app", "1.0.0")
.with_storage::<JsonStorage>()
.build();Sourcepub fn build(self) -> SettingsConfig<S, Schema>where
S: Default,
pub fn build(self) -> SettingsConfig<S, Schema>where
S: Default,
Build the SettingsConfig
If config_dir is not set, uses the system config directory for the app.
Trait Implementations§
Source§impl<S: Clone + StorageBackend, Schema: Clone + SettingsSchema> Clone for SettingsConfigBuilder<S, Schema>
impl<S: Clone + StorageBackend, Schema: Clone + SettingsSchema> Clone for SettingsConfigBuilder<S, Schema>
Source§fn clone(&self) -> SettingsConfigBuilder<S, Schema>
fn clone(&self) -> SettingsConfigBuilder<S, Schema>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more