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
use named_type::NamedType;
use named_type_derive::*;
use serde::{Deserialize, Serialize};
use std::{
    env::current_dir,
    io,
    path::{Path, PathBuf},
};
use strum_macros::{Display, EnumIter, EnumString};

/// Represents the style for storing paths in a reproduction file.
#[derive(
    Display, EnumIter, EnumString, NamedType, Debug, Clone, Copy, PartialEq, Deserialize, Serialize,
)]
pub enum ReproductionPathStyle {
    /// Specifies that paths should be saved the same way they were specified
    /// on the command line.
    #[strum(to_string = "Keep", serialize = "keep", serialize = "k")]
    Keep,

    /// Specifies that all paths should be saved relative to DQCsim's working
    /// directory.
    #[strum(to_string = "Relative", serialize = "relative", serialize = "r")]
    Relative,

    /// Specifies that all paths should be saved canonically, i.e. relative to
    /// the root directory.
    #[strum(to_string = "Absolute", serialize = "absolute", serialize = "a")]
    Absolute,
}

impl ReproductionPathStyle {
    /// Converts a path as specified by the underlying `ReproductionPathStyle`.
    ///
    /// Calls `std::env::current_dir()` if the style is `Relative` to get the
    /// base for the relative path.
    pub fn convert_path(self, path: &Path) -> io::Result<PathBuf> {
        match self {
            ReproductionPathStyle::Keep => Ok(path.into()),
            ReproductionPathStyle::Relative => {
                let workdir = current_dir()?;
                let path =
                    pathdiff::diff_paths(&path.canonicalize()?, &workdir).ok_or_else(|| {
                        io::Error::new(io::ErrorKind::NotFound, "Cannot make path relative")
                    })?;
                if path.as_os_str().is_empty() {
                    Ok(PathBuf::from("."))
                } else {
                    Ok(path)
                }
            }
            ReproductionPathStyle::Absolute => Ok(path.canonicalize()?),
        }
    }

    /// Convenience function that applies `convert_path()` on the contents of
    /// an `Option`.
    pub fn convert_path_option(self, path: &Option<PathBuf>) -> io::Result<Option<PathBuf>> {
        if let Some(path) = path.as_ref() {
            Ok(Some(self.convert_path(path)?))
        } else {
            Ok(None)
        }
    }
}