sqlint/ast/
conjunctive.rs

1use crate::ast::{ConditionTree, Expression};
2
3/// `AND`, `OR` and `NOT` conjunctive implementations.
4pub trait Conjunctive<'a> {
5    /// Builds an `AND` condition having `self` as the left leaf and `other` as the right.
6    ///
7    /// ```rust
8    /// # use sqlint::{ast::*, visitor::{Visitor, Sqlite}};
9    /// assert_eq!(
10    ///     "foo".equals("bar").and("wtf".less_than(3)),
11    ///     ConditionTree::And(vec![
12    ///         Expression::from("foo".equals("bar")),
13    ///         Expression::from("wtf".less_than(3))
14    ///     ])
15    /// )
16    /// ```
17    fn and<E>(self, other: E) -> ConditionTree<'a>
18    where
19        E: Into<Expression<'a>>;
20
21    /// Builds an `OR` condition having `self` as the left leaf and `other` as the right.
22    ///
23    /// ```rust
24    /// # use sqlint::{ast::*, visitor::{Visitor, Sqlite}};
25    /// assert_eq!(
26    ///     "foo".equals("bar").or("wtf".less_than(3)),
27    ///     ConditionTree::Or(vec![
28    ///         Expression::from("foo".equals("bar")),
29    ///         Expression::from("wtf".less_than(3))
30    ///     ])
31    /// )
32    /// ```
33    fn or<E>(self, other: E) -> ConditionTree<'a>
34    where
35        E: Into<Expression<'a>>;
36
37    /// Builds a `NOT` condition having `self` as the condition.
38    ///
39    /// ```rust
40    /// # use sqlint::{ast::*, visitor::{Visitor, Sqlite}};
41    /// assert_eq!(
42    ///     "foo".equals("bar").not(),
43    ///     ConditionTree::not("foo".equals("bar"))
44    /// )
45    /// ```
46    fn not(self) -> ConditionTree<'a>;
47}
48
49impl<'a, T> Conjunctive<'a> for T
50where
51    T: Into<Expression<'a>>,
52{
53    fn and<E>(self, other: E) -> ConditionTree<'a>
54    where
55        E: Into<Expression<'a>>,
56    {
57        ConditionTree::And(vec![self.into(), other.into()])
58    }
59
60    fn or<E>(self, other: E) -> ConditionTree<'a>
61    where
62        E: Into<Expression<'a>>,
63    {
64        ConditionTree::Or(vec![self.into(), other.into()])
65    }
66
67    fn not(self) -> ConditionTree<'a> {
68        ConditionTree::not(self.into())
69    }
70}