Gardening + docs improvals

This commit is contained in:
Sergey Levitin 2020-10-25 11:50:47 +03:00
parent 633d5b0d64
commit 32600ff8f6
2 changed files with 30 additions and 30 deletions

View file

@ -7,7 +7,7 @@ To change the source code, fork the `dev` branch of this repository and work ins
``` ```
cargo clippy --all --all-features --all-targets cargo clippy --all --all-features --all-targets
cargo test --all cargo test --all
cargo doc --open --all-features RUSTDOCFLAGS="--cfg docsrs" cargo doc --open --all-features
# Using nightly rustfmt # Using nightly rustfmt
cargo +nightly fmt --all -- --check cargo +nightly fmt --all -- --check
``` ```

View file

@ -10,6 +10,12 @@ use std::{
}; };
use thiserror::Error; use thiserror::Error;
/// A persistent storage based on [SQLite](https://www.sqlite.org/).
pub struct SqliteStorage<S> {
pool: SqlitePool,
serializer: S,
}
/// An error returned from [`SqliteStorage`]. /// An error returned from [`SqliteStorage`].
/// ///
/// [`SqliteStorage`]: struct.SqliteStorage.html /// [`SqliteStorage`]: struct.SqliteStorage.html
@ -24,17 +30,6 @@ where
SqliteError(#[from] sqlx::Error), SqliteError(#[from] sqlx::Error),
} }
/// A persistent storage based on [SQLite](https://www.sqlite.org/).
pub struct SqliteStorage<S> {
pool: SqlitePool,
serializer: S,
}
#[derive(sqlx::FromRow)]
struct DialogueDBRow {
dialogue: Vec<u8>,
}
impl<S> SqliteStorage<S> { impl<S> SqliteStorage<S> {
pub async fn open( pub async fn open(
path: &str, path: &str,
@ -57,24 +52,6 @@ CREATE TABLE IF NOT EXISTS teloxide_dialogues (
} }
} }
async fn get_dialogue(
pool: &SqlitePool,
chat_id: i64,
) -> Result<Option<Box<Vec<u8>>>, sqlx::Error> {
Ok(
match sqlx::query_as::<_, DialogueDBRow>(
"SELECT dialogue FROM teloxide_dialogues WHERE chat_id = ?",
)
.bind(chat_id)
.fetch_optional(pool)
.await?
{
Some(r) => Some(Box::new(r.dialogue)),
_ => None,
},
)
}
impl<S, D> Storage<D> for SqliteStorage<S> impl<S, D> Storage<D> for SqliteStorage<S>
where where
S: Send + Sync + Serializer<D> + 'static, S: Send + Sync + Serializer<D> + 'static,
@ -135,3 +112,26 @@ where
}) })
} }
} }
#[derive(sqlx::FromRow)]
struct DialogueDBRow {
dialogue: Vec<u8>,
}
async fn get_dialogue(
pool: &SqlitePool,
chat_id: i64,
) -> Result<Option<Box<Vec<u8>>>, sqlx::Error> {
Ok(
match sqlx::query_as::<_, DialogueDBRow>(
"SELECT dialogue FROM teloxide_dialogues WHERE chat_id = ?",
)
.bind(chat_id)
.fetch_optional(pool)
.await?
{
Some(r) => Some(Box::new(r.dialogue)),
_ => None,
},
)
}