Storage

Struct Storage 

Source
pub struct Storage { /* private fields */ }
Expand description

Handles file system paths for reading from and writing to data storage.

The Storage struct provides a way to manage file paths used for storing data in different locations. It simplifies the process of accessing and modifying data by providing methods for these operations.

§Example

use rusty_store::{Storage, StoreHandle, Storing};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Default, Storing)]
pub struct MyStore {
    pub count: u32,
}

impl MyStore {
    fn increment_count(&mut self) {
        self.count += 1;
    }
}


// Initialize the Storage with the defaults
let storage = Storage::new("APP_ID");

// Create a handle for managing the store data.
let mut handle = StoreHandle::<MyStore>::new("my_store_id");

// Read existing store from storage
storage
    .read(&mut handle)
    .expect("Failed to read from storage");

// Modify the store data
let counter = handle.get_store_mut();

counter.increment_count();
counter.increment_count();
counter.increment_count();

// Write changes to disk
storage
    .write(&mut handle)
    .expect("Failed to write to storage");

let counter = handle.get_store();

println!("Count: {}", counter.count);

Implementations§

Source§

impl Storage

Source

pub fn cache_dir(&self) -> &Path

Get the cache directory

Source

pub fn data_dir(&self) -> &Path

Get the data directory

Source

pub fn config_dir(&self) -> &Path

Get the config directory

Source

pub fn new(app_id: &str) -> Self

Creates a new Storage instance by obtaining the paths for cache, data, and configuration directories.

§Panics
  • Panics if the cache directory, data directory, or configuration directory path cannot be determined.
Examples found in repository?
examples/minimal/main.rs (line 21)
19fn main() {
20    // Initialize the Storage and create a new manager
21    let mut counter: StoreManager<MyStore> = Storage::new("com.github.mazynoah.storage")
22        .new_manager("manager")
23        .expect("Failed to create StoreManager");
24
25    counter
26        .increment_count()
27        .expect("Could not increment count");
28
29    println!("Count: {}", counter.get_store().count);
30}
More examples
Hide additional examples
examples/manager_trait/main.rs (line 21)
19fn main() {
20    // Initialize the Storage with the defaults
21    let storage = Storage::new("com.github.mazynoah.storage");
22
23    // Use `StoreManager` to manage the store.
24    let mut manager =
25        StoreManager::new(&storage, "manager_trait").expect("Failed to create StoreManager");
26
27    // Modify and save the data using `StoreManager`.
28    manager
29        .increment_count()
30        .expect("Failed to increment count");
31
32    let counter = manager.get_store();
33
34    println!("Count: {}", counter.count);
35}
examples/manager_uncommitted/main.rs (line 21)
19fn main() {
20    // Initialize the Storage with the defaults
21    let storage = Storage::new("com.github.mazynoah.storage");
22
23    // Use `StoreManager` to manage the store.
24    let mut manager =
25        StoreManager::new(&storage, "manager_uncommitted").expect("Failed to create StoreManager");
26
27    // Modify the data without saving the changes to disk.
28    manager.increment_count();
29    manager.increment_count();
30    manager.increment_count();
31
32    // Save the data to the storage
33    manager.save().expect("Failed to save count to storage");
34
35    let counter = manager.get_store();
36
37    println!("Count: {}", counter.count);
38}
examples/handle/main.rs (line 17)
15fn main() {
16    // Initialize the Storage with the defaults
17    let storage = Storage::new("com.github.mazynoah.storage");
18
19    // Create a handle for managing the store data.
20    let mut handle = StoreHandle::<MyStore>::new("handle");
21
22    // Read existing store from storage
23    storage
24        .read(&mut handle)
25        .expect("Failed to read from storage");
26
27    // Modify the store data
28    let counter = handle.get_store_mut();
29
30    counter.increment_count();
31    counter.increment_count();
32    counter.increment_count();
33
34    // Write changes to disk
35    storage
36        .write(&mut handle)
37        .expect("Failed to write to storage");
38
39    let counter = handle.get_store();
40
41    println!("Count: {}", counter.count);
42}
examples/manager/main.rs (line 17)
15fn main() {
16    // Initialize the Storage with the defaults
17    let storage = Storage::new("com.github.mazynoah.storage");
18
19    // Create a StoreManager for managing the store data
20    let mut counter_manager = storage
21        .new_manager::<MyStore>("manager")
22        .expect("Failed to create StoreManager");
23
24    // Alternatively:
25    let mut counter_manager =
26        StoreManager::<MyStore>::new(&storage, "manager").expect("Failed to create StoreManager");
27
28    // Get a mutable reference to the store
29    let counter = counter_manager.get_store_mut();
30
31    counter.count = 75;
32    counter.increment_count();
33
34    // Save the data to the storage
35    counter_manager
36        .save()
37        .expect("Failed to save count to storage");
38
39    let counter = counter_manager.get_store();
40
41    println!("Count: {}", counter.count);
42}
examples/custom_location/main.rs (line 23)
21fn main() {
22    // Initialize the Storage with the defaults
23    let storage = Storage::new("com.github.mazynoah.storage");
24
25    // Create a StoreManager for managing the store data
26    let mut counter_manager = storage
27        .new_manager::<MyStore>("custom_location")
28        .expect("Failed to create StoreManager");
29
30    // Alternatively:
31    let mut counter_manager = StoreManager::<MyStore>::new(&storage, "custom_location")
32        .expect("Failed to create StoreManager");
33
34    // Get a mutable reference to the store
35    let counter = counter_manager.get_store_mut();
36
37    counter.count = 75;
38    counter.increment_count();
39
40    // Save the data to the storage
41    counter_manager
42        .save()
43        .expect("Failed to save count to storage");
44
45    let counter = counter_manager.get_store();
46
47    println!("Count: {}", counter.count);
48}
Source

pub fn from_dirs( cache_dir: PathBuf, data_dir: PathBuf, config_dir: PathBuf, ) -> Self

Creates a new Storage instance with specific cache, data, and config paths.

§Arguments
  • cache_dir - A PathBuf representing the cache directory path.
  • data_dir - A PathBuf representing the data directory path.
  • config_dir - A PathBuf representing the configuration directory path.
§Returns

A new Storage instance with the specified cache, data, and config paths.

§Example
use std::path::PathBuf;
use rusty_store::Storage;

let cache_dir = PathBuf::from("/path/to/cache");
let data_dir = PathBuf::from("/path/to/data");
let config_dir = PathBuf::from("/path/to/config");

let storage = Storage::from_dirs(cache_dir, data_dir, config_dir);
Source

pub fn new_manager<T: Storing>( &self, store_id: &str, ) -> Result<StoreManager<T>, StoreError>

Returns a new StoreManager of type T with the given store_id

Examples found in repository?
examples/minimal/main.rs (line 22)
19fn main() {
20    // Initialize the Storage and create a new manager
21    let mut counter: StoreManager<MyStore> = Storage::new("com.github.mazynoah.storage")
22        .new_manager("manager")
23        .expect("Failed to create StoreManager");
24
25    counter
26        .increment_count()
27        .expect("Could not increment count");
28
29    println!("Count: {}", counter.get_store().count);
30}
More examples
Hide additional examples
examples/manager/main.rs (line 21)
15fn main() {
16    // Initialize the Storage with the defaults
17    let storage = Storage::new("com.github.mazynoah.storage");
18
19    // Create a StoreManager for managing the store data
20    let mut counter_manager = storage
21        .new_manager::<MyStore>("manager")
22        .expect("Failed to create StoreManager");
23
24    // Alternatively:
25    let mut counter_manager =
26        StoreManager::<MyStore>::new(&storage, "manager").expect("Failed to create StoreManager");
27
28    // Get a mutable reference to the store
29    let counter = counter_manager.get_store_mut();
30
31    counter.count = 75;
32    counter.increment_count();
33
34    // Save the data to the storage
35    counter_manager
36        .save()
37        .expect("Failed to save count to storage");
38
39    let counter = counter_manager.get_store();
40
41    println!("Count: {}", counter.count);
42}
examples/custom_location/main.rs (line 27)
21fn main() {
22    // Initialize the Storage with the defaults
23    let storage = Storage::new("com.github.mazynoah.storage");
24
25    // Create a StoreManager for managing the store data
26    let mut counter_manager = storage
27        .new_manager::<MyStore>("custom_location")
28        .expect("Failed to create StoreManager");
29
30    // Alternatively:
31    let mut counter_manager = StoreManager::<MyStore>::new(&storage, "custom_location")
32        .expect("Failed to create StoreManager");
33
34    // Get a mutable reference to the store
35    let counter = counter_manager.get_store_mut();
36
37    counter.count = 75;
38    counter.increment_count();
39
40    // Save the data to the storage
41    counter_manager
42        .save()
43        .expect("Failed to save count to storage");
44
45    let counter = counter_manager.get_store();
46
47    println!("Count: {}", counter.count);
48}
Source

pub fn new_handle<T: Storing>(&self, store_id: &str) -> StoreHandle<T>

Returns a new Handle of type T with the given store_id

Source

pub fn read<T: Storing>( &self, handle: &mut StoreHandle<T>, ) -> Result<(), StoreError>

Reads the store from a file and updates the provided StoreHandle. If the file does not exist, it creates a default store if a default is available.

§Example
use rusty_store::{Storage, StoreHandle, Storing};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Default, Storing)]
pub struct MyStore {
    pub count: u32,
}

impl MyStore {
    fn increment_count(&mut self) {
        self.count += 1;
    }
}

let storage = Storage::new("APP_ID");
let mut handle: StoreHandle<MyStore> = StoreHandle::new("my_store_id");

storage.read(&mut handle).expect("Failed to read store");
Examples found in repository?
examples/handle/main.rs (line 24)
15fn main() {
16    // Initialize the Storage with the defaults
17    let storage = Storage::new("com.github.mazynoah.storage");
18
19    // Create a handle for managing the store data.
20    let mut handle = StoreHandle::<MyStore>::new("handle");
21
22    // Read existing store from storage
23    storage
24        .read(&mut handle)
25        .expect("Failed to read from storage");
26
27    // Modify the store data
28    let counter = handle.get_store_mut();
29
30    counter.increment_count();
31    counter.increment_count();
32    counter.increment_count();
33
34    // Write changes to disk
35    storage
36        .write(&mut handle)
37        .expect("Failed to write to storage");
38
39    let counter = handle.get_store();
40
41    println!("Count: {}", counter.count);
42}
Source

pub fn write<T: Storing>( &self, handle: &mut StoreHandle<T>, ) -> Result<(), StoreError>

Writes the current store T from the provided StoreHandle to a file. If the file does not exist, it creates a default store if a default is available.

§Example
use rusty_store::{Storage, StoreHandle, Storing};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Default, Storing)]
pub struct MyStore {
    pub count: u32,
}

impl MyStore {
    fn increment_count(&mut self) {
        self.count += 1;
    }
}

let storage = Storage::new("APP_ID");
let mut handle: StoreHandle<MyStore> = StoreHandle::new("my_store_id");

storage.write(&mut handle).expect("Failed to read store");
Examples found in repository?
examples/handle/main.rs (line 36)
15fn main() {
16    // Initialize the Storage with the defaults
17    let storage = Storage::new("com.github.mazynoah.storage");
18
19    // Create a handle for managing the store data.
20    let mut handle = StoreHandle::<MyStore>::new("handle");
21
22    // Read existing store from storage
23    storage
24        .read(&mut handle)
25        .expect("Failed to read from storage");
26
27    // Modify the store data
28    let counter = handle.get_store_mut();
29
30    counter.increment_count();
31    counter.increment_count();
32    counter.increment_count();
33
34    // Write changes to disk
35    storage
36        .write(&mut handle)
37        .expect("Failed to write to storage");
38
39    let counter = handle.get_store();
40
41    println!("Count: {}", counter.count);
42}

Trait Implementations§

Source§

impl Clone for Storage

Source§

fn clone(&self) -> Storage

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Storage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Storage

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Storage

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,