pub struct ProfileManager<S: StorageBackend = JsonStorage> { /* private fields */ }Expand description
Manages profiles for a specific target (settings or sub-settings)
The ProfileManager handles:
- Creating, deleting, renaming, and duplicating profiles
- Switching the active profile
- Persisting the profile manifest
- Emitting events on profile changes
Implementations§
Source§impl<S: StorageBackend> ProfileManager<S>
impl<S: StorageBackend> ProfileManager<S>
Sourcepub fn new(base_dir: &Path, target_name: impl Into<String>, storage: S) -> Self
pub fn new(base_dir: &Path, target_name: impl Into<String>, storage: S) -> Self
Create a new profile manager for a given base directory
§Arguments
base_dir- The directory containing the profilestarget_name- Name of this profile target (e.g., “remotes”, “settings”)storage- Storage backend to use for manifest
Sourcepub fn initialize(
config_dir: &Path,
target_name: &str,
storage: S,
enabled: bool,
migrator: &ProfileMigrator,
) -> Result<(PathBuf, Option<Self>)>
pub fn initialize( config_dir: &Path, target_name: &str, storage: S, enabled: bool, migrator: &ProfileMigrator, ) -> Result<(PathBuf, Option<Self>)>
Initialize the profile manager, running migrations if enabled
This is a helper to centralize initialization logic that was previously in SettingsManager.
§Arguments
config_dir- The root configuration directorytarget_name- The name of the target (e.g. “settings”)storage- Storage backendenabled- Whether profiles are enabledmigrator- Migration strategy
§Returns
Returns a tuple of (active_settings_dir, Option<ProfileManager>).
If profiles are disabled, returns (config_dir, None).
§Errors
Returns an error if:
- Migration fails
- Profile manager initialization fails
- Active profile path cannot be resolved
Sourcepub fn set_on_event<F>(&self, callback: F)
pub fn set_on_event<F>(&self, callback: F)
Sourcepub fn set_on_invalidate<F>(&self, callback: F)
pub fn set_on_invalidate<F>(&self, callback: F)
Sourcepub fn invalidate_manifest(&self)
pub fn invalidate_manifest(&self)
Invalidate the internal manifest cache
This forces the manifest to be re-read from disk on the next access.
§Panics
Panics if the internal lock is poisoned.
Sourcepub fn profile_path(&self, name: &str) -> PathBuf
pub fn profile_path(&self, name: &str) -> PathBuf
Get the path to a specific profile’s directory
Sourcepub fn active_path(&self) -> Result<PathBuf>
pub fn active_path(&self) -> Result<PathBuf>
Sourcepub fn active(&self) -> Result<String>
pub fn active(&self) -> Result<String>
Get the currently active profile name
§Returns
Returns the name of the currently active profile.
§Errors
Returns an error if the manifest cannot be read.
§Panics
This function will not panic under normal circumstances. The .unwrap() call
is safe because ensure_manifest() is called first, which guarantees the manifest
is populated.
Sourcepub fn list(&self) -> Result<Vec<String>>
pub fn list(&self) -> Result<Vec<String>>
List all profile names
§Returns
Returns a vector of profile names.
§Errors
Returns an error if the manifest cannot be read.
§Panics
This function will not panic under normal circumstances. The .unwrap() call
is safe because ensure_manifest() is called first, which guarantees the manifest
is populated.
Sourcepub fn exists(&self, name: &str) -> Result<bool>
pub fn exists(&self, name: &str) -> Result<bool>
Check if a profile exists
§Arguments
name- The name of the profile to check
§Returns
Returns true if the profile exists, false otherwise.
§Errors
Returns an error if the manifest cannot be read.
§Panics
This function will not panic under normal circumstances. The .unwrap() call
is safe because ensure_manifest() is called first, which guarantees the manifest
is populated.
Sourcepub fn create(&self, name: &str) -> Result<()>
pub fn create(&self, name: &str) -> Result<()>
Create a new profile
Creates an empty profile directory and updates the manifest.
§Arguments
name- The name of the profile to create
§Errors
Returns an error if the profile cannot be created.
§Panics
This function will not panic under normal circumstances. The .unwrap() calls
are safe because ensure_manifest() is called first, which guarantees the manifest
is populated.
Sourcepub fn switch(&self, name: &str) -> Result<()>
pub fn switch(&self, name: &str) -> Result<()>
Switch to a different profile
Updates the active profile in the manifest and invalidates caches.
§Arguments
name- The name of the profile to switch to
§Errors
Returns an error if the profile cannot be switched.
§Panics
This function will not panic under normal circumstances. The .unwrap() calls
are safe because ensure_manifest() is called first, which guarantees the manifest
is populated.
Sourcepub fn delete(&self, name: &str) -> Result<()>
pub fn delete(&self, name: &str) -> Result<()>
Delete a profile
Removes the profile directory and updates the manifest. Cannot delete the active profile or the last remaining profile.
§Arguments
name- The name of the profile to delete
§Errors
Returns an error if the profile cannot be deleted.
§Panics
This function will not panic under normal circumstances. The .unwrap() calls
are safe because ensure_manifest() is called first, which guarantees the manifest
is populated.
Sourcepub fn rename(&self, from: &str, to: &str) -> Result<()>
pub fn rename(&self, from: &str, to: &str) -> Result<()>
Rename a profile
§Arguments
from- The name of the profile to renameto- The new name for the profile
§Errors
Returns an error if the profile cannot be renamed.
§Panics
This function will not panic under normal circumstances. The .unwrap() calls
are safe because ensure_manifest() is called first, which guarantees the manifest
is populated.
Sourcepub fn duplicate(&self, source: &str, target: &str) -> Result<()>
pub fn duplicate(&self, source: &str, target: &str) -> Result<()>
Duplicate a profile
Copies all contents from the source profile to a new profile.
§Arguments
source- The name of the source profiletarget- The name of the target profile
§Errors
Returns an error if the profile cannot be duplicated.
§Panics
This function will not panic under normal circumstances. The .unwrap() calls
are safe because ensure_manifest() is called first, which guarantees the manifest
is populated.
Sourcepub fn rollback_to_flat(&self) -> Result<()>
pub fn rollback_to_flat(&self) -> Result<()>
Rollback to flat structure (removes all profiles except active)
§⚠️ Warning
This operation is irreversible and will:
- Keep only the active profile’s data
- Permanently delete all other profiles
- Remove the profile structure entirely
§Example
use rcman::{SettingsManager, SubSettingsConfig};
let manager = SettingsManager::builder("my-app", "1.0.0")
.with_sub_settings(SubSettingsConfig::new("remotes").with_profiles())
.build()?;
let remotes = manager.sub_settings("remotes")?;
// Remove profile support, keeping only active profile data
remotes.profiles()?.rollback_to_flat()?;§Errors
Returns an error if the rollback fails
Sourcepub fn initialize_with_migration<F>(&self, detect_existing: F) -> Result<bool>
pub fn initialize_with_migration<F>(&self, detect_existing: F) -> Result<bool>
Initialize profiles with auto-migration from flat structure
If files exist in the base directory but no manifest exists, moves them into a “default” profile.
§Arguments
detect_existing- A function that returnstrueif there are existing files to migrate
§Returns
Returns true if migration was needed, false otherwise.
§Errors
Returns an error if the manifest cannot be read or saved.
§Panics
Panics if the internal lock is poisoned.
Sourcepub fn complete_migration(&self) -> Result<()>
pub fn complete_migration(&self) -> Result<()>
Mark migration as complete and save manifest
§Errors
Returns an error if the manifest cannot be saved.
Sourcepub fn manifest(&self) -> Result<ProfileManifest>
pub fn manifest(&self) -> Result<ProfileManifest>
Sourcepub fn profiles_dir(&self) -> &Path
pub fn profiles_dir(&self) -> &Path
Get the profiles directory path