Skip to main content

py_sql/node/
otherwise_node.rs

1use serde_json::Value;
2
3use crate::ast::RbatisAST;
4use crate::error::Error;
5use crate::node::node::do_child_nodes;
6use crate::node::node_type::NodeType;
7use rexpr::runtime::RExprRuntime;
8
9#[derive(Clone, Debug)]
10pub struct OtherwiseNode {
11    pub childs: Vec<NodeType>,
12}
13
14impl OtherwiseNode {
15    pub fn def_name() -> &'static str {
16        "_"
17    }
18
19    pub fn from(
20        source: &str,
21        express: &str,
22        childs: Vec<NodeType>,
23    ) -> Result<Self, crate::error::Error> {
24        let source = source.trim();
25        if source.starts_with(Self::def_name()) {
26            return Ok(OtherwiseNode { childs });
27        } else if source.starts_with(Self::name()) {
28            return Ok(OtherwiseNode { childs });
29        }
30        return Err(Error::from(
31            "[rbaits] OtherwiseNode must start with '_:' or 'otherwise:'",
32        ));
33    }
34}
35
36impl RbatisAST for OtherwiseNode {
37    fn name() -> &'static str {
38        "otherwise"
39    }
40    fn eval(
41        &self,
42        convert: &dyn crate::StringConvert,
43        env: &mut Value,
44        engine: &RExprRuntime,
45        arg_array: &mut Vec<Value>,
46        arg_sql: &mut String,
47    ) -> Result<serde_json::Value, crate::error::Error> {
48        return do_child_nodes(convert, &self.childs, env, engine, arg_array, arg_sql);
49    }
50}