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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use core::fmt::Debug;

use crate::Generator;
use crate::Indentation;

pub struct Body {
    pub factory: Option<Box<dyn Fn(Indentation, usize) -> String>>,
    pub semicolon_for_empty: bool,
}

impl Body {
    pub fn new() -> Self {
        Self {
            factory: None,
            semicolon_for_empty: true,
        }
    }

    pub fn with_factory<T: Fn(Indentation, usize) -> String + 'static>(factory: T) -> Self {
        Self {
            factory: Some(Box::new(factory)),
            semicolon_for_empty: true,
        }
    }

    pub fn with_semicolon_for_empty(mut self, semicolon_for_empty: bool) -> Self {
        self.semicolon_for_empty = semicolon_for_empty;

        self
    }
}

impl Debug for Body {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Body")
            .field("factory:is-some", &self.factory.is_some())
            .field("semicolon_for_empty", &self.semicolon_for_empty)
            .finish()
    }
}

impl Generator for Body {
    fn generate(&self, indentation: Indentation, level: usize) -> String {
        match self.factory {
            Some(ref factory) => {
                let mut code = String::new();

                code.push_str(" {");
                code.push('\n');
                code.push_str(&factory(indentation, level + 1));
                code.push('\n');
                code.push_str(&indentation.indent("}", level));
                code.push('\n');

                code
            }
            None => {
                let mut code = String::new();

                if self.semicolon_for_empty {
                    code.push(';');
                } else {
                    code.push_str(" {");
                    code.push('\n');
                    code.push_str(&indentation.indent("// empty body", level + 1));
                    code.push('\n');
                    code.push_str(&indentation.indent("}", level));
                }

                code.push('\n');

                code
            }
        }
    }
}

impl<T: ToString> From<Vec<T>> for Body {
    fn from(body: Vec<T>) -> Self {
        let body = body
            .iter()
            .map(|line| line.to_string())
            .collect::<Vec<String>>();

        Self {
            factory: Some(Box::new(move |indentation, level| {
                let body = body.clone();

                body.iter()
                    .map(|line| indentation.indent(&line.to_string(), level))
                    .collect::<Vec<String>>()
                    .join("\n")
            })),
            semicolon_for_empty: true,
        }
    }
}

impl From<String> for Body {
    fn from(body: String) -> Self {
        Self {
            factory: Some(Box::new(move |indentation, level| {
                let body = body.clone();

                indentation.indent(body, level)
            })),
            semicolon_for_empty: true,
        }
    }
}

impl From<&str> for Body {
    fn from(body: &str) -> Self {
        body.to_string().into()
    }
}

impl<T: Fn(Indentation, usize) -> String + 'static> From<T> for Body {
    fn from(factory: T) -> Self {
        Self {
            factory: Some(Box::new(factory)),
            semicolon_for_empty: true,
        }
    }
}

impl From<Option<Box<dyn Fn(Indentation, usize) -> String>>> for Body {
    fn from(factory: Option<Box<dyn Fn(Indentation, usize) -> String>>) -> Self {
        Self {
            factory,
            semicolon_for_empty: true,
        }
    }
}

impl From<Option<String>> for Body {
    fn from(body: Option<String>) -> Self {
        match body {
            Some(body) => body.into(),
            None => Self {
                factory: None,
                semicolon_for_empty: true,
            },
        }
    }
}

impl Default for Body {
    fn default() -> Self {
        Self {
            factory: None,
            semicolon_for_empty: true,
        }
    }
}