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
64
65
66
67
68
69
70
71
72
73
use super::*;

#[derive(Debug, Clone)]
pub struct MethodSpec {
    pub name: String,
    pub arguments: Vec<Statement>,
    pub elements: Elements,
    pub is_static: bool,
}

impl MethodSpec {
    pub fn new(name: &str) -> MethodSpec {
        MethodSpec {
            name: name.to_owned(),
            arguments: Vec::new(),
            elements: Elements::new(),
            is_static: false,
        }
    }

    pub fn with_static(name: &str) -> MethodSpec {
        MethodSpec {
            name: name.to_owned(),
            arguments: Vec::new(),
            elements: Elements::new(),
            is_static: true,
        }
    }

    pub fn push_argument<S>(&mut self, argument: S)
    where
        S: Into<Statement>,
    {
        self.arguments.push(argument.into());
    }

    pub fn push<E>(&mut self, element: E)
    where
        E: Into<Element>,
    {
        self.elements.push(element);
    }
}

impl From<MethodSpec> for Element {
    fn from(value: MethodSpec) -> Element {
        let mut open = Statement::new();

        let mut arguments = Statement::new();

        for argument in value.arguments {
            arguments.push(argument);
        }

        if value.is_static {
            open.push("static ");
        }

        open.push(value.name);
        open.push("(");
        open.push(arguments.join(", "));
        open.push(")");

        open.push(" {");

        let mut out = Elements::new();
        out.push(open);
        out.push_nested(value.elements.join(Spacing));
        out.push("}");

        out.into()
    }
}