code_gen/rust/function/
with_functions.rs

1use crate::rust::Function;
2use crate::{CodeBuffer, Statement};
3
4/// An element with functions.
5pub trait WithFunctions: Sized {
6    /// Gets the functions.
7    fn functions(&self) -> &[Function];
8
9    /// Adds the `function`.
10    fn add_function<F>(&mut self, function: F)
11    where
12        F: Into<Function>;
13
14    /// Adds the `function`.
15    fn with_function<F>(mut self, function: F) -> Self
16    where
17        F: Into<Function>,
18    {
19        self.add_function(function);
20        self
21    }
22
23    /// Writes the functions.
24    fn write_functions(&self, b: &mut CodeBuffer, level: usize) {
25        if let Some((first, rest)) = self.functions().split_first() {
26            first.write(b, level);
27            for function in rest {
28                b.end_line();
29                function.write(b, level);
30            }
31        }
32    }
33}