1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! # Conditional Expressions
//!
//! doc wip
use vantage_expressions::{Expression, expr};
/// SurrealDB conditional expression (IF-THEN-ELSE)
///
/// doc wip
///
/// # Examples
///
/// ```rust
/// use vantage_expressions::expr;
/// use vantage_surrealdb::conditional::Conditional;
///
/// // doc wip
/// let conditional = Conditional::new(
/// expr!("age >= 18"),
/// expr!("'adult'"),
/// expr!("'minor'")
/// );
/// ```
#[derive(Debug, Clone)]
pub struct Conditional {
condition: Expression,
then_expr: Expression,
else_expr: Expression,
}
impl Conditional {
/// Creates a new conditional expression
///
/// doc wip
///
/// # Arguments
///
/// * `condition` - doc wip
/// * `then_expr` - doc wip
/// * `else_expr` - doc wip
pub fn new(
condition: impl Into<Expression>,
then_expr: impl Into<Expression>,
else_expr: impl Into<Expression>,
) -> Self {
Self {
condition: condition.into(),
then_expr: then_expr.into(),
else_expr: else_expr.into(),
}
}
}
impl From<Conditional> for Expression {
fn from(val: Conditional) -> Self {
expr!(
"IF ({}) THEN ({}) ELSE ({}) END",
val.condition,
val.then_expr,
val.else_expr
)
}
}