Crates.io | rusty-store |
lib.rs | rusty-store |
version | 0.2.1 |
source | src |
created_at | 2024-08-26 14:12:01.632343 |
updated_at | 2024-09-07 12:14:46.813147 |
description | A Rust library for managing and storing serialized data using RON (Rusty Object Notation). It provides utilities for handling various types of stores, managing their persistence, and offering abstractions for modifying and committing data. |
homepage | |
repository | https://github.com/mazynoah/RustyStore |
max_upload_size | |
id | 1352288 |
size | 70,561 |
RustyStore is a Rust library for managing and storing serialized data using RON (Rusty Object Notation). It includes functionality for handling various types of stores and managing their persistence.
The library offers a set of utilities for reading, writing, and managing serialized data with RON. The primary components are:
Storage
: Manages file system paths for cache, data, and configuration storage.StoreHandle
: Represents a handle to a specific store, allowing access and modification of the data.StoreManager
: Provides an abstraction for managing and modifying store data, including options for committing or deferring changes.Store
: A store is any kind of struct which implements the Storing
trait. [dependencies]
rusty-store = "0.2.1"
examples/minimal.rs
The recommended minimal setup
use rusty_store::{StoreManager, Storage, Storing};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Default, Storing)]
pub struct MyStore {
pub count: u32,
}
pub trait MyStoreTrait {
fn increment_count(&mut self) -> Result<(), rusty_store::StoreError>;
}
impl MyStoreTrait for StoreManager<MyStore> {
fn increment_count(&mut self) -> Result<(), rusty_store::StoreError> {
self.modify_store(|store| store.count += 1)
}
}
fn main() {
// Initialize the Storage and create a new manager
let mut counter: StoreManager<MyStore> = Storage::new("com.github.mazynoah.storage")
.new_manager("manager")
.expect("Failed to create StoreManager");
counter
.increment_count()
.expect("Could not increment count");
println!("Count: {}", counter.get_store().count);
}
examples/handle.rs
Demonstrates basic usage of StoreHandle
to read, modify, and write data to storage.
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;
}
}
fn main() {
// Initialize the Storage with the defaults
let storage = Storage::new("com.github.mazynoah.storage");
// Create a handle for managing the store data.
let mut handle = StoreHandle::<MyStore>::new("handle");
// 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);
}