pub trait Storage<D> {
type Error;
// Required methods
fn remove_dialogue(
self: Arc<Self>,
key: DialogueKey,
) -> BoxFuture<'static, Result<(), Self::Error>>
where D: Send + 'static;
fn update_dialogue(
self: Arc<Self>,
key: DialogueKey,
dialogue: D,
) -> BoxFuture<'static, Result<(), Self::Error>>
where D: Send + 'static;
fn get_dialogue(
self: Arc<Self>,
key: DialogueKey,
) -> BoxFuture<'static, Result<Option<D>, Self::Error>>;
fn get_or_insert_dialogue(
self: Arc<Self>,
key: DialogueKey,
default: fn() -> D,
reset_incompatible: bool,
) -> BoxFuture<'static, Result<D, Self::Error>>;
// Provided method
fn erase(self: Arc<Self>) -> Arc<ErasedStorage<D>> ⓘ
where D: 'static,
Self: Sized + Send + Sync + 'static,
Self::Error: Error + Send + Sync + 'static { ... }
}Expand description
A storage of dialogues.
You can implement this trait for a structure that communicates with a DB and be sure that after you restart your bot, all the dialogues won’t be lost.
Storage is used only to store dialogue states, i.e. it can’t be used as a
generic database.
Currently we support the following storages out of the box:
InMemStorage– a storage based onstd::collections::HashMap.RedisStorage– a Redis-based storage.SqliteStorage– an SQLite-based persistent storage.
Required Associated Types§
Required Methods§
Sourcefn remove_dialogue(
self: Arc<Self>,
key: DialogueKey,
) -> BoxFuture<'static, Result<(), Self::Error>>where
D: Send + 'static,
fn remove_dialogue(
self: Arc<Self>,
key: DialogueKey,
) -> BoxFuture<'static, Result<(), Self::Error>>where
D: Send + 'static,
Removes a dialogue indexed by key.
If the dialogue indexed by key does not exist, this function
results in an error.
Sourcefn update_dialogue(
self: Arc<Self>,
key: DialogueKey,
dialogue: D,
) -> BoxFuture<'static, Result<(), Self::Error>>where
D: Send + 'static,
fn update_dialogue(
self: Arc<Self>,
key: DialogueKey,
dialogue: D,
) -> BoxFuture<'static, Result<(), Self::Error>>where
D: Send + 'static,
Updates a dialogue indexed by key with dialogue.
Sourcefn get_dialogue(
self: Arc<Self>,
key: DialogueKey,
) -> BoxFuture<'static, Result<Option<D>, Self::Error>>
fn get_dialogue( self: Arc<Self>, key: DialogueKey, ) -> BoxFuture<'static, Result<Option<D>, Self::Error>>
Returns the dialogue indexed by key.
Sourcefn get_or_insert_dialogue(
self: Arc<Self>,
key: DialogueKey,
default: fn() -> D,
reset_incompatible: bool,
) -> BoxFuture<'static, Result<D, Self::Error>>
fn get_or_insert_dialogue( self: Arc<Self>, key: DialogueKey, default: fn() -> D, reset_incompatible: bool, ) -> BoxFuture<'static, Result<D, Self::Error>>
Returns the stored dialogue or initializes it with default if absent.
Implementations must not overwrite a concurrently inserted dialogue.
When reset_incompatible is true, implementations may also replace a
state that fails to deserialize, but must not ignore other errors or
overwrite a concurrent update.
Provided Methods§
Sourcefn erase(self: Arc<Self>) -> Arc<ErasedStorage<D>> ⓘ
fn erase(self: Arc<Self>) -> Arc<ErasedStorage<D>> ⓘ
Erases Self::Error to std::error::Error.
Examples found in repository?
35async fn main() {
36 pretty_env_logger::init();
37 log::info!("Starting DB remember bot...");
38
39 let bot = Bot::from_env();
40
41 let storage: MyStorage = if std::env::var("DB_REMEMBER_REDIS").is_ok() {
42 RedisStorage::open("redis://127.0.0.1:6379", Bincode).await.unwrap().erase()
43 } else {
44 SqliteStorage::open("db.sqlite", Json).await.unwrap().erase()
45 };
46
47 let handler = Update::filter_message()
48 .enter_dialogue::<Message, ErasedStorage<State>, State>()
49 .branch(dptree::case![State::Start].endpoint(start))
50 .branch(
51 dptree::case![State::GotNumber(n)]
52 .branch(dptree::entry().filter_command::<Command>().endpoint(got_number))
53 .branch(dptree::endpoint(invalid_command)),
54 );
55
56 Dispatcher::builder(bot, handler)
57 .dependencies(dptree::deps![storage])
58 .enable_ctrlc_handler()
59 .build()
60 .dispatch()
61 .await;
62}Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementors§
Source§impl<D> Storage<D> for InMemStorage<D>
impl<D> Storage<D> for InMemStorage<D>
type Error = InMemStorageError
Source§impl<S, D> Storage<D> for PostgresStorage<S>where
S: Send + Sync + Serializer<D> + 'static,
D: Send + Serialize + DeserializeOwned + 'static,
<S as Serializer<D>>::Error: Debug + Display,
Available on crate features postgres-storage-nativetls or postgres-storage-rustls only.
impl<S, D> Storage<D> for PostgresStorage<S>where
S: Send + Sync + Serializer<D> + 'static,
D: Send + Serialize + DeserializeOwned + 'static,
<S as Serializer<D>>::Error: Debug + Display,
postgres-storage-nativetls or postgres-storage-rustls only.type Error = PostgresStorageError<<S as Serializer<D>>::Error>
Source§impl<S, D> Storage<D> for RedisStorage<S>where
S: Send + Sync + Serializer<D> + 'static,
D: Send + Serialize + DeserializeOwned + 'static,
<S as Serializer<D>>::Error: Debug + Display,
Available on crate feature redis-storage only.
impl<S, D> Storage<D> for RedisStorage<S>where
S: Send + Sync + Serializer<D> + 'static,
D: Send + Serialize + DeserializeOwned + 'static,
<S as Serializer<D>>::Error: Debug + Display,
redis-storage only.type Error = RedisStorageError<<S as Serializer<D>>::Error>
Source§impl<S, D> Storage<D> for SqliteStorage<S>where
S: Send + Sync + Serializer<D> + 'static,
D: Send + Serialize + DeserializeOwned + 'static,
S::Error: Debug + Display,
Available on crate features sqlite-storage-nativetls or sqlite-storage-rustls only.
impl<S, D> Storage<D> for SqliteStorage<S>where
S: Send + Sync + Serializer<D> + 'static,
D: Send + Serialize + DeserializeOwned + 'static,
S::Error: Debug + Display,
sqlite-storage-nativetls or sqlite-storage-rustls only.