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 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.