mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
Sqlite-storage feature added
This commit is contained in:
parent
34a35e690e
commit
fc5c855a23
2 changed files with 54 additions and 0 deletions
48
src/dispatching/dialogue/storage/sqlite_storage.rs
Normal file
48
src/dispatching/dialogue/storage/sqlite_storage.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
use super::{serializer::Serializer, Storage};
|
||||
use std::{
|
||||
convert::Infallible,
|
||||
fmt::{Debug, Display},
|
||||
sync::Arc,
|
||||
};
|
||||
use rusqlite::{params, Connection, Error, Result};
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
use thiserror::Error;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
pub enum SqliteStorageLocation {
|
||||
InMemory,
|
||||
Path(String),
|
||||
}
|
||||
|
||||
// An error returned from [`SqliteStorage`].
|
||||
#[derive(Debug, Error)]
|
||||
pub enum SqliteStorageError<SE>
|
||||
where
|
||||
SE: Debug + Display,
|
||||
{
|
||||
#[error("parsing/serializing error: {0}")]
|
||||
SerdeError(SE),
|
||||
#[error("error from Sqlite: {0}")]
|
||||
SqliteError(#[from] Error),
|
||||
}
|
||||
|
||||
pub struct SqliteStorage<S> {
|
||||
conn: Mutex<Connection>,
|
||||
serializer: S,
|
||||
}
|
||||
|
||||
impl <S> SqliteStorage<S> {
|
||||
pub async fn open(
|
||||
path: SqliteStorageLocation,
|
||||
serializer: S,
|
||||
) -> Result<Arc<Self>, SqliteStorageError<Infallible>>{
|
||||
let url = match path {
|
||||
SqliteStorageLocation::InMemory => String::from("sqlite::memory:"),
|
||||
SqliteStorageLocation::Path(p) => p,
|
||||
};
|
||||
Ok(Arc::new(Self {
|
||||
conn: Mutex::new(Connection::open(&url[..])?),
|
||||
serializer,
|
||||
}))
|
||||
}
|
||||
}
|
6
tests/sqlite.rs
Normal file
6
tests/sqlite.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
use teloxide::dispatching::dialogue::SqliteStorage;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_sqlite() {
|
||||
todo!()
|
||||
}
|
Loading…
Reference in a new issue