pub struct ProfileStore { /* private fields */ }Expand description
A directory-scoped JSON file store for profile data.
ProfileStore represents a profile directory (typically ~/.cipherstash/).
Individual files are addressed by name when calling save,
load, and other operations.
§Example
use stack_profile::ProfileStore;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct MyConfig {
name: String,
}
let store = ProfileStore::resolve(None)?;
store.save("my-config.json", &MyConfig { name: "example".into() })?;
let config: MyConfig = store.load("my-config.json")?;Implementations§
Source§impl ProfileStore
impl ProfileStore
Sourcepub fn new(dir: impl Into<PathBuf>) -> Self
pub fn new(dir: impl Into<PathBuf>) -> Self
Create a profile store rooted at the given directory.
Sourcepub fn resolve(explicit: Option<PathBuf>) -> Result<Self, ProfileError>
pub fn resolve(explicit: Option<PathBuf>) -> Result<Self, ProfileError>
Resolve the profile directory.
Resolution order:
explicitpath, if providedCS_CONFIG_PATHenvironment variable, if set~/.cipherstash(the default)
Sourcepub fn save<T: Serialize>(
&self,
filename: &str,
value: &T,
) -> Result<(), ProfileError>
pub fn save<T: Serialize>( &self, filename: &str, value: &T, ) -> Result<(), ProfileError>
Save a value as pretty-printed JSON to a file in the store directory.
Creates the directory and any parents if they don’t exist.
Sourcepub fn save_with_mode<T: Serialize>(
&self,
filename: &str,
value: &T,
_mode: u32,
) -> Result<(), ProfileError>
pub fn save_with_mode<T: Serialize>( &self, filename: &str, value: &T, _mode: u32, ) -> Result<(), ProfileError>
Save a value as pretty-printed JSON with restricted Unix file permissions.
On non-Unix platforms the mode is ignored and this behaves like save.
Sourcepub fn set_current_workspace(
&self,
workspace_id: &str,
) -> Result<(), ProfileError>
pub fn set_current_workspace( &self, workspace_id: &str, ) -> Result<(), ProfileError>
Set the current workspace.
Writes the workspace ID to the current_workspace file in the profile
directory. The workspace must already have a directory under workspaces/
(created during login). Use init_workspace to
create a new workspace directory.
Returns ProfileError::WorkspaceNotFound if the workspace directory
does not exist.
Sourcepub fn init_workspace(&self, workspace_id: &str) -> Result<(), ProfileError>
pub fn init_workspace(&self, workspace_id: &str) -> Result<(), ProfileError>
Create a workspace directory and set it as the current workspace.
Unlike set_current_workspace, this
creates the workspace directory if it does not exist. Used during login
to initialize a new workspace.
Sourcepub fn current_workspace(&self) -> Result<String, ProfileError>
pub fn current_workspace(&self) -> Result<String, ProfileError>
Return the current workspace ID.
Returns ProfileError::NoCurrentWorkspace if no workspace has been set.
Sourcepub fn clear_current_workspace(&self) -> Result<(), ProfileError>
pub fn clear_current_workspace(&self) -> Result<(), ProfileError>
Remove the current workspace selection.
Sourcepub fn list_workspaces(&self) -> Result<Vec<String>, ProfileError>
pub fn list_workspaces(&self) -> Result<Vec<String>, ProfileError>
List workspace IDs that have profile data on disk.
Returns a sorted list of workspace IDs that have subdirectories in
the workspaces/ directory.
Sourcepub fn workspace_store(
&self,
workspace_id: &str,
) -> Result<ProfileStore, ProfileError>
pub fn workspace_store( &self, workspace_id: &str, ) -> Result<ProfileStore, ProfileError>
Return a ProfileStore scoped to a specific workspace directory.
The returned store is rooted at workspaces/<workspace_id>/ within this
store’s directory. All save/load/save_profile/load_profile calls
on the returned store operate inside that workspace directory.
Sourcepub fn current_workspace_store(&self) -> Result<ProfileStore, ProfileError>
pub fn current_workspace_store(&self) -> Result<ProfileStore, ProfileError>
Return a ProfileStore scoped to the current workspace.
Shortcut for store.workspace_store(&store.current_workspace()?).
Returns ProfileError::NoCurrentWorkspace if no workspace has been set.
Sourcepub fn migrate_to_workspace(
&self,
workspace_id: &str,
) -> Result<(), ProfileError>
pub fn migrate_to_workspace( &self, workspace_id: &str, ) -> Result<(), ProfileError>
Move legacy flat-file profiles into a workspace directory.
Moves auth.json and secretkey.json from the profile root into
workspaces/<workspace_id>/ and sets workspace_id as the current
workspace. Files that already exist in the target are not overwritten.
Missing source files are silently skipped.
Sourcepub fn load<T: DeserializeOwned>(
&self,
filename: &str,
) -> Result<T, ProfileError>
pub fn load<T: DeserializeOwned>( &self, filename: &str, ) -> Result<T, ProfileError>
Load a value from a JSON file in the store directory.
Returns ProfileError::NotFound if the file does not exist.
Sourcepub fn clear(&self, filename: &str) -> Result<(), ProfileError>
pub fn clear(&self, filename: &str) -> Result<(), ProfileError>
Remove a file from the store directory.
Does nothing if the file does not already exist.
Sourcepub fn exists(&self, filename: &str) -> bool
pub fn exists(&self, filename: &str) -> bool
Check whether a file exists in the store directory.
Sourcepub fn save_profile<T: ProfileData>(
&self,
value: &T,
) -> Result<(), ProfileError>
pub fn save_profile<T: ProfileData>( &self, value: &T, ) -> Result<(), ProfileError>
Save a ProfileData value using its declared filename and mode.
Sourcepub fn load_profile<T: ProfileData>(&self) -> Result<T, ProfileError>
pub fn load_profile<T: ProfileData>(&self) -> Result<T, ProfileError>
Load a ProfileData value from its declared filename.
Sourcepub fn clear_profile<T: ProfileData>(&self) -> Result<(), ProfileError>
pub fn clear_profile<T: ProfileData>(&self) -> Result<(), ProfileError>
Remove the file for a ProfileData type.
Sourcepub fn exists_profile<T: ProfileData>(&self) -> bool
pub fn exists_profile<T: ProfileData>(&self) -> bool
Check whether the file for a ProfileData type exists.