use std::marker::PhantomData; use mingling_core::{ ProgramCollect, hook::{ProgramControlUnit, ProgramHook}, setup::ProgramSetup, this, }; use crate::res::ResExitCode; /// Provides the ability to control the program's exit code, which is returned when the program ends. /// /// - Use `mingling::update_exit_code` to update the exit code. /// - Use `mingling::current_exit_code` to query the current exit code. pub struct ExitCodeSetup { _collect: PhantomData, } impl Default for ExitCodeSetup where C: ProgramCollect + 'static, { fn default() -> Self { Self { _collect: PhantomData, } } } impl ProgramSetup for ExitCodeSetup where C: ProgramCollect + 'static, { fn setup(self, program: &mut crate::Program) { // Insert resource program.with_resource(ResExitCode { exit_code: 0 }); // Insert hook to override exit code before program ends program.with_hook(ProgramHook::empty().on_finish(|_| { let this = this::().res_or_default::(); ProgramControlUnit::OverrideExitCode(this.exit_code) })); } }