Skip to main content

zero_postgres/
statement.rs

1//! Statement reference types for polymorphic exec_* methods.
2
3use crate::state::extended::PreparedStatement;
4
5/// A resolved reference to either raw SQL or a prepared statement.
6///
7/// Returned by [`IntoStatement::statement_ref`] to allow exhaustive
8/// matching at call sites, replacing the old `Option`-returning methods
9/// that required `unwrap`.
10pub enum StatementRef<'a> {
11    Sql(&'a str),
12    Prepared(&'a PreparedStatement),
13}
14
15/// Sealed trait for types that can be used as statement references in exec_* methods.
16///
17/// This trait is sealed and cannot be implemented outside this crate.
18pub trait IntoStatement: private::Sealed {
19    /// Return a [`StatementRef`] discriminating raw SQL from a prepared statement.
20    fn statement_ref(&self) -> StatementRef<'_>;
21}
22
23mod private {
24    use crate::state::extended::PreparedStatement;
25
26    pub trait Sealed {}
27
28    impl Sealed for PreparedStatement {}
29    impl Sealed for &PreparedStatement {}
30    impl Sealed for str {}
31    impl Sealed for &str {}
32    impl Sealed for &&str {}
33}
34
35impl IntoStatement for &PreparedStatement {
36    fn statement_ref(&self) -> StatementRef<'_> {
37        StatementRef::Prepared(self)
38    }
39}
40
41impl IntoStatement for &str {
42    fn statement_ref(&self) -> StatementRef<'_> {
43        StatementRef::Sql(self)
44    }
45}
46
47impl IntoStatement for &&str {
48    fn statement_ref(&self) -> StatementRef<'_> {
49        StatementRef::Sql(self)
50    }
51}
52
53impl IntoStatement for str {
54    fn statement_ref(&self) -> StatementRef<'_> {
55        StatementRef::Sql(self)
56    }
57}
58
59impl IntoStatement for PreparedStatement {
60    fn statement_ref(&self) -> StatementRef<'_> {
61        StatementRef::Prepared(self)
62    }
63}