Debian Codegraph @

src/macros.rs

raw
1 macro_rules! platform {
2 { unix => { $($unix:tt)* }, windows => { $($windows:tt)* }, } => {
3 if cfg!(unix) {
4 #[cfg(unix)] { $($unix)* }
5 #[cfg(not(unix))] { unreachable!() }
6 } else if cfg!(windows) {
7 #[cfg(windows)] { $($windows)* }
8 #[cfg(not(windows))] { unreachable!() }
9 } else {
10 #[cfg(not(any(unix, windows)))] compile_error!("Unsupported platform");
11 unreachable!()
12 }
13 }
14 }
15
16 /// Like `writeln!`, but nicely wraps lines.
17 ///
18 /// Unlike `writeln` and like `eprintln` panics if it can't write to
19 /// the specified stream.
20 macro_rules! wwriteln {
21 {
22 stream=$stream: expr
23 } => {{
24 let stream: &mut dyn std::io::Write = $stream;
25 if let Err(err) = writeln!(stream) {
26 panic!("Error writing to output stream: {}", err);
27 }
28 }};
29
30 {
31 stream=$stream: expr,
32 indent=$i: expr,
33 $($arg: expr),*
34 } => {{
35 let stream: &mut dyn std::io::Write = $stream;
36 let i = $i;
37 crate::output::wrapping::iwwriteln(
38 stream,
39 i.as_ref(), i.as_ref(),
40 format_args!($($arg),*))
41 }};
42
43 {
44 stream=$stream: expr,
45 initial_indent=$ii: expr,
46 subsequent_indent=$si: expr,
47 $($arg: expr),*
48 } => {{
49 let stream: &mut dyn std::io::Write = $stream;
50 let ii = $ii;
51 let si = $si;
52 crate::output::wrapping::iwwriteln(
53 stream,
54 ii.as_ref(), si.as_ref(),
55 format_args!($($arg),*))
56 }};
57
58 {
59 stream=$stream: expr,
60 initial_indent=$ii: expr,
61 $($arg: expr),*
62 } => {{
63 let stream: &mut dyn std::io::Write = $stream;
64 let ii = $ii;
65 let si = format!("{:1$}", "", ii.len());
66 crate::output::wrapping::iwwriteln(
67 stream, ii.as_ref(), si.as_ref(),
68 format_args!($($arg),*))
69 }};
70
71 {
72 stream=$stream: expr,
73 $($arg: expr),*
74 } => {{
75 let stream: &mut dyn std::io::Write = $stream;
76 crate::output::wrapping::wwriteln(
77 stream, format_args!($($arg),*))
78 }};
79
80 {
81 $stream: expr,
82 $($arg: expr),*
83 } => {
84 wwriteln!(stream=$stream, $($arg),*)
85 };
86
87 {
88 $stream: expr
89 } => {
90 wwriteln!(stream=$stream)
91 };
92 }
93
94 /// Like eprintln, but nicely wraps lines.
95 macro_rules! weprintln {
96 { } => {
97 wwriteln!(stream=&mut std::io::stderr())
98 };
99
100 {
101 indent=$i: expr,
102 $($arg: expr),*
103 } => {
104 wwriteln!(stream=&mut std::io::stderr(),
105 indent=$i,
106 $($arg),*)
107 };
108
109 {
110 initial_indent=$ii: expr,
111 subsequent_indent=$si: expr,
112 $($arg: expr),*
113 } => {
114 wwriteln!(stream=&mut std::io::stderr(),
115 initial_indent=$ii,
116 subsequent_indent=$si,
117 $($arg),*)
118 };
119
120 {
121 initial_indent=$ii: expr,
122 $($arg: expr),*
123 } => {
124 wwriteln!(stream=&mut std::io::stderr(),
125 initial_indent=$ii,
126 $($arg),*)
127 };
128
129 {
130 $($arg: expr),*
131 } => {
132 wwriteln!(stream=&mut std::io::stderr(), $($arg),*)
133 };
134 }
135
136 /// Like weprintln, but doesn't emit anything in quiet mode.
137 macro_rules! make_qprintln {
138 ($stream: expr, $quiet: expr) => {
139 macro_rules! qprintln {
140 // XXX: Lot's of repetition due to the fact that nested
141 // macros still cannot have repetitions:
142 // https://github.com/rust-lang/rust/issues/83527
143
144 // First, with `indent`.
145 { indent=$i: expr } => {
146 if ! $quiet {
147 wwriteln!(stream=$stream, indent=$i);
148 }
149 };
150 { indent=$i: expr, $a0: expr } => {
151 if ! $quiet {
152 wwriteln!(stream=$stream, indent=$i, $a0);
153 }
154 };
155 { indent=$i: expr, $a0: expr, $a1: expr } => {
156 if ! $quiet {
157 wwriteln!(stream=$stream, indent=$i, $a0, $a1);
158 }
159 };
160 { indent=$i: expr, $a0: expr, $a1: expr, $a2: expr } => {
161 if ! $quiet {
162 wwriteln!(stream=$stream, indent=$i, $a0, $a1, $a2);
163 }
164 };
165 { indent=$i: expr, $a0: expr, $a1: expr, $a2: expr, $a3: expr } => {
166 if ! $quiet {
167 wwriteln!(stream=$stream, indent=$i, $a0, $a1, $a2, $a3);
168 }
169 };
170 { indent=$i: expr, $a0: expr, $a1: expr, $a2: expr, $a3: expr, $a4: expr } => {
171 if ! $quiet {
172 wwriteln!(stream=$stream, indent=$i, $a0, $a1, $a2, $a3, $a4);
173 }
174 };
175 { indent=$i: expr, $a0: expr, $a1: expr, $a2: expr, $a3: expr, $a4: expr, $a5: expr } => {
176 if ! $quiet {
177 wwriteln!(stream=$stream, indent=$i, $a0, $a1, $a2, $a3, $a4, $a5);
178 }
179 };
180 { indent=$i: expr, $a0: expr, $a1: expr, $a2: expr, $a3: expr, $a4: expr, $a5: expr, $a6: expr } => {
181 if ! $quiet {
182 wwriteln!(stream=$stream, indent=$i, $a0, $a1, $a2, $a3, $a4, $a5, $a6);
183 }
184 };
185 // Again, with `initial_indent` and `subsequent_indent`.
186 { initial_indent=$ii: expr, subsequent_indent=$si: expr } => {
187 if ! $quiet {
188 wwriteln!(stream=$stream, initial_indent=$ii, subsequent_indent=$si);
189 }
190 };
191 { initial_indent=$ii: expr, subsequent_indent=$si: expr, $a0: expr } => {
192 if ! $quiet {
193 wwriteln!(stream=$stream, initial_indent=$ii, subsequent_indent=$si, $a0);
194 }
195 };
196 { initial_indent=$ii: expr, subsequent_indent=$si: expr, $a0: expr, $a1: expr } => {
197 if ! $quiet {
198 wwriteln!(stream=$stream, initial_indent=$ii, subsequent_indent=$si, $a0, $a1);
199 }
200 };
201 { initial_indent=$ii: expr, subsequent_indent=$si: expr, $a0: expr, $a1: expr, $a2: expr } => {
202 if ! $quiet {
203 wwriteln!(stream=$stream, initial_indent=$ii, subsequent_indent=$si, $a0, $a1, $a2);
204 }
205 };
206 { initial_indent=$ii: expr, subsequent_indent=$si: expr, $a0: expr, $a1: expr, $a2: expr, $a3: expr } => {
207 if ! $quiet {
208 wwriteln!(stream=$stream, initial_indent=$ii, subsequent_indent=$si, $a0, $a1, $a2, $a3);
209 }
210 };
211 { initial_indent=$ii: expr, subsequent_indent=$si: expr, $a0: expr, $a1: expr, $a2: expr, $a3: expr, $a4: expr } => {
212 if ! $quiet {
213 wwriteln!(stream=$stream, initial_indent=$ii, subsequent_indent=$si, $a0, $a1, $a2, $a3, $a4);
214 }
215 };
216 { initial_indent=$ii: expr, subsequent_indent=$si: expr, $a0: expr, $a1: expr, $a2: expr, $a3: expr, $a4: expr, $a5: expr } => {
217 if ! $quiet {
218 wwriteln!(stream=$stream, initial_indent=$ii, subsequent_indent=$si, $a0, $a1, $a2, $a3, $a4, $a5);
219 }
220 };
221 { initial_indent=$ii: expr, subsequent_indent=$si: expr, $a0: expr, $a1: expr, $a2: expr, $a3: expr, $a4: expr, $a5: expr, $a6: expr } => {
222 if ! $quiet {
223 wwriteln!(stream=$stream, initial_indent=$ii, subsequent_indent=$si, $a0, $a1, $a2, $a3, $a4, $a5, $a6);
224 }
225 };
226
227 // Again, with `initial_indent`.
228 { initial_indent=$ii: expr } => {
229 if ! $quiet {
230 wwriteln!(stream=$stream, initial_indent=$ii);
231 }
232 };
233 { initial_indent=$ii: expr, $a0: expr } => {
234 if ! $quiet {
235 wwriteln!(stream=$stream, initial_indent=$ii, $a0);
236 }
237 };
238 { initial_indent=$ii: expr, $a0: expr, $a1: expr } => {
239 if ! $quiet {
240 wwriteln!(stream=$stream, initial_indent=$ii, $a0, $a1);
241 }
242 };
243 { initial_indent=$ii: expr, $a0: expr, $a1: expr, $a2: expr } => {
244 if ! $quiet {
245 wwriteln!(stream=$stream, initial_indent=$ii, $a0, $a1, $a2);
246 }
247 };
248 { initial_indent=$ii: expr, $a0: expr, $a1: expr, $a2: expr, $a3: expr } => {
249 if ! $quiet {
250 wwriteln!(stream=$stream, initial_indent=$ii, $a0, $a1, $a2, $a3);
251 }
252 };
253 { initial_indent=$ii: expr, $a0: expr, $a1: expr, $a2: expr, $a3: expr, $a4: expr } => {
254 if ! $quiet {
255 wwriteln!(stream=$stream, initial_indent=$ii, $a0, $a1, $a2, $a3, $a4);
256 }
257 };
258 { initial_indent=$ii: expr, $a0: expr, $a1: expr, $a2: expr, $a3: expr, $a4: expr, $a5: expr } => {
259 if ! $quiet {
260 wwriteln!(stream=$stream, initial_indent=$ii, $a0, $a1, $a2, $a3, $a4, $a5);
261 }
262 };
263 { initial_indent=$ii: expr, $a0: expr, $a1: expr, $a2: expr, $a3: expr, $a4: expr, $a5: expr, $a6: expr } => {
264 if ! $quiet {
265 wwriteln!(stream=$stream, initial_indent=$ii, $a0, $a1, $a2, $a3, $a4, $a5, $a6);
266 }
267 };
268
269 // Again, without any indent.
270 {} => {
271 if ! $quiet {
272 wwriteln!(stream=$stream);
273 }
274 };
275 { $a0: expr } => {
276 if ! $quiet {
277 wwriteln!(stream=$stream, $a0);
278 }
279 };
280 { $a0: expr, $a1: expr } => {
281 if ! $quiet {
282 wwriteln!(stream=$stream, $a0, $a1);
283 }
284 };
285 { $a0: expr, $a1: expr, $a2: expr } => {
286 if ! $quiet {
287 wwriteln!(stream=$stream, $a0, $a1, $a2);
288 }
289 };
290 { $a0: expr, $a1: expr, $a2: expr, $a3: expr } => {
291 if ! $quiet {
292 wwriteln!(stream=$stream, $a0, $a1, $a2, $a3);
293 }
294 };
295 { $a0: expr, $a1: expr, $a2: expr, $a3: expr, $a4: expr } => {
296 if ! $quiet {
297 wwriteln!(stream=$stream, $a0, $a1, $a2, $a3, $a4);
298 }
299 };
300 { $a0: expr, $a1: expr, $a2: expr, $a3: expr, $a4: expr, $a5: expr } => {
301 if ! $quiet {
302 wwriteln!(stream=$stream, $a0, $a1, $a2, $a3, $a4, $a5);
303 }
304 };
305 { $a0: expr, $a1: expr, $a2: expr, $a3: expr, $a4: expr, $a5: expr, $a6: expr } => {
306 if ! $quiet {
307 wwriteln!(stream=$stream, $a0, $a1, $a2, $a3, $a4, $a5, $a6);
308 }
309 };
310 }
311 };
312 ($quiet: expr) => {
313 make_qprintln!(&mut std::io::stderr(), $quiet)
314 }
315 }