1
Fork 0

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 self
} }
#[must_use]
pub fn delay_failure(self) -> Self { pub fn delay_failure(self) -> Self {
Self { failure_behavior: BehaviorOnFailure::DelayFail, ..self } Self { failure_behavior: BehaviorOnFailure::DelayFail, ..self }
} }
#[must_use]
pub fn fail_fast(self) -> Self { pub fn fail_fast(self) -> Self {
Self { failure_behavior: BehaviorOnFailure::Exit, ..self } Self { failure_behavior: BehaviorOnFailure::Exit, ..self }
} }
#[must_use]
pub fn allow_failure(self) -> Self { pub fn allow_failure(self) -> Self {
Self { failure_behavior: BehaviorOnFailure::Ignore, ..self } Self { failure_behavior: BehaviorOnFailure::Ignore, ..self }
} }
@ -127,11 +130,13 @@ impl BootstrapCommand {
} }
/// Capture all output of the command, do not print it. /// Capture all output of the command, do not print it.
#[must_use]
pub fn capture(self) -> Self { pub fn capture(self) -> Self {
Self { stdout: OutputMode::Capture, stderr: OutputMode::Capture, ..self } Self { stdout: OutputMode::Capture, stderr: OutputMode::Capture, ..self }
} }
/// Capture stdout of the command, do not print it. /// Capture stdout of the command, do not print it.
#[must_use]
pub fn capture_stdout(self) -> Self { pub fn capture_stdout(self) -> Self {
Self { stdout: OutputMode::Capture, ..self } Self { stdout: OutputMode::Capture, ..self }
} }
@ -178,10 +183,12 @@ pub struct CommandOutput {
} }
impl CommandOutput { impl CommandOutput {
#[must_use]
pub fn did_not_start() -> Self { pub fn did_not_start() -> Self {
Self { status: CommandStatus::DidNotStart, stdout: vec![], stderr: vec![] } Self { status: CommandStatus::DidNotStart, stdout: vec![], stderr: vec![] }
} }
#[must_use]
pub fn is_success(&self) -> bool { pub fn is_success(&self) -> bool {
match self.status { match self.status {
CommandStatus::Finished(status) => status.success(), CommandStatus::Finished(status) => status.success(),
@ -189,10 +196,12 @@ impl CommandOutput {
} }
} }
#[must_use]
pub fn is_failure(&self) -> bool { pub fn is_failure(&self) -> bool {
!self.is_success() !self.is_success()
} }
#[must_use]
pub fn status(&self) -> Option<ExitStatus> { pub fn status(&self) -> Option<ExitStatus> {
match self.status { match self.status {
CommandStatus::Finished(status) => Some(status), CommandStatus::Finished(status) => Some(status),
@ -200,14 +209,17 @@ impl CommandOutput {
} }
} }
#[must_use]
pub fn stdout(&self) -> String { pub fn stdout(&self) -> String {
String::from_utf8(self.stdout.clone()).expect("Cannot parse process stdout as UTF-8") String::from_utf8(self.stdout.clone()).expect("Cannot parse process stdout as UTF-8")
} }
#[must_use]
pub fn stdout_if_ok(&self) -> Option<String> { pub fn stdout_if_ok(&self) -> Option<String> {
if self.is_success() { Some(self.stdout()) } else { None } if self.is_success() { Some(self.stdout()) } else { None }
} }
#[must_use]
pub fn stderr(&self) -> String { pub fn stderr(&self) -> String {
String::from_utf8(self.stderr.clone()).expect("Cannot parse process stderr as UTF-8") 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 /// Whenever a git invocation is needed, this function should be preferred over
/// manually building a git `BootstrapCommand`. This approach allows us to manage /// 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 /// 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. /// on each one of them correctly.
pub fn git(source_dir: Option<&Path>) -> BootstrapCommand { pub fn git(source_dir: Option<&Path>) -> BootstrapCommand {
let mut git = command("git"); let mut git = command("git");