Skip to content

Commit e3b6316

Browse files
committed
unify line processing for lockfile generation and dependency fetching
to prevent them from diverging in the future - adjust Command::process_lines to change the 'pl lifetime when changing the processing function
1 parent d2e681a commit e3b6316

File tree

2 files changed

+33
-55
lines changed

2 files changed

+33
-55
lines changed

src/cmd/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,14 @@ impl<'w, 'pl> Command<'w, 'pl> {
329329
/// # Ok(())
330330
/// # }
331331
/// ```
332-
pub fn process_lines(mut self, f: &'pl mut dyn FnMut(&str, &mut ProcessLinesActions)) -> Self {
333-
self.process_lines = Some(f);
334-
self
332+
pub fn process_lines<'pl2>(
333+
self,
334+
f: &'pl2 mut dyn FnMut(&str, &mut ProcessLinesActions),
335+
) -> Command<'w, 'pl2> {
336+
Command {
337+
process_lines: Some(f),
338+
..self
339+
}
335340
}
336341

337342
/// Enable or disable logging all the output lines to the [`log` crate][log]. By default

src/prepare.rs

Lines changed: 25 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::cmd::{Command, CommandError};
1+
use crate::cmd::{Command, CommandError, ProcessLinesActions};
22
use crate::{build::CratePatch, Crate, Toolchain, Workspace};
33
use anyhow::Context as _;
44
use log::info;
@@ -101,9 +101,6 @@ impl<'a> Prepare<'a> {
101101
return Ok(());
102102
}
103103

104-
let mut yanked_deps = false;
105-
let mut missing_deps = false;
106-
let mut broken_deps = false;
107104
let mut cmd = Command::new(self.workspace, self.toolchain.cargo()).args(&[
108105
"generate-lockfile",
109106
"--manifest-path",
@@ -115,35 +112,7 @@ impl<'a> Prepare<'a> {
115112
.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "nightly");
116113
}
117114

118-
match cmd
119-
.cd(self.source_dir)
120-
.process_lines(&mut |line, _| {
121-
if line.contains("failed to select a version for the requirement") {
122-
yanked_deps = true;
123-
} else if line.contains("failed to load source for dependency")
124-
|| line.contains("no matching package named")
125-
{
126-
missing_deps = true;
127-
} else if line.contains("failed to parse manifest at")
128-
|| line.contains("error: invalid table header")
129-
{
130-
broken_deps = true;
131-
}
132-
})
133-
.run_capture()
134-
{
135-
Ok(_) => Ok(()),
136-
Err(CommandError::ExecutionFailed { status: _, stderr }) if yanked_deps => {
137-
Err(PrepareError::YankedDependencies(stderr).into())
138-
}
139-
Err(CommandError::ExecutionFailed { status: _, stderr }) if missing_deps => {
140-
Err(PrepareError::MissingDependencies(stderr).into())
141-
}
142-
Err(CommandError::ExecutionFailed { status: _, stderr }) if broken_deps => {
143-
Err(PrepareError::InvalidCargoTomlSyntaxInDependencies(stderr).into())
144-
}
145-
Err(err) => Err(err.into()),
146-
}
115+
run_command(cmd.cd(self.source_dir))
147116
}
148117

149118
fn fetch_deps(&mut self) -> anyhow::Result<()> {
@@ -157,9 +126,6 @@ pub(crate) fn fetch_deps(
157126
source_dir: &Path,
158127
fetch_build_std_targets: &[&str],
159128
) -> anyhow::Result<()> {
160-
let mut yanked_deps = false;
161-
let mut missing_deps = false;
162-
let mut broken_deps = false;
163129
let mut cmd = Command::new(workspace, toolchain.cargo())
164130
.args(&["fetch", "--manifest-path", "Cargo.toml"])
165131
.cd(source_dir);
@@ -173,22 +139,29 @@ pub(crate) fn fetch_deps(
173139
cmd = cmd.args(&["--target", target]);
174140
}
175141

176-
match cmd
177-
.process_lines(&mut |line, _| {
178-
if line.contains("failed to select a version for the requirement") {
179-
yanked_deps = true;
180-
} else if line.contains("failed to load source for dependency")
181-
|| line.contains("no matching package named")
182-
{
183-
missing_deps = true;
184-
} else if line.contains("failed to parse manifest at")
185-
|| line.contains("error: invalid table header")
186-
{
187-
broken_deps = true;
188-
}
189-
})
190-
.run_capture()
191-
{
142+
run_command(cmd)
143+
}
144+
145+
fn run_command(cmd: Command) -> anyhow::Result<()> {
146+
let mut yanked_deps = false;
147+
let mut missing_deps = false;
148+
let mut broken_deps = false;
149+
150+
let mut process = |line: &str, _: &mut ProcessLinesActions| {
151+
if line.contains("failed to select a version for the requirement") {
152+
yanked_deps = true;
153+
} else if line.contains("failed to load source for dependency")
154+
|| line.contains("no matching package named")
155+
{
156+
missing_deps = true;
157+
} else if line.contains("failed to parse manifest at")
158+
|| line.contains("error: invalid table header")
159+
{
160+
broken_deps = true;
161+
}
162+
};
163+
164+
match cmd.process_lines(&mut process).run_capture() {
192165
Ok(_) => Ok(()),
193166
Err(CommandError::ExecutionFailed { status: _, stderr }) if yanked_deps => {
194167
Err(PrepareError::YankedDependencies(stderr).into())

0 commit comments

Comments
 (0)