| Crates.io | json_state |
| lib.rs | json_state |
| version | 0.1.0 |
| created_at | 2025-05-07 08:39:49.703416+00 |
| updated_at | 2025-05-07 08:39:49.703416+00 |
| description | A simple Rust library for managing states using json with file persistence. |
| homepage | |
| repository | https://github.com/bambby-plus/json_state.git |
| max_upload_size | |
| id | 1663635 |
| size | 34,129 |
A lightweight, flexible Rust module for managing application state persistence across memory and filesystem.
The JSON_STATE provides a simple and efficient way to store, retrieve, and manage application states. It enables seamless persistence of JSON-based state data both in memory and on the filesystem, making it ideal for applications that need to maintain state between sessions or require robust state handling.
Add the following to your Cargo.toml:
[dependencies]
state = "0.1.0"
serde_json = "1.0"
uuid = "1.0"
serde = { version = "1.0", features = ["derive"] }
Here's a simple example demonstrating how to use the State Management module:
use serde_json::{Value, json};
use state::{State, StateManager};
fn main() -> std::io::Result<()> {
// Create a StateManager with the directory path
let mut manager = StateManager::new("states".to_string());
// Create a new state with some payload
let payload = json!({ "name": "John", "age": 30 });
let state = State::new(payload);
let state_id = state.get_id().clone();
// Save the state to memory and file system
manager.save(state.clone())?;
println!("Saved state with ID: {}", state_id);
// Load the manager from the directory to verify saved state
let loaded_manager = StateManager::load_from_dir("states")?;
println!("Loaded states: {:?}", loaded_manager.states.keys());
// Load the specific state using the public load method
match manager.load(&state_id) {
Ok(Some(loaded_state)) => {
println!("Loaded state: {:?}", loaded_state);
}
Ok(None) => {
println!("State with ID {} not found.", state_id);
}
Err(e) => {
println!("Error loading state: {}", e);
}
}
// Delete the state from memory and file system
manager.delete(&state_id)?;
println!("Deleted state with ID: {}", state_id);
// Try loading the deleted state to verify deletion
match manager.load(&state_id) {
Ok(Some(loaded_state)) => {
println!("State still exists: {:?}", loaded_state);
}
Ok(None) => {
println!("State with ID {} has been deleted.", state_id);
}
Err(e) => {
println!("Error loading state: {}", e);
}
}
// Clear all states from a directory
StateManager::clear_dir("states")?;
println!("Cleared all states from directory");
Ok(())
}
A structure representing a single state item with a unique ID and JSON payload.
// Create a new state with a JSON payload
let state = State::new(json!({ "key": "value" }));
// Get the state's unique ID
let id = state.get_id();
// Get the state's payload
let payload = state.get_payload();
Manages multiple states, providing operations for saving, loading, and deleting states.
// Create a new StateManager with a storage directory
let mut manager = StateManager::new("states".to_string());
// Load an existing StateManager from a directory
let manager = StateManager::load_from_dir("states")?;
// Save a state (to memory and filesystem)
manager.save(state)?;
// Load a state by ID
let loaded_state = manager.load(&state_id)?;
// Delete a state by ID
manager.delete(&state_id)?;
// Clear all states from a directory
StateManager::clear_dir("states")?;
The module uses Rust's standard std::io::Result for error handling, making it straightforward to integrate with existing error handling approaches.
The library includes comprehensive tests that demonstrate proper usage and validate functionality:
// Run the tests
cargo test
Test isolation is achieved by using unique directories for each test case, preventing cross-test interference.
This project is licensed under the MIT License - see the LICENSE file for details.