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
impl Storage
Sourcepub fn config_dir(&self) -> &Path
pub fn config_dir(&self) -> &Path
Get the config directory
Sourcepub fn new(app_id: &str) -> Self
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?
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
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}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}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}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}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}Sourcepub fn from_dirs(
cache_dir: PathBuf,
data_dir: PathBuf,
config_dir: PathBuf,
) -> Self
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- APathBufrepresenting the cache directory path.data_dir- APathBufrepresenting the data directory path.config_dir- APathBufrepresenting 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);Sourcepub fn new_manager<T: Storing>(
&self,
store_id: &str,
) -> Result<StoreManager<T>, StoreError>
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?
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
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}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}Sourcepub fn new_handle<T: Storing>(&self, store_id: &str) -> StoreHandle<T>
pub fn new_handle<T: Storing>(&self, store_id: &str) -> StoreHandle<T>
Returns a new Handle of type T with the given store_id
Sourcepub fn read<T: Storing>(
&self,
handle: &mut StoreHandle<T>,
) -> Result<(), StoreError>
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?
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}Sourcepub fn write<T: Storing>(
&self,
handle: &mut StoreHandle<T>,
) -> Result<(), StoreError>
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?
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}