Expand description
FSM state management for ferogram bots.
This crate is part of ferogram, an async Rust MTProto client built by Ankit Chaubey.
- Channel: t.me/Ferogram
- Chat: t.me/FerogramChat
Provides a finite-state machine layer for multi-step bot conversations.
Each user/chat slot holds an optional state string and an arbitrary
key-value data bag. Handlers are gated on the current state and receive
a StateContext to transition to the next state or read/write data.
Most users reach this through the ferogram crate’s handler builder
(.state::<MyState>(MyState::WaitingName)). Use ferogram-fsm directly
only when building a custom dispatcher or storage backend.
§What’s in here
FsmState: Trait that state enums must implement. Serialises a variant to a string key and deserialises it back. Derived automatically via#[derive(FsmState)]fromferogram-derive.StateContext: Injected into state-matched handlers. ExposesStateContext::transitionto move to the next state,StateContext::clear_stateto finish the conversation, and typedStateContext::set_data/StateContext::get_datafor per-slot JSON-serialised fields.StateStorage: Async trait for the persistence backend. Implement it to add Redis, SQLite, or any other store.MemoryStorage: Built-in in-process backend backed byDashMap. Zero setup; state is lost on restart.StateKey/StateKeyStrategy: Controls how the storage slot is keyed. The default strategy keys by(chat_id, user_id)so each user in a group has independent state.StorageError: Error type returned by all storage operations.
§Example
use ferogram_fsm::{FsmState, MemoryStorage, StateContext};
#[derive(Clone, Debug, PartialEq)]
enum OrderState { WaitingItem, WaitingQty, Done }
impl FsmState for OrderState {
fn as_key(&self) -> String {
match self {
Self::WaitingItem => "WaitingItem".into(),
Self::WaitingQty => "WaitingQty".into(),
Self::Done => "Done".into(),
}
}
fn from_key(key: &str) -> Option<Self> {
match key {
"WaitingItem" => Some(Self::WaitingItem),
"WaitingQty" => Some(Self::WaitingQty),
"Done" => Some(Self::Done),
_ => None,
}
}
}
// In practice, use #[derive(FsmState)] from ferogram-derive instead.Structs§
- Memory
Storage - An in-process, non-persistent
StateStoragebacked byDashMap. - State
Context - The FSM context injected into state-matched handlers.
- State
Key - Identifies which conversation slot to read/write state for.
- Storage
Error - An error from a
StateStoragebackend.
Enums§
- State
KeyStrategy - How the FSM key is composed from an incoming message.
Traits§
- FsmState
- A type that can be used as an FSM state.
- Message
Like - Minimal view of an incoming message needed to build a
StateKey. - State
Storage - Persistent storage backend for FSM state.