pub struct StateManager {
pub dir: String,
pub states: HashMap<Uuid, State>,
}Expand description
Manages multiple state objects with persistence capabilities.
StateManager provides functionality to:
- Save states to both memory and filesystem
- Load states from memory or filesystem
- Delete states from both memory and filesystem
- Load all states from a directory
Fields§
§dir: StringDirectory path where state files are stored
states: HashMap<Uuid, State>In-memory storage of states indexed by their UUID
Implementations§
Source§impl StateManager
impl StateManager
Sourcepub fn new(dir: String) -> Self
pub fn new(dir: String) -> Self
Creates a new StateManager with the specified directory for persistence.
§Arguments
dir- The directory path where state files will be stored
§Returns
A new StateManager instance initialized with the specified directory
§Examples
use state::StateManager;
let manager = StateManager::new("states".to_string());Sourcepub fn save(&mut self, state: State) -> Result<()>
pub fn save(&mut self, state: State) -> Result<()>
Saves a state to both in-memory storage and the filesystem.
§Arguments
state- The state to save
§Returns
An io::Result indicating success or containing an error if the filesystem operation failed
§Examples
use serde_json::json;
use state::{State, StateManager};
let mut manager = StateManager::new("states".to_string());
let state = State::new(json!({"name": "John"}));
manager.save(state).expect("Failed to save state");§Errors
This function will return an error if:
- The directory cannot be created
- The state file cannot be written
Sourcepub fn load(&self, id: &Uuid) -> Result<Option<State>>
pub fn load(&self, id: &Uuid) -> Result<Option<State>>
Loads a state by its ID, first checking in-memory storage then the filesystem.
§Arguments
id- The UUID of the state to load
§Returns
An io::Result containing:
Some(State)if the state was foundNoneif the state was not found- An error if the filesystem operation failed
§Examples
use state::StateManager;
let manager = StateManager::new("states".to_string());
match manager.load(&state_id) {
Ok(Some(state)) => println!("State found: {:?}", state),
Ok(None) => println!("State not found"),
Err(e) => println!("Error loading state: {}", e),
}§Errors
This function will return an error if:
- The state file exists but cannot be read
- The state file contains invalid JSON
Sourcepub fn delete(&mut self, id: &Uuid) -> Result<()>
pub fn delete(&mut self, id: &Uuid) -> Result<()>
Deletes a state by its ID from both in-memory storage and the filesystem.
§Arguments
id- The UUID of the state to delete
§Returns
An io::Result indicating success or containing an error if the filesystem operation failed
§Examples
use state::StateManager;
let mut manager = StateManager::new("states".to_string());
manager.delete(&state_id).expect("Failed to delete state");§Errors
This function will return an error if:
- The state file exists but cannot be deleted
Sourcepub fn load_from_dir(dir: &str) -> Result<Self>
pub fn load_from_dir(dir: &str) -> Result<Self>
Creates a StateManager by loading all state files from the specified directory.
§Arguments
dir- The directory path to load state files from
§Returns
An io::Result containing a new StateManager with all states loaded from the directory
§Examples
use state::StateManager;
let manager = StateManager::load_from_dir("states").expect("Failed to load states");
println!("Loaded {} states", manager.states.len());§Errors
This function will return an error if:
- The directory cannot be read
- A state file contains invalid JSON
- A state file cannot be read
Sourcepub fn clear_dir(dir: &str) -> Result<()>
pub fn clear_dir(dir: &str) -> Result<()>
Clears all state files from the specified directory.
§Arguments
dir- The directory path to clear
§Returns
An io::Result indicating success or containing an error if the operation failed
§Examples
use state::StateManager;
StateManager::clear_dir("states").expect("Failed to clear states directory");§Errors
This function will return an error if:
- The directory cannot be read or modified