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
//! Scatters.

use grammar::v1::Rule;
use nonempty::NonEmpty;
use pest::iterators::Pair;
use wdl_grammar as grammar;
use wdl_macros::check_node;
use wdl_macros::dive_one;
use wdl_macros::unwrap_one;

use crate::v1::document::expression;
use crate::v1::document::identifier::singular;
use crate::v1::document::identifier::singular::Identifier;
use crate::v1::document::workflow::execution::statement;
use crate::v1::document::workflow::execution::Statement;
use crate::v1::document::Expression;

mod builder;

pub use builder::Builder;

/// An error related to [`Scatter`].
#[derive(Debug)]
pub enum Error {
    /// A builder error.
    Builder(builder::Error),

    /// An expression error.
    Expression(expression::Error),

    /// An identifier error.
    Identifier(singular::Error),

    /// A workflow execution statement error.
    Statement(statement::Error),
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::Builder(err) => write!(f, "builder error: {err}"),
            Error::Expression(err) => write!(f, "expression error: {err}"),
            Error::Identifier(err) => write!(f, "identifier error: {err}"),
            Error::Statement(err) => {
                write!(f, "workflow execution statement error: {err}")
            }
        }
    }
}

impl std::error::Error for Error {}

/// A scatter statement.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Scatter {
    /// The entities being scattered over.
    iterable: Expression,

    /// The workflow execution statements for each entity.
    statements: Option<NonEmpty<Box<Statement>>>,

    /// The variable name for each entity.
    variable: Identifier,
}

impl Scatter {
    /// Gets the iterables from the [`Scatter`] by reference.
    ///
    /// # Examples
    ///
    /// ```
    /// use ast::v1::document::expression::Literal;
    /// use ast::v1::document::identifier::singular;
    /// use ast::v1::document::workflow::execution::statement::call;
    /// use ast::v1::document::workflow::execution::statement::scatter::Builder;
    /// use ast::v1::document::workflow::execution::Statement;
    /// use ast::v1::document::Expression;
    /// use ast::v1::document::Identifier;
    /// use wdl_ast as ast;
    ///
    /// let variable = singular::Identifier::try_from("entity")?;
    /// let iterable = Expression::Literal(Literal::Identifier(singular::Identifier::try_from(
    ///     "entities",
    /// )?));
    /// let statement = Statement::Call(
    ///     call::Builder::default()
    ///         .name(Identifier::from(singular::Identifier::try_from(
    ///             "hello_world",
    ///         )?))?
    ///         .try_build()?,
    /// );
    ///
    /// let scatter = Builder::default()
    ///     .variable(variable)?
    ///     .iterable(iterable.clone())?
    ///     .push_workflow_execution_statement(statement)
    ///     .try_build()?;
    ///
    /// assert_eq!(scatter.iterable(), &iterable);
    ///
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn iterable(&self) -> &Expression {
        &self.iterable
    }

    /// Gets the [workflow execution statement(s)](Statement) from this
    /// [`Scatter`] by reference (if they exist).
    ///
    /// # Examples
    ///
    /// ```
    /// use ast::v1::document::expression::Literal;
    /// use ast::v1::document::identifier::singular;
    /// use ast::v1::document::workflow::execution::statement::call;
    /// use ast::v1::document::workflow::execution::statement::scatter::Builder;
    /// use ast::v1::document::workflow::execution::Statement;
    /// use ast::v1::document::Expression;
    /// use ast::v1::document::Identifier;
    /// use wdl_ast as ast;
    ///
    /// let variable = singular::Identifier::try_from("entity")?;
    /// let iterable = Expression::Literal(Literal::Identifier(singular::Identifier::try_from(
    ///     "entities",
    /// )?));
    /// let statement = Statement::Call(
    ///     call::Builder::default()
    ///         .name(Identifier::from(singular::Identifier::try_from(
    ///             "hello_world",
    ///         )?))?
    ///         .try_build()?,
    /// );
    ///
    /// let scatter = Builder::default()
    ///     .variable(variable)?
    ///     .iterable(iterable)?
    ///     .push_workflow_execution_statement(statement.clone())
    ///     .try_build()?;
    ///
    /// assert_eq!(scatter.statements().unwrap().len(), 1);
    /// assert_eq!(
    ///     scatter
    ///         .statements()
    ///         .unwrap()
    ///         .iter()
    ///         .next()
    ///         .unwrap()
    ///         .as_ref(),
    ///     &statement
    /// );
    ///
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn statements(&self) -> Option<&NonEmpty<Box<Statement>>> {
        self.statements.as_ref()
    }

    /// Gets the variable for this [`Scatter`] by reference.
    ///
    /// # Examples
    ///
    /// ```
    /// use ast::v1::document::expression::Literal;
    /// use ast::v1::document::identifier::singular;
    /// use ast::v1::document::workflow::execution::statement::call;
    /// use ast::v1::document::workflow::execution::statement::scatter::Builder;
    /// use ast::v1::document::workflow::execution::Statement;
    /// use ast::v1::document::Expression;
    /// use ast::v1::document::Identifier;
    /// use wdl_ast as ast;
    ///
    /// let variable = singular::Identifier::try_from("entity")?;
    /// let iterable = Expression::Literal(Literal::Identifier(singular::Identifier::try_from(
    ///     "entities",
    /// )?));
    /// let statement = Statement::Call(
    ///     call::Builder::default()
    ///         .name(Identifier::from(singular::Identifier::try_from(
    ///             "hello_world",
    ///         )?))?
    ///         .try_build()?,
    /// );
    ///
    /// let scatter = Builder::default()
    ///     .variable(variable.clone())?
    ///     .iterable(iterable)?
    ///     .push_workflow_execution_statement(statement)
    ///     .try_build()?;
    ///
    /// assert_eq!(scatter.variable(), &variable);
    ///
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn variable(&self) -> &Identifier {
        &self.variable
    }
}

impl TryFrom<Pair<'_, grammar::v1::Rule>> for Scatter {
    type Error = Error;

    fn try_from(node: Pair<'_, grammar::v1::Rule>) -> Result<Self, Self::Error> {
        check_node!(node, workflow_scatter);
        let mut builder = Builder::default();

        for node in node.into_inner() {
            match node.as_rule() {
                Rule::workflow_scatter_iteration_statement => {
                    // TODO: a clone is required here because Pest's `FlatPairs`
                    // type does not support creating an iterator without taking
                    // ownership (at the time of writing). This can be made
                    // better with a PR to Pest.
                    let variable_node = dive_one!(
                        node.clone(),
                        workflow_scatter_iteration_statement_variable,
                        workflow_scatter_iteration_statement
                    );
                    let variable_node =
                        unwrap_one!(variable_node, workflow_scatter_iteration_statement_variable);
                    let variable =
                        singular::Identifier::try_from(variable_node).map_err(Error::Identifier)?;

                    let iterable_node = dive_one!(
                        node.clone(),
                        workflow_scatter_iteration_statement_iterable,
                        workflow_scatter_iteration_statement
                    );
                    let iterable_node =
                        unwrap_one!(iterable_node, workflow_scatter_iteration_statement_iterable);
                    let iterable =
                        Expression::try_from(iterable_node).map_err(Error::Expression)?;

                    builder = builder.variable(variable).map_err(Error::Builder)?;
                    builder = builder.iterable(iterable).map_err(Error::Builder)?;
                }
                Rule::workflow_execution_statement => {
                    let statement = Statement::try_from(node).map_err(Error::Statement)?;
                    builder = builder.push_workflow_execution_statement(statement);
                }
                Rule::WHITESPACE => {}
                Rule::COMMENT => {}
                rule => unreachable!("scatter should not contain {:?}", rule),
            }
        }

        builder.try_build().map_err(Error::Builder)
    }
}

#[cfg(test)]
mod tests {
    use wdl_macros::test::create_invalid_node_test;
    use wdl_macros::test::valid_node;

    use super::*;
    use crate::v1::document::expression::Literal;
    use crate::v1::document::workflow::execution::statement::call::body::Value;

    #[test]
    fn it_parses_from_a_supported_node_type() {
        let scatter = valid_node!(
            r#"scatter (file in files) {
                call read {
                    input: file, foo=bar
                }

                call external.write {
                    input: file, baz=false
                }
            }"#,
            workflow_scatter,
            Scatter
        );

        assert_eq!(scatter.variable().as_str(), "file");
        assert_eq!(
            scatter.iterable(),
            &Expression::Literal(Literal::Identifier(
                singular::Identifier::try_from("files").unwrap()
            ))
        );

        let statements = scatter.statements().unwrap();
        assert_eq!(statements.len(), 2);

        let mut statements = statements.into_iter();

        let first_call = match statements.next().unwrap().as_ref() {
            Statement::Call(call) => call,
            _ => unreachable!(),
        };
        assert_eq!(first_call.name().to_string(), "read");
        assert_eq!(
            first_call.body().unwrap().get("file"),
            Some(&Value::ImplicitBinding)
        );
        assert_eq!(
            first_call.body().unwrap().get("foo"),
            Some(&Value::Expression(Expression::Literal(
                Literal::Identifier(singular::Identifier::try_from("bar").unwrap())
            )))
        );
        assert_eq!(first_call.body().unwrap().get("does_not_exist"), None);

        let second_call = match statements.next().unwrap().as_ref() {
            Statement::Call(call) => call,
            _ => unreachable!(),
        };
        assert_eq!(second_call.name().to_string(), "external.write");
        assert_eq!(
            second_call.body().unwrap().get("file"),
            Some(&Value::ImplicitBinding)
        );
        assert_eq!(
            second_call.body().unwrap().get("baz"),
            Some(&Value::Expression(Expression::Literal(Literal::Boolean(
                false
            ))))
        );
        assert_eq!(second_call.body().unwrap().get("does_not_exist"), None);
    }

    create_invalid_node_test!(
        "version 1.1\n\ntask hello { command <<<>>> }",
        document,
        workflow_scatter,
        Scatter,
        it_fails_to_parse_from_an_unsupported_node_type
    );
}