wdl_format/v1/
task.rs

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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//! Formatting for tasks.

use wdl_ast::SyntaxKind;

use crate::PreToken;
use crate::TokenStream;
use crate::Trivia;
use crate::Writable as _;
use crate::element::FormatElement;

/// Formats a [`TaskDefinition`](wdl_ast::v1::TaskDefinition).
pub fn format_task_definition(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
    let mut children = element.children().expect("task definition children");

    stream.blank_lines_allowed_between_comments();

    let task_keyword = children.next().expect("task keyword");
    assert!(task_keyword.element().kind() == SyntaxKind::TaskKeyword);
    (&task_keyword).write(stream);
    stream.end_word();

    let name = children.next().expect("task name");
    assert!(name.element().kind() == SyntaxKind::Ident);
    (&name).write(stream);
    stream.end_word();

    let open_brace = children.next().expect("open brace");
    assert!(open_brace.element().kind() == SyntaxKind::OpenBrace);
    (&open_brace).write(stream);
    stream.end_line();
    stream.increment_indent();

    let mut meta = None;
    let mut parameter_meta = None;
    let mut input = None;
    let mut body = Vec::new();
    let mut command = None;
    let mut output = None;
    let mut requirements = None;
    let mut runtime = None;
    let mut hints = None;
    let mut close_brace = None;

    for child in children {
        match child.element().kind() {
            SyntaxKind::InputSectionNode => {
                input = Some(child.clone());
            }
            SyntaxKind::MetadataSectionNode => {
                meta = Some(child.clone());
            }
            SyntaxKind::ParameterMetadataSectionNode => {
                parameter_meta = Some(child.clone());
            }
            SyntaxKind::BoundDeclNode => {
                body.push(child.clone());
            }
            SyntaxKind::CommandSectionNode => {
                command = Some(child.clone());
            }
            SyntaxKind::OutputSectionNode => {
                output = Some(child.clone());
            }
            SyntaxKind::RequirementsSectionNode => {
                requirements = Some(child.clone());
            }
            SyntaxKind::RuntimeSectionNode => {
                runtime = Some(child.clone());
            }
            SyntaxKind::TaskHintsSectionNode => {
                hints = Some(child.clone());
            }
            SyntaxKind::CloseBrace => {
                close_brace = Some(child.clone());
            }
            _ => {
                unreachable!(
                    "unexpected child in task definition: {:?}",
                    child.element().kind()
                );
            }
        }
    }

    if let Some(meta) = meta {
        (&meta).write(stream);
        stream.blank_line();
    }

    if let Some(parameter_meta) = parameter_meta {
        (&parameter_meta).write(stream);
        stream.blank_line();
    }

    if let Some(input) = input {
        (&input).write(stream);
        stream.blank_line();
    }

    stream.blank_lines_allowed();
    for child in body {
        (&child).write(stream);
    }
    stream.blank_lines_allowed_between_comments();
    stream.blank_line();

    if let Some(command) = command {
        (&command).write(stream);
        stream.blank_line();
    }

    if let Some(output) = output {
        (&output).write(stream);
        stream.blank_line();
    }

    if let Some(requirements) = requirements {
        (&requirements).write(stream);
        stream.blank_line();
    } else if let Some(runtime) = runtime {
        (&runtime).write(stream);
        stream.blank_line();
    }

    if let Some(hints) = hints {
        (&hints).write(stream);
        stream.blank_line();
    }

    stream.trim_while(|t| matches!(t, PreToken::BlankLine | PreToken::Trivia(Trivia::BlankLine)));

    stream.decrement_indent();
    (&close_brace.expect("task close brace")).write(stream);
    stream.end_line();
}

/// Formats a [`CommandSection`](wdl_ast::v1::CommandSection).
pub fn format_command_section(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
    let mut children = element.children().expect("command section children");

    let command_keyword = children.next().expect("command keyword");
    assert!(command_keyword.element().kind() == SyntaxKind::CommandKeyword);
    (&command_keyword).write(stream);
    stream.end_word();

    let open_delimiter = children.next().expect("open delimiter");
    match open_delimiter.element().kind() {
        SyntaxKind::OpenBrace => {
            stream.push_literal_in_place_of_token(
                open_delimiter
                    .element()
                    .as_token()
                    .expect("open brace should be token"),
                "<<<".to_string(),
            );
        }
        SyntaxKind::OpenHeredoc => {
            (&open_delimiter).write(stream);
        }
        _ => {
            unreachable!(
                "unexpected open delimiter in command section: {:?}",
                open_delimiter.element().kind()
            );
        }
    }
    // Technically there's no trivia inside the command section,
    // so we don't want to increment indent here.
    // All the indentation should be handled by the command text itself.
    // TODO: multi-line placeholders need better formatting
    for child in children {
        match child.element().kind() {
            SyntaxKind::CloseBrace => {
                stream.push_literal_in_place_of_token(
                    child
                        .element()
                        .as_token()
                        .expect("close brace should be token"),
                    ">>>".to_string(),
                );
            }
            SyntaxKind::CloseHeredoc => {
                (&child).write(stream);
            }
            SyntaxKind::LiteralCommandText | SyntaxKind::PlaceholderNode => {
                (&child).write(stream);
            }
            _ => {
                unreachable!(
                    "unexpected child in command section: {:?}",
                    child.element().kind()
                );
            }
        }
    }
    stream.end_line();
}

/// Formats a [`RequirementsItem`](wdl_ast::v1::RequirementsItem).
pub fn format_requirements_item(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
    let mut children = element.children().expect("requirements item children");

    let name = children.next().expect("requirements item name");
    assert!(name.element().kind() == SyntaxKind::Ident);
    (&name).write(stream);

    let colon = children.next().expect("requirements item colon");
    assert!(colon.element().kind() == SyntaxKind::Colon);
    (&colon).write(stream);
    stream.end_word();

    let value = children.next().expect("requirements item value");
    (&value).write(stream);

    assert!(children.next().is_none());
}

/// Formats a [`RequirementsSection`](wdl_ast::v1::RequirementsSection).
pub fn format_requirements_section(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
    let mut children = element.children().expect("requirements section children");

    let requirements_keyword = children.next().expect("requirements keyword");
    assert!(requirements_keyword.element().kind() == SyntaxKind::RequirementsKeyword);
    (&requirements_keyword).write(stream);
    stream.end_word();

    let open_brace = children.next().expect("open brace");
    assert!(open_brace.element().kind() == SyntaxKind::OpenBrace);
    (&open_brace).write(stream);
    stream.increment_indent();

    let mut items = Vec::new();
    let mut close_brace = None;

    for child in children {
        match child.element().kind() {
            SyntaxKind::RequirementsItemNode => {
                items.push(child.clone());
            }
            SyntaxKind::CloseBrace => {
                close_brace = Some(child.clone());
            }
            _ => {
                unreachable!(
                    "unexpected child in requirements section: {:?}",
                    child.element().kind()
                );
            }
        }
    }

    for item in items {
        (&item).write(stream);
        stream.end_line();
    }

    stream.decrement_indent();
    (&close_brace.expect("requirements close brace")).write(stream);
    stream.end_line();
}

/// Formats a [`TaskHintsItem`](wdl_ast::v1::TaskHintsItem).
pub fn format_task_hints_item(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
    let mut children = element.children().expect("task hints item children");

    let name = children.next().expect("task hints item name");
    assert!(name.element().kind() == SyntaxKind::Ident);
    (&name).write(stream);

    let colon = children.next().expect("task hints item colon");
    assert!(colon.element().kind() == SyntaxKind::Colon);
    (&colon).write(stream);
    stream.end_word();

    let value = children.next().expect("task hints item value");
    (&value).write(stream);

    assert!(children.next().is_none());
}

/// Formats a [`RuntimeItem`](wdl_ast::v1::RuntimeItem).
pub fn format_runtime_item(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
    let mut children = element.children().expect("runtime item children");

    let name = children.next().expect("runtime item name");
    assert!(name.element().kind() == SyntaxKind::Ident);
    (&name).write(stream);

    let colon = children.next().expect("runtime item colon");
    assert!(colon.element().kind() == SyntaxKind::Colon);
    (&colon).write(stream);
    stream.end_word();

    let value = children.next().expect("runtime item value");
    (&value).write(stream);

    assert!(children.next().is_none());
}

/// Formats a [`RuntimeSection`](wdl_ast::v1::RuntimeSection).
pub fn format_runtime_section(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
    let mut children = element.children().expect("runtime section children");

    let runtime_keyword = children.next().expect("runtime keyword");
    assert!(runtime_keyword.element().kind() == SyntaxKind::RuntimeKeyword);
    (&runtime_keyword).write(stream);
    stream.end_word();

    let open_brace = children.next().expect("open brace");
    assert!(open_brace.element().kind() == SyntaxKind::OpenBrace);
    (&open_brace).write(stream);
    stream.increment_indent();

    let mut items = Vec::new();
    let mut close_brace = None;

    for child in children {
        match child.element().kind() {
            SyntaxKind::RuntimeItemNode => {
                items.push(child.clone());
            }
            SyntaxKind::CloseBrace => {
                close_brace = Some(child.clone());
            }
            _ => {
                unreachable!(
                    "unexpected child in runtime section: {:?}",
                    child.element().kind()
                );
            }
        }
    }

    for item in items {
        (&item).write(stream);
        stream.end_line();
    }

    stream.decrement_indent();
    (&close_brace.expect("runtime close brace")).write(stream);
    stream.end_line();
}

/// Formats a [`TaskHintsSection`](wdl_ast::v1::TaskHintsSection).
pub fn format_task_hints_section(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
    let mut children = element.children().expect("task hints section children");

    let hints_keyword = children.next().expect("hints keyword");
    assert!(hints_keyword.element().kind() == SyntaxKind::HintsKeyword);
    (&hints_keyword).write(stream);
    stream.end_word();

    let open_brace = children.next().expect("open brace");
    assert!(open_brace.element().kind() == SyntaxKind::OpenBrace);
    (&open_brace).write(stream);
    stream.increment_indent();

    let mut items = Vec::new();
    let mut close_brace = None;

    for child in children {
        match child.element().kind() {
            SyntaxKind::TaskHintsItemNode => {
                items.push(child.clone());
            }
            SyntaxKind::CloseBrace => {
                close_brace = Some(child.clone());
            }
            _ => {
                unreachable!(
                    "unexpected child in task hints section: {:?}",
                    child.element().kind()
                );
            }
        }
    }

    for item in items {
        (&item).write(stream);
        stream.end_line();
    }

    stream.decrement_indent();
    (&close_brace.expect("task hints close brace")).write(stream);
    stream.end_line();
}