Skip to main content

StateManager

Struct StateManager 

Source
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: String

Directory path where state files are stored

§states: HashMap<Uuid, State>

In-memory storage of states indexed by their UUID

Implementations§

Source§

impl StateManager

Source

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());
Source

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
Source

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 found
  • None if 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
Source

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
Source

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
Source

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

Trait Implementations§

Source§

impl Default for StateManager

Source§

fn default() -> StateManager

Returns the “default value” for a type. 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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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.