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
#[derive(Eq, PartialEq, Clone, Debug)]
pub enum Context {
Dir(PathBuf),
GitUrl(GitUrl),
}
impl Context {
pub fn new<S: AsRef<str>>(s: S) -> Context {
let s_ref = s.as_ref();
if GitUrl::should_treat_as_url(s_ref) {
Context::GitUrl(GitUrl::new(s_ref.to_owned()).unwrap())
} else { Context::Dir(Path::new(&s_ref).to_owned()) }
}
}
impl_interpolatable_value! (Context);
impl FromStr for Context {
type
Err
=
Void;
fn from_str(s: &str) -> Result<Self, Self::Err> { Ok(Context::new(s)) }
}
impl fmt::Display for Context {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self {
&Context::Dir(ref path) => write!(f , "{}" , path . display ( )),
&Context::GitUrl(ref url) => write!(f , "{}" , url),
}
}
}
#[test]
fn context_may_contain_git_urls() {
let urls =
vec!("git://github.com/docker/docker" ,
"git@github.com:docker/docker.git" ,
"git@bitbucket.org:atlassianlabs/atlassian-docker.git" ,
"https://github.com/docker/docker.git" ,
"http://github.com/docker/docker.git" ,
"github.com/docker/docker.git");
for url in urls {
let context: Context = FromStr::from_str(url).unwrap();
assert_eq!(context , Context :: GitUrl (
GitUrl :: new ( url . to_string ( ) ) . unwrap ( ) ));
assert_eq!(context . to_string ( ) , url);
}
}
#[test]
fn context_may_contain_dir_paths() {
let paths = vec!("." , "./foo" , "./foo/bar/");
for path in paths {
let context: Context = FromStr::from_str(path).unwrap();
assert_eq!(context , Context :: Dir (
Path :: new ( path ) . to_owned ( ) ));
assert_eq!(context . to_string ( ) , path);
}
}