toasty_core/stmt/
expr_stmt.rs

1use super::{Expr, Insert, Statement};
2
3/// A statement used as an expression.
4///
5/// Wraps a statement (such as a subquery) so it can be used in expression
6/// contexts.
7///
8/// # Examples
9///
10/// ```text
11/// (SELECT max(age) FROM users)   // subquery as expression
12/// ```
13#[derive(Debug, Clone, PartialEq)]
14pub struct ExprStmt {
15    /// The wrapped statement.
16    pub stmt: Box<Statement>,
17}
18
19impl Expr {
20    /// Creates a sub-statement expression from any value convertible to
21    /// [`Statement`].
22    pub fn stmt(stmt: impl Into<Statement>) -> Self {
23        Self::Stmt(ExprStmt {
24            stmt: Box::new(stmt.into()),
25        })
26    }
27}
28
29impl From<ExprStmt> for Expr {
30    fn from(value: ExprStmt) -> Self {
31        Self::Stmt(value)
32    }
33}
34
35impl From<Statement> for ExprStmt {
36    fn from(value: Statement) -> Self {
37        Self { stmt: value.into() }
38    }
39}
40
41impl From<Insert> for ExprStmt {
42    fn from(value: Insert) -> Self {
43        Self {
44            stmt: Box::new(Statement::from(value)),
45        }
46    }
47}
48
49impl From<Insert> for Expr {
50    fn from(value: Insert) -> Self {
51        Self::Stmt(value.into())
52    }
53}