code_gen/expression/
with_params.rs

1use crate::{CodeBuffer, Expression, Literal};
2
3/// An element with expression params.
4pub trait WithParams: Sized {
5    /// Gets the params.
6    fn params(&self) -> &[Box<dyn Expression>];
7
8    /// Adds the boxed `param`.
9    fn add_boxed_param(&mut self, param: Box<dyn Expression>);
10
11    /// Adds the boxed `param`.
12    fn with_boxed_param(mut self, param: Box<dyn Expression>) -> Self {
13        self.add_boxed_param(param);
14        self
15    }
16
17    /// Adds the `param`.
18    fn add_param<E>(&mut self, param: E)
19    where
20        E: 'static + Expression,
21    {
22        self.add_boxed_param(Box::new(param));
23    }
24
25    /// Adds the `param`.
26    fn with_param<E>(mut self, param: E) -> Self
27    where
28        E: 'static + Expression,
29    {
30        self.add_param(param);
31        self
32    }
33
34    /// Adds the `literal` param.
35    fn add_literal_param<L>(&mut self, literal: L)
36    where
37        L: Into<Literal>,
38    {
39        self.add_param(literal.into());
40    }
41
42    /// Adds the `literal` param.
43    fn with_literal_param<L>(self, literal: L) -> Self
44    where
45        L: Into<Literal>,
46    {
47        self.with_param(literal.into())
48    }
49
50    /// Writes the params. (comma separated)
51    fn write_params(&self, b: &mut CodeBuffer) {
52        if let Some((first, rest)) = self.params().split_first() {
53            first.write(b);
54            for param in rest {
55                b.write(", ");
56                param.write(b);
57            }
58        }
59    }
60
61    /// Writes the params with parentheses. (comma separated)
62    fn write_params_with_parens(&self, b: &mut CodeBuffer) {
63        b.write("(");
64        self.write_params(b);
65        b.write(")");
66    }
67}