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
use {
    super::ExprNode,
    crate::{
        ast::Expr,
        parse_sql::parse_comma_separated_exprs,
        result::{Error, Result},
        translate::translate_expr,
    },
};

#[derive(Clone)]
pub enum ExprList {
    Text(String),
    Exprs(Vec<ExprNode>),
}

impl From<&str> for ExprList {
    fn from(exprs: &str) -> Self {
        ExprList::Text(exprs.to_owned())
    }
}

impl From<Vec<ExprNode>> for ExprList {
    fn from(exprs: Vec<ExprNode>) -> Self {
        ExprList::Exprs(exprs)
    }
}

impl From<Vec<&str>> for ExprList {
    fn from(exprs: Vec<&str>) -> Self {
        ExprList::Exprs(exprs.into_iter().map(Into::into).collect())
    }
}

impl TryFrom<ExprList> for Vec<Expr> {
    type Error = Error;

    fn try_from(expr_list: ExprList) -> Result<Self> {
        match expr_list {
            ExprList::Text(exprs) => parse_comma_separated_exprs(exprs)?
                .iter()
                .map(translate_expr)
                .collect::<Result<Vec<_>>>(),
            ExprList::Exprs(nodes) => nodes
                .into_iter()
                .map(TryInto::try_into)
                .collect::<Result<Vec<_>>>(),
        }
    }
}