wdl_format/v1/
workflow.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
//! Formatting for workflows.

pub mod call;

use wdl_ast::SyntaxKind;

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

/// Formats a [`ConditionalStatement`](wdl_ast::v1::ConditionalStatement).
pub fn format_conditional_statement(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
    let mut children = element.children().expect("conditional statement children");

    let if_keyword = children.next().expect("if keyword");
    assert!(if_keyword.element().kind() == SyntaxKind::IfKeyword);
    (&if_keyword).write(stream);
    stream.end_word();

    let open_paren = children.next().expect("open paren");
    assert!(open_paren.element().kind() == SyntaxKind::OpenParen);
    (&open_paren).write(stream);

    for child in children.by_ref() {
        (&child).write(stream);
        if child.element().kind() == SyntaxKind::CloseParen {
            stream.end_word();
            break;
        }
    }

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

    for child in children {
        if child.element().kind() == SyntaxKind::CloseBrace {
            stream.decrement_indent();
        }
        (&child).write(stream);
    }
    stream.end_line();
}

/// Formats a [`ScatterStatement`](wdl_ast::v1::ScatterStatement).
pub fn format_scatter_statement(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
    let mut children = element.children().expect("scatter statement children");

    let scatter_keyword = children.next().expect("scatter keyword");
    assert!(scatter_keyword.element().kind() == SyntaxKind::ScatterKeyword);
    (&scatter_keyword).write(stream);
    stream.end_word();

    let open_paren = children.next().expect("open paren");
    assert!(open_paren.element().kind() == SyntaxKind::OpenParen);
    (&open_paren).write(stream);

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

    let in_keyword = children.next().expect("in keyword");
    assert!(in_keyword.element().kind() == SyntaxKind::InKeyword);
    (&in_keyword).write(stream);
    stream.end_word();

    for child in children.by_ref() {
        (&child).write(stream);
        if child.element().kind() == SyntaxKind::CloseParen {
            stream.end_word();
            break;
        }
    }

    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();

    for child in children {
        if child.element().kind() == SyntaxKind::CloseBrace {
            stream.decrement_indent();
        }
        (&child).write(stream);
    }
    stream.end_line();
}

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

    stream.blank_lines_allowed_between_comments();

    let workflow_keyword = children.next().expect("workflow keyword");
    assert!(workflow_keyword.element().kind() == SyntaxKind::WorkflowKeyword);
    (&workflow_keyword).write(stream);
    stream.end_word();

    let name = children.next().expect("workflow 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.increment_indent();

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

    for child in children {
        match child.element().kind() {
            SyntaxKind::MetadataSectionNode => {
                meta = Some(child.clone());
            }
            SyntaxKind::ParameterMetadataSectionNode => {
                parameter_meta = Some(child.clone());
            }
            SyntaxKind::InputSectionNode => {
                input = Some(child.clone());
            }
            SyntaxKind::BoundDeclNode => {
                body.push(child.clone());
            }
            SyntaxKind::CallStatementNode => {
                body.push(child.clone());
            }
            SyntaxKind::ConditionalStatementNode => {
                body.push(child.clone());
            }
            SyntaxKind::ScatterStatementNode => {
                body.push(child.clone());
            }
            SyntaxKind::OutputSectionNode => {
                output = Some(child.clone());
            }
            SyntaxKind::WorkflowHintsSectionNode => {
                hints = Some(child.clone());
            }
            SyntaxKind::CloseBrace => {
                close_brace = Some(child.clone());
            }
            _ => {
                unreachable!(
                    "unexpected child in workflow 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(output) = output {
        (&output).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("workflow close brace")).write(stream);
    stream.end_line();
}

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

    let open_bracket = children.next().expect("open bracket");
    assert!(open_bracket.element().kind() == SyntaxKind::OpenBracket);
    (&open_bracket).write(stream);
    stream.increment_indent();

    let mut items = Vec::new();
    let mut commas = Vec::new();
    let mut close_bracket = None;

    for child in children {
        match child.element().kind() {
            SyntaxKind::Comma => {
                commas.push(child.clone());
            }
            SyntaxKind::CloseBracket => {
                close_bracket = Some(child.clone());
            }
            _ => {
                items.push(child.clone());
            }
        }
    }

    let mut commas = commas.into_iter();
    for item in items {
        (&item).write(stream);
        if let Some(comma) = commas.next() {
            (&comma).write(stream);
        } else {
            stream.push_literal(",".to_string(), SyntaxKind::Comma);
        }
        stream.end_line();
    }

    stream.decrement_indent();
    (&close_bracket.expect("workflow hints array close bracket")).write(stream);
}

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

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

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

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

    stream.end_line();

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

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

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

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

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

    stream.end_line();

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

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

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

    for child in children {
        if child.element().kind() == SyntaxKind::CloseBrace {
            stream.decrement_indent();
        }
        (&child).write(stream);
        stream.end_line();
    }
}

/// Formats a [`WorkflowHintsSection`](wdl_ast::v1::WorkflowHintsSection).
pub fn format_workflow_hints_section(element: &FormatElement, stream: &mut TokenStream<PreToken>) {
    let mut children = element.children().expect("workflow 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();

    for child in children {
        if child.element().kind() == SyntaxKind::CloseBrace {
            stream.decrement_indent();
        }
        (&child).write(stream);
        stream.end_line();
    }
}