Sqlite-storage feature added

This commit is contained in:
S-Y-rat 2020-07-31 09:31:08 +03:00
parent 34a35e690e
commit fc5c855a23
2 changed files with 54 additions and 0 deletions

View 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
View file

@ -0,0 +1,6 @@
use teloxide::dispatching::dialogue::SqliteStorage;
#[tokio::test]
async fn test_sqlite() {
todo!()
}