tokio_postgres_fork/
to_statement.rs

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