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
use anyhow::{bail, Result};
use std::env;
use std::path::PathBuf;
pub fn wasmer_should_print_color() -> bool {
env::var("WASMER_COLOR")
.ok()
.and_then(|inner| inner.parse::<bool>().ok())
.unwrap_or_else(|| atty::is(atty::Stream::Stdout))
}
fn retrieve_alias_pathbuf(alias: &str, real_dir: &str) -> Result<(String, PathBuf)> {
let pb = PathBuf::from(&real_dir);
if let Ok(pb_metadata) = pb.metadata() {
if !pb_metadata.is_dir() {
bail!("\"{}\" exists, but it is not a directory", &real_dir);
}
} else {
bail!("Directory \"{}\" does not exist", &real_dir);
}
Ok((alias.to_string(), pb))
}
pub fn parse_mapdir(entry: &str) -> Result<(String, PathBuf)> {
if let [alias, real_dir] = entry.split("::").collect::<Vec<&str>>()[..] {
retrieve_alias_pathbuf(alias, real_dir)
}
else if let [alias, real_dir] = entry.split(':').collect::<Vec<&str>>()[..] {
retrieve_alias_pathbuf(alias, real_dir)
} else {
bail!(
"Directory mappings must consist of two paths separate by a `::` or `:`. Found {}",
&entry
)
}
}
pub fn parse_envvar(entry: &str) -> Result<(String, String)> {
let entry = entry.trim();
match entry.find('=') {
None => bail!(
"Environment variable must be of the form `<name>=<value>`; found `{}`",
&entry
),
Some(0) => bail!(
"Environment variable is not well formed, the `name` is missing in `<name>=<value>`; got `{}`",
&entry
),
Some(position) if position == entry.len() - 1 => bail!(
"Environment variable is not well formed, the `value` is missing in `<name>=<value>`; got `{}`",
&entry
),
Some(position) => Ok((entry[..position].into(), entry[position + 1..].into())),
}
}
#[cfg(test)]
mod tests {
use super::parse_envvar;
#[test]
fn test_parse_envvar() {
assert_eq!(
parse_envvar("A").unwrap_err().to_string(),
"Environment variable must be of the form `<name>=<value>`; found `A`"
);
assert_eq!(
parse_envvar("=A").unwrap_err().to_string(),
"Environment variable is not well formed, the `name` is missing in `<name>=<value>`; got `=A`"
);
assert_eq!(
parse_envvar("A=").unwrap_err().to_string(),
"Environment variable is not well formed, the `value` is missing in `<name>=<value>`; got `A=`"
);
assert_eq!(parse_envvar("A=B").unwrap(), ("A".into(), "B".into()));
assert_eq!(parse_envvar(" A=B\t").unwrap(), ("A".into(), "B".into()));
assert_eq!(
parse_envvar("A=B=C=D").unwrap(),
("A".into(), "B=C=D".into())
);
}
}