Skip to main content

ferogram_fsm/
lib.rs

1// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
2//
3// ferogram: async Telegram MTProto client in Rust
4// https://github.com/ankit-chaubey/ferogram
5//
6// Licensed under either the MIT License or the Apache License 2.0.
7// See the LICENSE-MIT or LICENSE-APACHE file in this repository:
8// https://github.com/ankit-chaubey/ferogram
9//
10// Feel free to use, modify, and share this code.
11// Please keep this notice when redistributing.
12
13#![deny(unsafe_code)]
14
15mod context;
16mod error;
17mod key;
18mod storage;
19
20pub use context::StateContext;
21pub use error::StorageError;
22pub use key::{MessageLike, StateKey, StateKeyStrategy};
23pub use storage::{MemoryStorage, StateStorage};
24
25/// A type that can be used as an FSM state.
26///
27/// Implement this trait on an enum to use it with [`StateContext`] and
28/// the FSM dispatcher.
29///
30/// In practice you will derive this via `#[derive(FsmState)]`:
31///
32/// ```rust,no_run
33/// #[derive(Clone, Debug, PartialEq)]
34/// enum CheckoutState {
35///     Cart,
36///     Address,
37///     Payment,
38///     Confirmation,
39/// }
40/// ```
41pub trait FsmState: Send + Sync + 'static {
42    /// Serialize this state variant to a string key (e.g. `"WaitingProduct"`).
43    fn as_key(&self) -> String;
44
45    /// Deserialize a state variant from a key string. Returns `None` if the
46    /// key does not match any variant.
47    fn from_key(key: &str) -> Option<Self>
48    where
49        Self: Sized;
50}