wdl-cli 0.3.1

Facilities for building command-line tools using the `wdl` crates
Documentation
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! Inputs parsed in from the command line.

use std::ops::Deref;
use std::ops::DerefMut;
use std::path::Path;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::LazyLock;

use indexmap::IndexMap;
use regex::Regex;
use serde_json::Value;
use thiserror::Error;
use wdl_analysis::Document;
use wdl_engine::Inputs as EngineInputs;

pub mod file;
pub mod origin_paths;

pub use file::InputFile;
pub use origin_paths::OriginPaths;

/// A regex that matches a valid identifier.
///
/// This is useful when recognizing whether a key provided on the command line
/// is a valid identifier.
static IDENTIFIER_REGEX: LazyLock<Regex> = LazyLock::new(|| {
    // SAFETY: this is checked statically with tests to always unwrap.
    Regex::new(r"^([a-zA-Z][a-zA-Z0-9_.]*)$").unwrap()
});

/// If a value in a key-value pair passed in on the command line cannot be
/// resolved to a WDL type, this regex is compared to the value.
///
/// If the regex matches, we assume the value is a string.
static ASSUME_STRING_REGEX: LazyLock<Regex> = LazyLock::new(|| {
    // SAFETY: this is checked statically with tests to always unwrap.
    Regex::new(r"^[\w /~.]*$").unwrap()
});

/// An error related to inputs.
#[derive(Error, Debug)]
pub enum Error {
    /// A file error.
    #[error(transparent)]
    File(#[from] file::Error),

    /// A file was specified on the command line but not found.
    #[error("file `{0}` was not found")]
    FileNotFound(PathBuf),

    /// Encountered an invalid key-value pair.
    #[error("invalid key-value pair `{pair}`: {reason}")]
    InvalidPair {
        /// The string-value of the pair.
        pair: String,

        /// The reason the pair was not valid.
        reason: String,
    },

    /// A deserialization error.
    #[error("unable to deserialize `{0}` as a valid WDL value")]
    Deserialize(String),
}

/// A [`Result`](std::result::Result) with an [`Error`].
pub type Result<T> = std::result::Result<T, Error>;

/// An input parsed from the command line.
#[derive(Clone, Debug)]
pub enum Input {
    /// A file.
    File(
        /// The path to the file.
        ///
        /// If this input is successfully created, the input is guaranteed to
        /// exist at the time the inputs were processed.
        PathBuf,
    ),

    /// A key-value pair representing an input.
    Pair {
        /// The key.
        key: String,

        /// The value.
        value: Value,
    },
}

impl Input {
    /// Attempts to return a reference to the inner [`Path`].
    ///
    /// * If the input is a [`Input::File`], a reference to the inner path is
    ///   returned wrapped in [`Some`].
    /// * Otherwise, [`None`] is returned.
    pub fn as_file(&self) -> Option<&Path> {
        match self {
            Input::File(p) => Some(p.as_path()),
            _ => None,
        }
    }

    /// Consumes `self` and attempts to return the inner [`PathBuf`].
    ///
    /// * If the input is a [`Input::File`], the inner path buffer is returned
    ///   wrapped in [`Some`].
    /// * Otherwise, [`None`] is returned.
    pub fn into_file(self) -> Option<PathBuf> {
        match self {
            Input::File(p) => Some(p),
            _ => None,
        }
    }

    /// Consumes `self` and returns the inner [`PathBuf`].
    ///
    /// # Panics
    ///
    /// If the input is not a [`Input::File`].
    pub fn unwrap_file(self) -> PathBuf {
        match self {
            Input::File(p) => p,
            v => panic!("{v:?} is not an `Input::File`"),
        }
    }

    /// Attempts to return a reference to the inner key-value pair.
    ///
    /// * If the input is a [`Input::Pair`], a reference to the inner key and
    ///   value is returned wrapped in [`Some`].
    /// * Otherwise, [`None`] is returned.
    pub fn as_pair(&self) -> Option<(&str, &Value)> {
        match self {
            Input::Pair { key, value } => Some((key.as_str(), value)),
            _ => None,
        }
    }

    /// Consumes `self` and attempts to return the inner key-value pair.
    ///
    /// * If the input is a [`Input::Pair`], the inner key-value pair is
    ///   returned wrapped in [`Some`].
    /// * Otherwise, [`None`] is returned.
    pub fn into_pair(self) -> Option<(String, Value)> {
        match self {
            Input::Pair { key, value } => Some((key, value)),
            _ => None,
        }
    }

    /// Consumes `self` and returns the inner key-value pair.
    ///
    /// # Panics
    ///
    /// If the input is not a [`Input::Pair`].
    pub fn unwrap_pair(self) -> (String, Value) {
        match self {
            Input::Pair { key, value } => (key, value),
            v => panic!("{v:?} is not an `Input::Pair`"),
        }
    }
}

impl FromStr for Input {
    type Err = Error;

    fn from_str(s: &str) -> std::result::Result<Self, Error> {
        match s.split_once("=") {
            Some((key, value)) => {
                if !IDENTIFIER_REGEX.is_match(key) {
                    return Err(Error::InvalidPair {
                        pair: s.to_string(),
                        reason: format!(
                            "key `{}` did not match the identifier regex (`{}`)",
                            key,
                            IDENTIFIER_REGEX.as_str()
                        ),
                    });
                }

                let value = serde_json::from_str(value).or_else(|_| {
                    if ASSUME_STRING_REGEX.is_match(value) {
                        Ok(Value::String(value.to_owned()))
                    } else {
                        Err(Error::Deserialize(value.to_owned()))
                    }
                })?;

                Ok(Input::Pair {
                    key: key.to_owned(),
                    value,
                })
            }
            None => {
                let path = PathBuf::from(s);

                if !path.exists() {
                    return Err(Error::FileNotFound(path));
                }

                Ok(Input::File(path))
            }
        }
    }
}

/// The inner type for inputs (for convenience).
type InputsInner = IndexMap<String, (PathBuf, Value)>;

/// A set of inputs parsed from the command line and compiled on top of one
/// another.
#[derive(Clone, Debug, Default)]
pub struct Inputs(InputsInner);

impl Inputs {
    /// Adds an input read from the command line.
    fn add_input(&mut self, input: &str) -> Result<()> {
        match input.parse::<Input>()? {
            Input::File(path) => {
                let inputs = InputFile::read(&path).map_err(Error::File)?;
                self.extend(inputs.into_inner());
            }
            Input::Pair { key, value } => {
                // SAFETY: we expect that the current working directory is
                // always available for the platforms that `wdl` will run
                // within.
                let cwd = std::env::current_dir().unwrap();
                self.insert(key, (cwd, value));
            }
        };

        Ok(())
    }

    /// Attempts to coalesce a set of inputs into an [`Inputs`].
    pub fn coalesce<T, V>(iter: T) -> Result<Self>
    where
        T: IntoIterator<Item = V>,
        V: AsRef<str>,
    {
        let mut inputs = Inputs::default();

        for input in iter {
            inputs.add_input(input.as_ref())?;
        }

        Ok(inputs)
    }

    /// Consumes `self` and returns the inner index map.
    pub fn into_inner(self) -> InputsInner {
        self.0
    }

    /// Converts a set of inputs to a set of engine inputs.
    ///
    /// Returns `Ok(Some(_))` if the inputs are not empty.
    ///
    /// Returns `Ok(None)` if the inputs are empty.
    ///
    /// When the inputs are not empty, the return type contained in `Some(_)` is
    /// a tuple of,
    ///
    /// - the name of the callee (the name of the task or workflow being run),
    /// - the transformed engine inputs, and
    /// - a map containing the origin path for each provided input key.
    pub fn into_engine_inputs(
        self,
        document: &Document,
    ) -> anyhow::Result<Option<(String, EngineInputs, OriginPaths)>> {
        let (origins, values) = self.0.into_iter().fold(
            (IndexMap::new(), serde_json::Map::new()),
            |(mut origins, mut values), (key, (origin, value))| {
                origins.insert(key.clone(), origin);
                values.insert(key, value);
                (origins, values)
            },
        );

        let result = EngineInputs::parse_object(document, values)?;

        Ok(result.map(|(callee_name, inputs)| {
            let callee_prefix = format!("{callee_name}.");

            let origins = origins
                .into_iter()
                .map(|(key, path)| {
                    if let Some(key) = key.strip_prefix(&callee_prefix) {
                        (key.to_owned(), path)
                    } else {
                        (key, path)
                    }
                })
                .collect::<IndexMap<String, PathBuf>>();

            (callee_name, inputs, OriginPaths::from(origins))
        }))
    }
}

impl Deref for Inputs {
    type Target = InputsInner;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Inputs {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn identifier_regex() {
        assert!(IDENTIFIER_REGEX.is_match("here_is_an.identifier"));
        assert!(!IDENTIFIER_REGEX.is_match("here is not an identifier"));
    }

    #[test]
    fn assume_string_regex() {
        // Matches.
        assert!(ASSUME_STRING_REGEX.is_match(""));
        assert!(ASSUME_STRING_REGEX.is_match("fooBAR082"));
        assert!(ASSUME_STRING_REGEX.is_match("foo bar baz"));

        // Non-matches.
        assert!(!ASSUME_STRING_REGEX.is_match("[1, a]"));
    }

    #[test]
    fn file_parsing() {
        // A valid JSON file path.
        let input = "./tests/fixtures/inputs_one.json".parse::<Input>().unwrap();
        assert!(matches!(
            input,
            Input::File(path) if path.to_str().unwrap() == "./tests/fixtures/inputs_one.json"
        ));

        // A valid YAML file path.
        let input = "./tests/fixtures/inputs_three.yml"
            .parse::<Input>()
            .unwrap();
        assert!(matches!(
            input,
            Input::File(path) if path.to_str().unwrap() == "./tests/fixtures/inputs_three.yml"
        ));

        // A missing file path.
        let err = "./tests/fixtures/missing.json"
            .parse::<Input>()
            .unwrap_err();
        assert!(matches!(
            err,
            Error::FileNotFound(path) if path.to_str().unwrap() == "./tests/fixtures/missing.json"
        ));
    }

    #[test]
    fn key_value_pair_parsing() {
        // A standard key-value pair.
        let input = r#"foo="bar""#.parse::<Input>().unwrap();
        let (key, value) = input.unwrap_pair();
        assert_eq!(key, "foo");
        assert_eq!(value.as_str().unwrap(), "bar");

        // A standard key-value pair.
        let input = r#"foo.bar_baz_quux="qil""#.parse::<Input>().unwrap();
        let (key, value) = input.unwrap_pair();
        assert_eq!(key, "foo.bar_baz_quux");
        assert_eq!(value.as_str().unwrap(), "qil");

        // An invalid identifier for the key.
        let err = r#"foo$="bar""#.parse::<Input>().unwrap_err();
        assert!(matches!(
                err,
                Error::InvalidPair {
                    pair,
                    reason
                } if pair == r#"foo$="bar""# &&
                reason == r"key `foo$` did not match the identifier regex (`^([a-zA-Z][a-zA-Z0-9_.]*)$`)"));

        // A value that is valid despite that value not being valid as a key.
        let input = r#"foo="bar$""#.parse::<Input>().unwrap();
        let (key, value) = input.unwrap_pair();
        assert_eq!(key, "foo");
        assert_eq!(value.as_str().unwrap(), "bar$");
    }

    #[test]
    fn coalesce() {
        // Helper functions.
        fn check_string_value(inputs: &Inputs, key: &str, value: &str) {
            let (_, input) = inputs.get(key).unwrap();
            assert_eq!(input.as_str().unwrap(), value);
        }

        fn check_float_value(inputs: &Inputs, key: &str, value: f64) {
            let (_, input) = inputs.get(key).unwrap();
            assert_eq!(input.as_f64().unwrap(), value);
        }

        fn check_boolean_value(inputs: &Inputs, key: &str, value: bool) {
            let (_, input) = inputs.get(key).unwrap();
            assert_eq!(input.as_bool().unwrap(), value);
        }

        fn check_integer_value(inputs: &Inputs, key: &str, value: i64) {
            let (_, input) = inputs.get(key).unwrap();
            assert_eq!(input.as_i64().unwrap(), value);
        }

        // The standard coalescing order.
        let inputs = Inputs::coalesce([
            "./tests/fixtures/inputs_one.json",
            "./tests/fixtures/inputs_two.json",
            "./tests/fixtures/inputs_three.yml",
        ])
        .unwrap();

        assert_eq!(inputs.len(), 5);
        check_string_value(&inputs, "foo", "bar");
        check_float_value(&inputs, "baz", 128.0);
        check_string_value(&inputs, "quux", "qil");
        check_string_value(&inputs, "new.key", "foobarbaz");
        check_string_value(&inputs, "new_two.key", "bazbarfoo");

        // The opposite coalescing order.
        let inputs = Inputs::coalesce([
            "./tests/fixtures/inputs_three.yml",
            "./tests/fixtures/inputs_two.json",
            "./tests/fixtures/inputs_one.json",
        ])
        .unwrap();

        assert_eq!(inputs.len(), 5);
        check_string_value(&inputs, "foo", "bar");
        check_float_value(&inputs, "baz", 42.0);
        check_string_value(&inputs, "quux", "qil");
        check_string_value(&inputs, "new.key", "foobarbaz");
        check_string_value(&inputs, "new_two.key", "bazbarfoo");

        // An example with some random key-value pairs thrown in.
        let inputs = Inputs::coalesce([
            r#"sandwich=-100"#,
            "./tests/fixtures/inputs_one.json",
            "./tests/fixtures/inputs_two.json",
            r#"quux="jacks""#,
            "./tests/fixtures/inputs_three.yml",
            r#"baz=false"#,
        ])
        .unwrap();

        assert_eq!(inputs.len(), 6);
        check_string_value(&inputs, "foo", "bar");
        check_boolean_value(&inputs, "baz", false);
        check_string_value(&inputs, "quux", "jacks");
        check_string_value(&inputs, "new.key", "foobarbaz");
        check_string_value(&inputs, "new_two.key", "bazbarfoo");
        check_integer_value(&inputs, "sandwich", -100);

        // An invalid key-value pair.
        let error =
            Inputs::coalesce(["./tests/fixtures/inputs_one.json", "foo=baz#bar"]).unwrap_err();
        assert!(matches!(
            error,
            Error::Deserialize(value) if value == "baz#bar"
        ));

        // A missing file.
        let error = Inputs::coalesce([
            "./tests/fixtures/inputs_one.json",
            "./tests/fixtures/inputs_two.json",
            "./tests/fixtures/inputs_three.yml",
            "./tests/fixtures/missing.json",
        ])
        .unwrap_err();
        assert!(matches!(
                error,
                Error::FileNotFound(path) if path.to_str().unwrap() == "./tests/fixtures/missing.json"));
    }

    #[test]
    fn multiple_equal_signs() {
        let (key, value) = r#"foo="bar=baz""#.parse::<Input>().unwrap().unwrap_pair();
        assert_eq!(key, "foo");
        assert_eq!(value.as_str().unwrap(), "bar=baz");
    }
}