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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*********************** GNU General Public License 3.0 ***********************\
| |
| Copyright (C) 2023 Kevin Matthes |
| |
| This program is free software: you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation, either version 3 of the License, or |
| (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with this program. If not, see <https://www.gnu.org/licenses/>. |
| |
\******************************************************************************/
/// Write a coloured error message to [`std::io::Stderr`] without a line feed.
///
/// # Examples
///
/// ```rust
/// use aeruginous::ceprint;
/// use sysexits::Result;
///
/// fn function() -> Result<()> {
/// ceprint!("Green message."!Green);
/// ceprint!("Green"!Green, " and white message.");
///
/// Ok(())
/// }
/// ```
///
/// # Errors
///
/// See [`crate::ColourMessage`].
/// Write a coloured error message to [`std::io::Stderr`].
///
/// # Examples
///
/// ```rust
/// use aeruginous::ceprintln;
/// use sysexits::Result;
///
/// fn function() -> Result<()> {
/// ceprintln!("Green message."!Green);
/// ceprintln!("Green"!Green, " and white message.");
///
/// Ok(())
/// }
/// ```
///
/// # Errors
///
/// See [`crate::ColourMessage`].
/// Implement getter methods for the given struct fields.
///
/// Getter methods usually only return either a reference to or a copy of the
/// corresponding field without any further action on the data. Their
/// definition is, thus, a repetitive task which can be automated by some
/// technologies. This macro aims to provide simple and convenient semantics to
/// do so.
///
/// # Copies of Values
///
/// This macro offers two modes for getter methods which shall retrieve a copy
/// the respective fields: `@cp` as well as `@fn @cp`. The difference between
/// these two is that the former one will create a new `impl` block for the
/// methods whilst the latter one requires the existence of such a block to put
/// the generated methods into.
///
/// The following example illustrates the generation of getter methods within a
/// completely new block. The Rust Documentation System will generate a section
/// on its own for this `impl` block.
///
/// ```rust
/// use aeruginous::getters;
///
/// struct Example {
/// a: i32,
/// b: f64,
/// }
///
/// getters!(@cp Example {
/// a: i32,
/// b: f64
/// });
///
/// let example = Example { a: 42, b: 23.0 };
///
/// assert_eq!(example.a(), 42);
/// assert_eq!(example.b(), 23.0);
/// ```
///
/// In case of further methods for the same struct, such a break in the
/// documentation might be rather unaesthetic. For this use case, there is the
/// second mode of this macro: `@fn @cp`. Users can decide to have their
/// getter methods rendered into an already existing `impl` block, as shown by
/// the following example.
///
/// ```rust
/// use aeruginous::getters;
///
/// struct Example {
/// a: i32,
/// b: f64,
/// }
///
/// impl Example {
/// getters!(@fn @cp
/// a: i32,
/// b: f64
/// );
///
/// pub fn function() -> i32 {
/// 42
/// }
/// }
///
/// let example = Example { a: 42, b: 23.0 };
///
/// assert_eq!(example.a(), 42);
/// assert_eq!(example.b(), 23.0);
/// assert_eq!(Example::function(), 42);
/// ```
///
/// # References to Values
///
/// More complex data often does not implement the [`Copy`] trait. Thus, when
/// creating a getter for such a field, returning a reference to the information
/// should be the preferred solution. This macro also offers modes for these
/// cases.
///
/// Again, first of all, here is an example for the creation of an entirely new
/// `impl` block to store the methods in.
///
/// ```rust
/// use aeruginous::getters;
///
/// struct Example {
/// a: String,
/// b: Vec<i32>,
/// }
///
/// getters!(@ref Example {
/// a: String,
/// b: Vec<i32>
/// });
///
/// let example = Example { a: "string".to_string(), b: vec![1, 2, 3] };
///
/// assert_eq!(example.a(), "string");
/// assert_eq!(example.b(), &[1, 2, 3]);
/// ```
///
/// Furthermore, the generation of getter methods returning references within
/// already existing `impl` blocks works analogously to the copying case.
///
/// ```rust
/// use aeruginous::getters;
///
/// struct Example {
/// a: String,
/// b: Vec<i32>,
/// }
///
/// impl Example {
/// getters!(@fn @ref
/// a: String,
/// b: Vec<i32>
/// );
///
/// pub fn function() -> i32 {
/// 42
/// }
/// }
///
/// let example = Example { a: "string".to_string(), b: vec![1, 2, 3] };
///
/// assert_eq!(example.a(), "string");
/// assert_eq!(example.b(), &[1, 2, 3]);
/// assert_eq!(Example::function(), 42);
/// ```
///
/// # Header Generation
///
/// All previously presented modes named the resulting method according to the
/// field they queried and did nothing more returning the data of that field
/// somehow. There might be use cases in which a the getter should not have the
/// same name as the field it queries or in which the query is more complex than
/// just returning a copy or a reference. For these cases, there is one last
/// mode: `@header`.
///
/// This macro renders the documentation as well as some useful compiler
/// attributes for each generated getter method. These information are
/// considered a getter's "header". When defining a specialised getter method,
/// one might would like to have exactly this header for the new method, as
/// well. This mode provides the required functionality therefore. In contrast
/// to the other modes, only *one* method per call can be tagged by such a
/// header. Furthermore, an *already existing* `impl` block is mandatory.
///
/// ```rust
/// use aeruginous::getters;
///
/// struct Example {
/// a: i32,
/// b: f64,
/// c: bool,
/// }
///
/// impl Example {
/// getters!(@fn @cp
/// a: i32,
/// b: f64
/// );
///
/// getters!(@header c = (
/// pub fn field_c(&self) -> String {
/// self.c.to_string()
/// }
/// ));
/// }
///
/// let example = Example {
/// a: 42,
/// b: 23.0,
/// c: true,
/// };
///
/// assert_eq!(example.a(), 42);
/// assert_eq!(example.b(), 23.0);
/// assert_eq!(example.field_c(), "true".to_string());
/// ```
;
=> ;
}
/// Create an `impl` block for the given struct.
///
/// Despite this macro being primarily intended for the definition of further
/// macros, it can be also applied in production anyway as the following
/// example illustrates.
///
/// ```rust
/// use aeruginous::implement;
///
/// struct Example;
///
/// implement!(Example;
/// pub fn function() -> i32 {
/// 42
/// }
/// );
///
/// assert_eq!(Example::function(), 42);
/// ```
/******************************************************************************/