diff --git a/src/dispatching/dialogue/storage/sqlite_storage.rs b/src/dispatching/dialogue/storage/sqlite_storage.rs new file mode 100644 index 00000000..e497228c --- /dev/null +++ b/src/dispatching/dialogue/storage/sqlite_storage.rs @@ -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 +where + SE: Debug + Display, +{ + #[error("parsing/serializing error: {0}")] + SerdeError(SE), + #[error("error from Sqlite: {0}")] + SqliteError(#[from] Error), +} + +pub struct SqliteStorage { + conn: Mutex, + serializer: S, +} + +impl SqliteStorage { + pub async fn open( + path: SqliteStorageLocation, + serializer: S, + ) -> Result, SqliteStorageError>{ + 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, + })) + } +} diff --git a/tests/sqlite.rs b/tests/sqlite.rs new file mode 100644 index 00000000..786e3c2f --- /dev/null +++ b/tests/sqlite.rs @@ -0,0 +1,6 @@ +use teloxide::dispatching::dialogue::SqliteStorage; + +#[tokio::test] +async fn test_sqlite() { + todo!() +}