wdl_cli/inputs/
origin_paths.rs

1//! The set of origin paths for each read input argument.
2//!
3//! An origin path is the associated directory for which each parsed command
4//! line argument is relative to. So, for example, arguments read in from files
5//! are relative to the directory the file lives within, whereas arguments
6//! provided on the command line are relative to the current working directory.
7//!
8//! This mechanism ensures that, when files or directories are specified as
9//! inputs, we know the prefix to join to those paths to resolve the final
10//! location of each path.
11
12use std::path::Path;
13use std::path::PathBuf;
14
15use indexmap::IndexMap;
16
17/// An associated set of path origins for a set of input keys.
18///
19/// This is useful when, for example, resolving all paths within an
20/// [`Inputs`](super::Inputs) to be relative to the input file from whence they
21/// originated.
22#[derive(Debug)]
23pub enum OriginPaths {
24    /// A single origin path for all inputs.
25    Single(PathBuf),
26    /// A dynamic mapping of input keys to origin paths.
27    Map(IndexMap<String, PathBuf>),
28}
29
30impl OriginPaths {
31    /// Attempts to retrieve the origin path for an input key.
32    pub fn get(&self, key: &str) -> Option<&Path> {
33        match self {
34            OriginPaths::Single(path) => Some(path.as_path()),
35            OriginPaths::Map(paths) => paths.get(key).map(|p| p.as_path()),
36        }
37    }
38}
39
40impl From<PathBuf> for OriginPaths {
41    fn from(value: PathBuf) -> Self {
42        Self::Single(value)
43    }
44}
45
46impl From<IndexMap<String, PathBuf>> for OriginPaths {
47    fn from(value: IndexMap<String, PathBuf>) -> Self {
48        Self::Map(value)
49    }
50}