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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#[derive(Eq, PartialEq, Clone, Debug)]
pub enum HostVolume {
Path(PathBuf),
UserRelativePath(PathBuf),
Name(String),
}
impl fmt::Display for HostVolume {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self {
&HostVolume::Path(ref path) => {
let p = try!(path . to_str ( ) . ok_or ( fmt :: Error ));
if path.is_absolute() {
write!(f , "{}" , p)
} else if p.starts_with("./") || p.starts_with("../") {
write!(f , "{}" , p)
} else {
write!(f , "./{}" , p)
}
}
&HostVolume::UserRelativePath(ref path) => {
let p = try!(path . to_str ( ) . ok_or ( fmt :: Error ));
if path.is_absolute() { return Err(fmt::Error); }
write!(f , "~/{}" , p)
}
&HostVolume::Name(ref name) => { write!(f , "{}" , name) }
}
}
}
impl FromStr for HostVolume {
type
Err
=
InvalidValueError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
lazy_static!{static ref HOST_VOLUME : Regex = Regex :: new (
r#"^(\.{0,2}/.*)|~/(.+)|([^./~].*)$"# ) . unwrap ( ) ;
}
let caps =
try!(HOST_VOLUME . captures ( s ) . ok_or_else (
|| { InvalidValueError :: new ( "host volume" , s ) } ));
if let Some(path) = caps.at(1) {
Ok(HostVolume::Path(Path::new(path).to_owned()))
} else if let Some(path) = caps.at(2) {
Ok(HostVolume::UserRelativePath(Path::new(path).to_owned()))
} else if let Some(name) = caps.at(3) {
Ok(HostVolume::Name(name.to_owned()))
} else { unreachable!() }
}
}
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct VolumeMount {
pub host: Option<HostVolume>,
pub container: PathBuf,
pub permissions: VolumePermissions,
#[doc(hidden)]
pub _phantom: PhantomData<()>,
}
impl VolumeMount {
pub fn host<P1, P2>(host: P1, container: P2) -> VolumeMount where
P1: Into<PathBuf>, P2: Into<PathBuf> {
VolumeMount{host: Some(HostVolume::Path(host.into())),
container: container.into(),
permissions: Default::default(),
_phantom: PhantomData,}
}
pub fn named<S, P>(name: S, container: P) -> VolumeMount where
S: Into<String>, P: Into<PathBuf> {
VolumeMount{host: Some(HostVolume::Name(name.into())),
container: container.into(),
permissions: Default::default(),
_phantom: PhantomData,}
}
pub fn anonymous<P>(container: P) -> VolumeMount where P: Into<PathBuf> {
VolumeMount{host: None,
container: container.into(),
permissions: Default::default(),
_phantom: PhantomData,}
}
}
impl_interpolatable_value! (VolumeMount);
impl fmt::Display for VolumeMount {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
if self.host.is_none() && self.permissions != Default::default() {
return Err(fmt::Error);
}
match &self.host {
&Some(ref host) => try!(write ! ( f , "{}:" , host )),
&None => { }
}
let containerstr =
try!(self . container . to_str ( ) . ok_or ( fmt :: Error ));
try!(write ! ( f , "{}" , containerstr ));
if self.permissions != Default::default() {
try!(write ! ( f , ":{}" , self . permissions ))
}
Ok(())
}
}
impl FromStr for VolumeMount {
type
Err
=
InvalidValueError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let items = s.split(":").collect::<Vec<_>>();
match items.len() {
1 => {
Ok(VolumeMount{host: None,
container: Path::new(items[0]).to_owned(),
permissions: Default::default(),
_phantom: PhantomData,})
}
2 => {
Ok(VolumeMount{host:
Some(try!(FromStr :: from_str ( items [ 0 ]
))),
container: Path::new(items[1]).to_owned(),
permissions: Default::default(),
_phantom: PhantomData,})
}
3 => {
Ok(VolumeMount{host:
Some(try!(FromStr :: from_str ( items [ 0 ]
))),
container: Path::new(items[1]).to_owned(),
permissions:
try!(FromStr :: from_str ( items [ 2 ] )),
_phantom: PhantomData,})
}
_ => Err(InvalidValueError::new("volume", s)),
}
}
}
#[test]
fn volume_mounts_should_have_string_representations() {
let vol1 = VolumeMount::anonymous("/var/lib");
let vol2 = VolumeMount::named("named", "/var/lib");
let vol3 =
VolumeMount{permissions:
VolumePermissions::ReadOnly,
..VolumeMount::host("/etc/foo",
"/etc/myfoo")};
let pairs =
vec!(( vol1 , "/var/lib" ) , ( vol2 , "named:/var/lib" ) , (
vol3 , "/etc/foo:/etc/myfoo:ro" ) ,);
for (mode, s) in pairs {
assert_eq!(mode . to_string ( ) , s);
assert_eq!(mode , VolumeMount :: from_str ( s ) . unwrap ( ));
}
}