Line data Source code
1 : use crate::to_statement::private::{Sealed, ToStatementType};
2 : use crate::Statement;
3 :
4 : mod private {
5 : use crate::{Client, Error, Statement};
6 :
7 : pub trait Sealed {}
8 :
9 : pub enum ToStatementType<'a> {
10 : Statement(&'a Statement),
11 : Query(&'a str),
12 : }
13 :
14 : impl ToStatementType<'_> {
15 0 : pub async fn into_statement(self, client: &Client) -> Result<Statement, Error> {
16 0 : match self {
17 0 : ToStatementType::Statement(s) => Ok(s.clone()),
18 0 : ToStatementType::Query(s) => client.prepare(s).await,
19 : }
20 0 : }
21 : }
22 : }
23 :
24 : /// A trait abstracting over prepared and unprepared statements.
25 : ///
26 : /// Many methods are generic over this bound, so that they support both a raw query string as well as a statement which
27 : /// was prepared previously.
28 : ///
29 : /// This trait is "sealed" and cannot be implemented by anything outside this crate.
30 : pub trait ToStatement: Sealed {
31 : #[doc(hidden)]
32 : fn __convert(&self) -> ToStatementType<'_>;
33 : }
34 :
35 : impl ToStatement for Statement {
36 0 : fn __convert(&self) -> ToStatementType<'_> {
37 0 : ToStatementType::Statement(self)
38 0 : }
39 : }
40 :
41 : impl Sealed for Statement {}
42 :
43 : impl ToStatement for str {
44 0 : fn __convert(&self) -> ToStatementType<'_> {
45 0 : ToStatementType::Query(self)
46 0 : }
47 : }
48 :
49 : impl Sealed for str {}
50 :
51 : impl ToStatement for String {
52 0 : fn __convert(&self) -> ToStatementType<'_> {
53 0 : ToStatementType::Query(self)
54 0 : }
55 : }
56 :
57 : impl Sealed for String {}
|