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
use serde::{Deserialize, Serialize};
use std::{
env::current_dir,
io,
path::{Path, PathBuf},
};
use strum_macros::{Display, EnumIter, EnumString};
#[derive(Display, EnumIter, EnumString, Debug, Clone, Copy, PartialEq, Deserialize, Serialize)]
pub enum ReproductionPathStyle {
#[strum(to_string = "Keep", serialize = "keep", serialize = "k")]
Keep,
#[strum(to_string = "Relative", serialize = "relative", serialize = "r")]
Relative,
#[strum(to_string = "Absolute", serialize = "absolute", serialize = "a")]
Absolute,
}
impl ReproductionPathStyle {
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()?),
}
}
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)
}
}
}