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 6, 2024
1 parent f933d78 commit 061edfe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
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
2 changes: 1 addition & 1 deletion src/bootstrap/src/utils/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 061edfe

Please sign in to comment.