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
use super::argument::Argument;
use super::modifier::Modifier;
use con_::Con::Owned;
use cons::Cons;
use element::Element;
use into_tokens::IntoTokens;
use java::Java;
use tokens::Tokens;
#[derive(Debug, Clone)]
pub struct Constructor<'el> {
pub modifiers: Vec<Modifier>,
pub arguments: Vec<Argument<'el>>,
pub body: Tokens<'el, Java<'el>>,
pub throws: Option<Tokens<'el, Java<'el>>>,
annotations: Tokens<'el, Java<'el>>,
}
impl<'el> Constructor<'el> {
pub fn new() -> Constructor<'el> {
Constructor {
modifiers: vec![Modifier::Public],
annotations: Tokens::new(),
arguments: Vec::new(),
throws: None,
body: Tokens::new(),
}
}
pub fn annotation<A>(&mut self, annotation: A)
where
A: IntoTokens<'el, Java<'el>>,
{
self.annotations.push(annotation.into_tokens());
}
}
into_tokens_impl_from!((Cons<'el>, Constructor<'el>), Java<'el>);
impl<'el> IntoTokens<'el, Java<'el>> for (Cons<'el>, Constructor<'el>) {
fn into_tokens(self) -> Tokens<'el, Java<'el>> {
use self::Element::*;
let (name, mut c) = self;
let args: Vec<Tokens<Java>> = c.arguments.into_iter().map(|a| a.into_tokens()).collect();
let args: Tokens<Java> = args.into_tokens();
let mut sig: Tokens<Java> = Tokens::new();
c.modifiers.sort();
sig.extend(c.modifiers.into_iter().map(Into::into));
if !args.is_empty() {
let sep = toks![",", PushSpacing];
let args = args.join(sep);
sig.append(toks![name, "(", Nested(Owned(args)), ")",]);
} else {
sig.append(toks![name, "()"]);
}
if let Some(throws) = c.throws {
sig.append("throws");
sig.append(throws);
}
let mut s = Tokens::new();
if !c.annotations.is_empty() {
s.push(c.annotations);
}
s.push(toks![sig.join_spacing(), " {"]);
s.nested(c.body);
s.push("}");
s
}
}
#[cfg(test)]
mod tests {
use super::Constructor;
use cons::Cons;
use java::Java;
use tokens::Tokens;
#[test]
fn test_vec() {
let c = Constructor::new();
let t: Tokens<Java> = (Cons::Borrowed("Foo"), c).into();
let s = t.to_string();
let out = s.as_ref().map(|s| s.as_str());
assert_eq!(Ok("public Foo() {\n}"), out);
}
#[test]
fn test_throws() {
let mut c = Constructor::new();
c.throws = Some("Exception".into());
let t: Tokens<Java> = (Cons::Borrowed("Foo"), c).into();
let s = t.to_string();
let out = s.as_ref().map(|s| s.as_str());
assert_eq!(Ok("public Foo() throws Exception {\n}"), out);
}
}