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
156
157
158
159
160
161
use crate::java::{Argument, BlockComment, Java, Modifier, VOID};
use crate::{Cons, IntoTokens, Tokens};
#[derive(Debug, Clone)]
pub struct Method<'el> {
pub modifiers: Vec<Modifier>,
pub arguments: Vec<Argument<'el>>,
pub body: Tokens<'el, Java<'el>>,
pub returns: Java<'el>,
pub parameters: Tokens<'el, Java<'el>>,
pub comments: Vec<Cons<'el>>,
pub throws: Option<Tokens<'el, Java<'el>>>,
annotations: Tokens<'el, Java<'el>>,
name: Cons<'el>,
}
impl<'el> Method<'el> {
pub fn new<N>(name: N) -> Method<'el>
where
N: Into<Cons<'el>>,
{
use self::Modifier::*;
Method {
modifiers: vec![Public],
arguments: vec![],
body: Tokens::new(),
returns: VOID,
parameters: Tokens::new(),
comments: Vec::new(),
throws: None,
annotations: Tokens::new(),
name: name.into(),
}
}
pub fn annotation<A>(&mut self, annotation: A)
where
A: IntoTokens<'el, Java<'el>>,
{
self.annotations.push(annotation.into_tokens());
}
pub fn name(&self) -> Cons<'el> {
self.name.clone()
}
}
into_tokens_impl_from!(Method<'el>, Java<'el>);
impl<'el> IntoTokens<'el, Java<'el>> for Method<'el> {
fn into_tokens(self) -> Tokens<'el, Java<'el>> {
let mut sig = Tokens::new();
sig.extend(self.modifiers.into_tokens());
if !self.parameters.is_empty() {
sig.append(toks!["<", self.parameters.join(", "), ">"]);
}
sig.append(self.returns);
sig.append({
let mut n = Tokens::new();
n.append(self.name);
let args: Vec<Tokens<Java>> = self
.arguments
.into_iter()
.map(IntoTokens::into_tokens)
.collect();
let args: Tokens<Java> = args.into_tokens();
n.append(toks!["(", args.join(", "), ")"]);
n
});
if let Some(throws) = self.throws {
sig.append("throws");
sig.append(throws);
}
let mut s = Tokens::new();
s.push_unless_empty(BlockComment(self.comments));
s.push_unless_empty(self.annotations);
let sig = sig.join_spacing();
if self.body.is_empty() {
s.push(toks![sig, ";"]);
} else {
s.push(toks![sig, " {"]);
s.nested(self.body);
s.push("}");
}
s
}
}
#[cfg(test)]
mod tests {
use super::Method;
use crate::tokens::Tokens;
fn build_method() -> Method<'static> {
let mut c = Method::new("foo");
c.parameters.append("T");
c
}
#[test]
fn test_with_comments() {
let mut c = build_method();
c.comments.push("Hello World".into());
let t = Tokens::from(c);
assert_eq!(
Ok(String::from(
"/**\n * Hello World\n */\npublic <T> void foo();",
)),
t.to_string()
);
}
#[test]
fn test_no_comments() {
let t = Tokens::from(build_method());
assert_eq!(Ok(String::from("public <T> void foo();")), t.to_string());
}
#[test]
fn test_throws() {
let mut m = build_method();
m.throws = Some("Exception".into());
let t = Tokens::from(m);
assert_eq!(
Ok(String::from("public <T> void foo() throws Exception;")),
t.to_string()
);
}
}