Skip to content

Commit 6328b5c

Browse files
committed
refactor builder interface
1 parent e72f01c commit 6328b5c

File tree

4 files changed

+33
-73
lines changed

4 files changed

+33
-73
lines changed

examples/docs-builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() -> Result<(), Box<dyn Error>> {
2626
.enable_networking(false);
2727

2828
let mut build_dir = workspace.build_dir("docs");
29-
build_dir.build(&toolchain, &krate, sandbox, |build| {
29+
build_dir.build(&toolchain, &krate, sandbox).run(|build| {
3030
build.cargo().args(&["doc", "--no-deps"]).run()?;
3131
Ok(())
3232
})?;

src/build.rs

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,64 +18,21 @@ pub struct BuildDirectory {
1818

1919
pub struct Builder<'a> {
2020
build_dir: &'a mut BuildDirectory,
21-
toolchain: Option<&'a Toolchain>,
22-
krate: Option<Crate>,
23-
sandbox: Option<SandboxBuilder>,
21+
toolchain: &'a Toolchain,
22+
krate: &'a Crate,
23+
sandbox: SandboxBuilder,
2424
patches: Vec<CratePatch>,
2525
}
2626

2727
impl<'a> Builder<'a> {
28-
pub(crate) fn new(build_dir: &'a mut BuildDirectory) -> Self {
29-
Builder {
30-
build_dir,
31-
toolchain: None,
32-
krate: None,
33-
sandbox: None,
34-
patches: Vec::new()
35-
}
36-
}
37-
38-
pub fn toolchain(mut self, toolchain: &'a Toolchain) -> Self {
39-
self.toolchain.replace(toolchain);
40-
self
41-
}
42-
43-
pub fn krate(mut self, krate: Crate) -> Self {
44-
self.krate.replace(krate);
45-
self
46-
}
47-
48-
pub fn sandbox(mut self, sandbox: SandboxBuilder) -> Self {
49-
self.sandbox.replace(sandbox);
50-
self
51-
}
5228

5329
pub fn patch(mut self, patch: CratePatch) -> Self {
5430
self.patches.push(patch);
5531
self
5632
}
5733

58-
pub fn build<R, F: FnOnce(&Build) -> Result<R, Error>>(mut self, f: F) -> Result<R, Error> {
59-
let tc = match self.toolchain {
60-
Some(t) => t,
61-
None => return Err(failure::err_msg("No tc")),
62-
};
63-
64-
let kr = match self.krate {
65-
Some(mut k) => {
66-
self.patches.drain(..).for_each(|p| k.add_patch(p));
67-
k
68-
},
69-
None => return Err(failure::err_msg("no krate")),
70-
};
71-
72-
let sn = match self.sandbox {
73-
Some(s) => s,
74-
None => return Err(failure::err_msg("No sandbox")),
75-
};
76-
77-
78-
self.build_dir.build(tc, &kr, sn, f)
34+
pub fn run<R, F: FnOnce(&Build) -> Result<R, Error>>(self, f: F) -> Result<R, Error> {
35+
self.build_dir.run(self.toolchain, self.krate, self.sandbox, self.patches, f)
7936
}
8037
}
8138

@@ -87,8 +44,14 @@ impl BuildDirectory {
8744
}
8845
}
8946

90-
pub fn builder(&mut self) -> Builder {
91-
Builder::new(self)
47+
pub fn build<'a>(&'a mut self,
48+
toolchain: &'a Toolchain,
49+
krate: &'a Crate,
50+
sandbox: SandboxBuilder,
51+
) -> Builder {
52+
Builder {
53+
build_dir: self, toolchain, krate, sandbox, patches: Vec::new()
54+
}
9255
}
9356

9457
/// Run a sandboxed build of the provided crate with the provided toolchain. The closure will
@@ -116,19 +79,20 @@ impl BuildDirectory {
11679
/// # Ok(())
11780
/// # }
11881
/// ```
119-
pub fn build<R, F: FnOnce(&Build) -> Result<R, Error>>(
82+
pub(crate) fn run<R, F: FnOnce(&Build) -> Result<R, Error>>(
12083
&mut self,
12184
toolchain: &Toolchain,
12285
krate: &Crate,
12386
sandbox: SandboxBuilder,
87+
patches: Vec<CratePatch>,
12488
f: F,
12589
) -> Result<R, Error> {
12690
let source_dir = self.source_dir();
12791
if source_dir.exists() {
12892
remove_dir_all(&source_dir)?;
12993
}
13094

131-
let mut prepare = Prepare::new(&self.workspace, toolchain, krate, &source_dir);
95+
let mut prepare = Prepare::new(&self.workspace, toolchain, krate, &source_dir, patches);
13296
prepare.prepare()?;
13397

13498
std::fs::create_dir_all(self.target_dir())?;

src/crates/mod.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,25 @@ pub struct CratePatch {
2828
}
2929

3030
/// A Rust crate that can be used with rustwide.
31-
pub struct Crate(CrateType, Option<Vec<CratePatch>>);
31+
pub struct Crate(CrateType);
3232

3333
impl Crate {
3434
/// Load a crate from the [crates.io registry](https://crates.io).
3535
pub fn crates_io(name: &str, version: &str) -> Self {
3636
Crate(CrateType::CratesIO(cratesio::CratesIOCrate::new(
3737
name, version,
38-
)), None)
38+
)))
3939
}
4040

4141
/// Load a crate from a git repository. The full URL needed to clone the repo has to be
4242
/// provided.
4343
pub fn git(url: &str) -> Self {
44-
Crate(CrateType::Git(git::GitRepo::new(url)), None)
44+
Crate(CrateType::Git(git::GitRepo::new(url)))
4545
}
4646

4747
/// Load a crate from a directory in the local filesystem.
4848
pub fn local(path: &Path) -> Self {
49-
Crate(CrateType::Local(local::Local::new(path)), None)
49+
Crate(CrateType::Local(local::Local::new(path)))
5050
}
5151

5252
/// Fetch the crate's source code and cache it in the workspace. This method will reach out to
@@ -70,15 +70,6 @@ impl Crate {
7070
}
7171
}
7272

73-
pub(crate) fn add_patch(&mut self, patch: CratePatch) {
74-
let patches: &mut Vec<CratePatch> = self.1.get_or_insert(Vec::new());
75-
patches.push(patch);
76-
}
77-
78-
pub(crate) fn patches(&self) -> &Option<Vec<CratePatch>> {
79-
&self.1
80-
}
81-
8273
pub(crate) fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> Result<(), Error> {
8374
if dest.exists() {
8475
info!(

src/prepare.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::cmd::Command;
2-
use crate::{Crate, Toolchain, Workspace};
2+
use crate::{Crate, crates::CratePatch, Toolchain, Workspace};
33
use failure::{Error, Fail, ResultExt};
44
use log::info;
55
use std::path::Path;
@@ -14,6 +14,7 @@ pub(crate) struct Prepare<'a> {
1414
krate: &'a Crate,
1515
source_dir: &'a Path,
1616
lockfile_captured: bool,
17+
patches: Vec<CratePatch>
1718
}
1819

1920
impl<'a> Prepare<'a> {
@@ -22,13 +23,15 @@ impl<'a> Prepare<'a> {
2223
toolchain: &'a Toolchain,
2324
krate: &'a Crate,
2425
source_dir: &'a Path,
26+
patches: Vec<CratePatch>
2527
) -> Self {
2628
Self {
2729
workspace,
2830
toolchain,
2931
krate,
3032
source_dir,
3133
lockfile_captured: false,
34+
patches,
3235
}
3336
}
3437

@@ -67,7 +70,7 @@ impl<'a> Prepare<'a> {
6770

6871
fn tweak_toml(&self) -> Result<(), Error> {
6972
let path = self.source_dir.join("Cargo.toml");
70-
let mut tweaker = TomlTweaker::new(&self.krate, &path)?;
73+
let mut tweaker = TomlTweaker::new(&self.krate, &path, &self.patches)?;
7174
tweaker.tweak();
7275
tweaker.save(&path)?;
7376
Ok(())
@@ -143,26 +146,28 @@ pub struct TomlTweaker<'a> {
143146
krate: &'a Crate,
144147
table: Table,
145148
dir: Option<&'a Path>,
149+
patches: &'a Vec<CratePatch>,
146150
}
147151

148152
impl<'a> TomlTweaker<'a> {
149-
pub fn new(krate: &'a Crate, cargo_toml: &'a Path) -> Result<Self, Error> {
153+
pub fn new(krate: &'a Crate, cargo_toml: &'a Path, patches: &'a Vec<CratePatch>) -> Result<Self, Error> {
150154
let toml_content = ::std::fs::read_to_string(cargo_toml)
151155
.with_context(|_| PrepareError::MissingCargoToml)?;
152156
let table: Table =
153157
toml::from_str(&toml_content).with_context(|_| PrepareError::InvalidCargoTomlSyntax)?;
154158

155159
let dir = cargo_toml.parent();
156160

157-
Ok(TomlTweaker { krate, table, dir })
161+
Ok(TomlTweaker { krate, table, dir, patches })
158162
}
159163

160164
#[cfg(test)]
161-
fn new_with_table(krate: &'a Crate, table: Table) -> Self {
165+
fn new_with_table(krate: &'a Crate, table: Table, patches: &'a Vec<CratePatch>) -> Self {
162166
TomlTweaker {
163167
krate,
164168
table,
165169
dir: None,
170+
patches,
166171
}
167172
}
168173

@@ -277,13 +282,13 @@ impl<'a> TomlTweaker<'a> {
277282
}
278283

279284
fn apply_patches(&mut self) {
280-
if let Some(krate_patches) = self.krate.patches() {
285+
if self.patches.len() > 0 {
281286
if !self.table.contains_key("patch.crates-io") {
282287
self.table.insert("patch.crates-io".into(), Value::Table(Table::new()));
283288
}
284289

285290
if let Some(&mut Value::Table(ref mut patches)) = self.table.get_mut("patch.crates-io") {
286-
for patch in krate_patches.iter().cloned() {
291+
for patch in self.patches.iter().cloned() {
287292
let mut patch_table = Table::new();
288293
patch_table.insert("git".into(), Value::String(patch.uri));
289294
patch_table.insert("branch".into(), Value::String(patch.branch));

0 commit comments

Comments
 (0)