Add #[must_use] attribute to several command-related methods

This should make it harder to accidentally forget to use results of methods on `BootstrapCommand` and `CommandStatus`.
This commit is contained in:
Jakub Beránek 2024-07-04 15:26:09 +02:00
parent f933d789b7
commit 061edfe1fa
2 changed files with 13 additions and 1 deletions

View File

@ -109,14 +109,17 @@ impl BootstrapCommand {
self
}
#[must_use]
pub fn delay_failure(self) -> Self {
Self { failure_behavior: BehaviorOnFailure::DelayFail, ..self }
}
#[must_use]
pub fn fail_fast(self) -> Self {
Self { failure_behavior: BehaviorOnFailure::Exit, ..self }
}
#[must_use]
pub fn allow_failure(self) -> Self {
Self { failure_behavior: BehaviorOnFailure::Ignore, ..self }
}
@ -127,11 +130,13 @@ impl BootstrapCommand {
}
/// Capture all output of the command, do not print it.
#[must_use]
pub fn capture(self) -> Self {
Self { stdout: OutputMode::Capture, stderr: OutputMode::Capture, ..self }
}
/// Capture stdout of the command, do not print it.
#[must_use]
pub fn capture_stdout(self) -> Self {
Self { stdout: OutputMode::Capture, ..self }
}
@ -178,10 +183,12 @@ pub struct CommandOutput {
}
impl CommandOutput {
#[must_use]
pub fn did_not_start() -> Self {
Self { status: CommandStatus::DidNotStart, stdout: vec![], stderr: vec![] }
}
#[must_use]
pub fn is_success(&self) -> bool {
match self.status {
CommandStatus::Finished(status) => status.success(),
@ -189,10 +196,12 @@ impl CommandOutput {
}
}
#[must_use]
pub fn is_failure(&self) -> bool {
!self.is_success()
}
#[must_use]
pub fn status(&self) -> Option<ExitStatus> {
match self.status {
CommandStatus::Finished(status) => Some(status),
@ -200,14 +209,17 @@ impl CommandOutput {
}
}
#[must_use]
pub fn stdout(&self) -> String {
String::from_utf8(self.stdout.clone()).expect("Cannot parse process stdout as UTF-8")
}
#[must_use]
pub fn stdout_if_ok(&self) -> Option<String> {
if self.is_success() { Some(self.stdout()) } else { None }
}
#[must_use]
pub fn stderr(&self) -> String {
String::from_utf8(self.stderr.clone()).expect("Cannot parse process stderr as UTF-8")
}

View File

@ -499,7 +499,7 @@ pub fn check_cfg_arg(name: &str, values: Option<&[&str]>) -> String {
/// Whenever a git invocation is needed, this function should be preferred over
/// manually building a git `BootstrapCommand`. This approach allows us to manage
/// bootstrap-specific needs/hacks from a single source, rather than applying them on next to every
/// `BootstrapCommand::new("git")`, which is painful to ensure that the required change is applied
/// git command creation, which is painful to ensure that the required change is applied
/// on each one of them correctly.
pub fn git(source_dir: Option<&Path>) -> BootstrapCommand {
let mut git = command("git");