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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#[derive(Eq, PartialEq, Clone, Debug)]
pub enum ServiceOrContainer {
Service(String),
Container(String),
}
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct VolumesFrom {
pub source: ServiceOrContainer,
pub permissions: VolumePermissions,
#[doc(hidden)]
pub _phantom: PhantomData<()>,
}
impl VolumesFrom {
pub fn service<S: Into<String>>(service: S) -> VolumesFrom {
VolumesFrom{source: ServiceOrContainer::Service(service.into()),
permissions: Default::default(),
_phantom: PhantomData,}
}
pub fn container<S: Into<String>>(container: S) -> VolumesFrom {
VolumesFrom{source: ServiceOrContainer::Container(container.into()),
permissions: Default::default(),
_phantom: PhantomData,}
}
}
impl_interpolatable_value! (VolumesFrom);
impl fmt::Display for VolumesFrom {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match &self.source {
&ServiceOrContainer::Service(ref name) =>
try!(write ! ( f , "{}" , name )),
&ServiceOrContainer::Container(ref name) =>
try!(write ! ( f , "container:{}" , name )),
}
if self.permissions != Default::default() {
try!(write ! ( f , ":{}" , self . permissions ))
}
Ok(())
}
}
impl FromStr for VolumesFrom {
type
Err
=
InvalidValueError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
lazy_static!{static ref FROM : Regex = Regex :: new (
"^(container:)?([^:]+)(?::([^:]+))?$" ) . unwrap ( ) ;
}
let caps =
try!(FROM . captures ( s ) . ok_or_else (
|| { InvalidValueError :: new ( "volumes_from" , s ) } ));
let name = caps.at(2).unwrap().to_owned();
let source =
if caps.at(1).is_some() {
ServiceOrContainer::Container(name)
} else { ServiceOrContainer::Service(name) };
let permissions =
match caps.at(3) {
None => Default::default(),
Some(permstr) => try!(FromStr :: from_str ( permstr )),
};
Ok(VolumesFrom{source: source,
permissions: permissions,
_phantom: PhantomData,})
}
}
#[test]
fn volumes_from_should_have_a_string_representation() {
let vf1 = VolumesFrom::service("foo");
let vf2 =
VolumesFrom{permissions:
VolumePermissions::ReadOnly,
..VolumesFrom::container("foo")};
let pairs = vec!(( vf1 , "foo" ) , ( vf2 , "container:foo:ro" ) ,);
for (vf, s) in pairs {
assert_eq!(vf . to_string ( ) , s);
assert_eq!(vf , VolumesFrom :: from_str ( s ) . unwrap ( ));
}
}