Skip to content

Commit

Permalink
Add #[must_use] attribute to several command-related methods
Browse files Browse the repository at this point in the history
This should make it harder to accidentally forget to use results of methods on `BootstrapCommand` and `CommandStatus`.
  • Loading branch information
Kobzol committed Jul 4, 2024
1 parent f933d78 commit 8394dad
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/bootstrap/src/utils/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand All @@ -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 }
}
Expand Down Expand Up @@ -178,36 +183,43 @@ 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(),
CommandStatus::DidNotStart => false,
}
}

#[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),
CommandStatus::DidNotStart => None,
}
}

#[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")
}
Expand Down

0 comments on commit 8394dad

Please sign in to comment.