Skip to content

Commit 9f0985d

Browse files
committed
add a builder in front of BuildDirectory::build
1 parent fa1480b commit 9f0985d

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/build.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,56 @@ pub struct BuildDirectory {
1515
name: String,
1616
}
1717

18+
pub struct Builder<'a> {
19+
build_dir: &'a mut BuildDirectory,
20+
toolchain: Option<&'a Toolchain>,
21+
krate: Option<&'a Crate>,
22+
sandbox: Option<SandboxBuilder>,
23+
}
24+
25+
impl<'a> Builder<'a> {
26+
pub fn new(build_dir: &'a mut BuildDirectory) -> Self {
27+
Builder {
28+
build_dir,
29+
toolchain: None,
30+
krate: None,
31+
sandbox: None
32+
}
33+
}
34+
35+
pub fn toolchain(mut self, toolchain: &'a Toolchain) -> Self {
36+
self.toolchain.replace(toolchain);
37+
self
38+
}
39+
40+
pub fn krate(mut self, krate: &'a Crate) -> Self {
41+
self.krate.replace(krate);
42+
self
43+
}
44+
45+
pub fn sandbox(mut self, sandbox: SandboxBuilder) -> Self {
46+
self.sandbox.replace(sandbox);
47+
self
48+
}
49+
50+
pub fn build<R, F: FnOnce(&Build) -> Result<R, Error>>(self, f: F) -> Result<R, Error> {
51+
let tc = match self.toolchain {
52+
Some(t) => t,
53+
None => return Err(failure::err_msg("No tc")),
54+
};
55+
let kr = match self.krate {
56+
Some(k) => k,
57+
None => return Err(failure::err_msg("No crate")),
58+
};
59+
let sn = match self.sandbox {
60+
Some(s) => s,
61+
None => return Err(failure::err_msg("No sandbox")),
62+
};
63+
64+
self.build_dir.build(tc, kr, sn, f)
65+
}
66+
}
67+
1868
impl BuildDirectory {
1969
pub(crate) fn new(workspace: Workspace, name: &str) -> Self {
2070
Self {
@@ -23,6 +73,10 @@ impl BuildDirectory {
2373
}
2474
}
2575

76+
pub fn builder(&mut self) -> Builder {
77+
Builder::new(self)
78+
}
79+
2680
/// Run a sandboxed build of the provided crate with the provided toolchain. The closure will
2781
/// be provided an instance of [`Build`](struct.Build.html) that allows spawning new processes
2882
/// inside the sandbox.

0 commit comments

Comments
 (0)