Skip to content

Commit

Permalink
Remove impl TryFrom<ProcessState> for ExitStatus
Browse files Browse the repository at this point in the history
The conversion from ProcessState to ExitStatus is now done by the
System trait as it depends on the system implementation. This commit
removes the TryFrom implementation and replaces it with a call to
System::exit_status_for_process_state.
  • Loading branch information
magicant committed May 19, 2024
1 parent 7dcbabb commit c66925b
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 30 deletions.
8 changes: 6 additions & 2 deletions yash-builtin/src/fg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ use yash_env::job::id::parse;
use yash_env::job::JobList;
use yash_env::job::Pid;
use yash_env::job::ProcessState;
use yash_env::semantics::ExitStatus;
use yash_env::semantics::Field;
use yash_env::system::Errno;
use yash_env::system::System as _;
Expand Down Expand Up @@ -189,7 +188,11 @@ pub async fn main(env: &mut Env, args: Vec<Field>) -> crate::Result {
};

match result {
Ok(state) => ExitStatus::try_from(state).unwrap().into(),
Ok(state) => env
.system
.exit_status_for_process_state(state)
.unwrap()
.into(),
Err(error) => report_simple_failure(env, &error.to_string()).await,
}
}
Expand All @@ -208,6 +211,7 @@ mod tests {
use yash_env::job::ProcessState;
use yash_env::option::Option::Monitor;
use yash_env::option::State::On;
use yash_env::semantics::ExitStatus;
use yash_env::subshell::JobControl;
use yash_env::subshell::Subshell;
use yash_env::system::r#virtual::Process;
Expand Down
22 changes: 0 additions & 22 deletions yash-env/src/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,28 +141,6 @@ impl ProcessState {
}
}

/// Error value indicating that the process is running.
///
/// This error value may be returned by [`TryFrom<ProcessState>::try_from`].
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct RunningProcess;

/// Converts `ProcessState` to `ExitStatus`.
///
/// For the `Running` state, the conversion fails with [`RunningProcess`].
impl TryFrom<ProcessState> for ExitStatus {
type Error = RunningProcess;
fn try_from(state: ProcessState) -> Result<Self, RunningProcess> {
match state {
ProcessState::Exited(exit_status) => Ok(exit_status),
ProcessState::Signaled { signal, .. } | ProcessState::Stopped(signal) => {
Ok(ExitStatus::from(signal))
}
ProcessState::Running => Err(RunningProcess),
}
}
}

/// Set of one or more processes executing a pipeline
///
/// In the current implementation, a job contains the process ID of one child
Expand Down
3 changes: 2 additions & 1 deletion yash-env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ impl Env {
loop {
let (pid, state) = self.wait_for_subshell(target).await?;
if !state.is_alive() {
return Ok((pid, state.try_into().unwrap()));
let exit_status = self.system.exit_status_for_process_state(state).unwrap();
return Ok((pid, exit_status));
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion yash-semantics/src/command/compound_command/subshell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use yash_env::semantics::ExitStatus;
use yash_env::semantics::Result;
use yash_env::subshell::JobControl;
use yash_env::subshell::Subshell;
use yash_env::system::SystemEx as _;
use yash_env::Env;
use yash_syntax::source::Location;
use yash_syntax::syntax::List;
Expand All @@ -47,7 +48,7 @@ pub async fn execute(env: &mut Env, body: Rc<List>, location: &Location) -> Resu
env.jobs.add(job);
}

env.exit_status = state.try_into().unwrap();
env.exit_status = env.system.exit_status_for_process_state(state).unwrap();
env.apply_errexit()
}
Err(errno) => {
Expand Down
4 changes: 2 additions & 2 deletions yash-semantics/src/command/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use yash_env::subshell::JobControl;
use yash_env::subshell::Subshell;
use yash_env::system::Errno;
use yash_env::system::FdFlag;
use yash_env::system::SystemEx;
use yash_env::system::SystemEx as _;
use yash_env::Env;
use yash_env::System;
use yash_syntax::syntax;
Expand Down Expand Up @@ -152,7 +152,7 @@ async fn execute_job_controlled_pipeline(
env.jobs.add(job);
}

env.exit_status = state.try_into().unwrap();
env.exit_status = env.system.exit_status_for_process_state(state).unwrap();
Continue(())
}
Err(errno) => {
Expand Down
3 changes: 2 additions & 1 deletion yash-semantics/src/command/simple_command/absent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use yash_env::semantics::ExitStatus;
use yash_env::semantics::Result;
use yash_env::subshell::JobControl;
use yash_env::subshell::Subshell;
use yash_env::system::SystemEx as _;
use yash_env::Env;
use yash_syntax::syntax::Assign;
use yash_syntax::syntax::Redir;
Expand Down Expand Up @@ -81,7 +82,7 @@ pub async fn execute_absent_target(
env.jobs.add(job);
}

state.try_into().unwrap()
env.system.exit_status_for_process_state(state).unwrap()
}
Err(errno) => {
print_error(
Expand Down
3 changes: 2 additions & 1 deletion yash-semantics/src/command/simple_command/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use yash_env::semantics::Result;
use yash_env::subshell::JobControl;
use yash_env::subshell::Subshell;
use yash_env::system::Errno;
use yash_env::system::SystemEx as _;
use yash_env::variable::Context;
use yash_env::Env;
use yash_env::System;
Expand Down Expand Up @@ -120,7 +121,7 @@ pub async fn start_external_utility_in_subshell_and_wait(
env.jobs.add(job);
}

state.try_into().unwrap()
env.system.exit_status_for_process_state(state).unwrap()
}
Err(errno) => {
print_error(
Expand Down

0 comments on commit c66925b

Please sign in to comment.