Skip to main content

ProfileStore

Struct ProfileStore 

Source
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

Source

pub fn new(dir: impl Into<PathBuf>) -> Self

Create a profile store rooted at the given directory.

Source

pub fn resolve(explicit: Option<PathBuf>) -> Result<Self, ProfileError>

Resolve the profile directory.

Resolution order:

  1. explicit path, if provided
  2. CS_CONFIG_PATH environment variable, if set
  3. ~/.cipherstash (the default)
Source

pub fn dir(&self) -> &Path

Return the directory path.

Source

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.

Source

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.

Source

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.

Source

pub fn clear(&self, filename: &str) -> Result<(), ProfileError>

Remove a file from the store directory.

Does nothing if the file does not already exist.

Source

pub fn exists(&self, filename: &str) -> bool

Check whether a file exists in the store directory.

Source

pub fn save_profile<T: ProfileData>( &self, value: &T, ) -> Result<(), ProfileError>

Save a ProfileData value using its declared filename and mode.

Source

pub fn load_profile<T: ProfileData>(&self) -> Result<T, ProfileError>

Load a ProfileData value from its declared filename.

Source

pub fn clear_profile<T: ProfileData>(&self) -> Result<(), ProfileError>

Remove the file for a ProfileData type.

Source

pub fn exists_profile<T: ProfileData>(&self) -> bool

Check whether the file for a ProfileData type exists.

Trait Implementations§

Source§

impl Default for ProfileStore

Returns a profile store at ~/.cipherstash.

§Panics

Panics if the home directory cannot be determined.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.