diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-08-18 02:30:09 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-08-18 02:30:09 +0800 |
| commit | cd9cad77542e1909f9600f8a2e2754cc4cc4507a (patch) | |
| tree | d77eb9838a92b4b71de6252e6d892056123cfc1a | |
| parent | e70ffc81399f0f90f71fd269f4ba2ee90ca20c4a (diff) | |
chore!: simplify ExitCodeSetup to non-generic unit struct
| -rw-r--r-- | CHANGELOG.md | 19 | ||||
| -rw-r--r-- | docs/_zh_CN/pages/12-exit-code.md | 4 | ||||
| -rw-r--r-- | docs/pages/12-exit-code.md | 4 | ||||
| -rw-r--r-- | examples/example-exitcode/src/main.rs | 2 | ||||
| -rw-r--r-- | examples/full-todolist/src/main.rs | 2 | ||||
| -rw-r--r-- | mingling/src/example_docs.rs | 2 | ||||
| -rw-r--r-- | mingling/src/setups/exit_code.rs | 62 | ||||
| -rw-r--r-- | mingling_cli/src/bin/cli.rs | 4 |
8 files changed, 67 insertions, 32 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index a3090b4..e216f6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -477,6 +477,25 @@ None _No behavioral changes — the setup still registers the same four directory resources (`ResCurrentDir`, `ResCurrentExe`, `ResHomeDir`, `ResTempDir`) in the program's resource store. The type simplification is purely ergonomic. +10. **[`setups:exit_code`]** **[BREAKING]** Simplified the `ExitCodeSetup` type — it is no longer generic over the program collect type `C` and no longer requires `ExitCodeSetup::<C>::default()` to construct. + + ### What changed + + Previously, `ExitCodeSetup` was a generic struct `ExitCodeSetup<C>` (carrying `PhantomData<C>`) that had to be constructed via `ExitCodeSetup::<C>::default()` before calling `Program::with_setup`. Now the struct is unit-like (`pub struct ExitCodeSetup;`), so it can be constructed directly as a value with no `::default()` call and no generic parameter. + + Additionally, the `setup` method's `program` parameter was retyped from `crate::Program<C>` to `mingling_core::Program<C>` for cleanliness. + + ### Removed / changed API + - **`ExitCodeSetup<C>`** → **`ExitCodeSetup`** — The struct no longer has a generic parameter (previously `ExitCodeSetup<C>` with `PhantomData<C>`). + - **`impl<C> Default for ExitCodeSetup<C>`** — Removed. The unit struct uses the derived/implicit `Default`, and more importantly construction is now just the plain value `ExitCodeSetup`, not `ExitCodeSetup::<C>::default()`. + - **`impl<C> ProgramSetup<C> for ExitCodeSetup<C>`** → **`impl<C> ProgramSetup<C> for ExitCodeSetup`** — The `ProgramSetup` impl is now on the unit type. + + ### Migration guide + - Replace `program.with_setup(ExitCodeSetup::<ThisProgram>::default())` (or `ExitCodeSetup::default()`) with `program.with_setup(ExitCodeSetup)`. + - Any type annotations referencing `ExitCodeSetup<C>` must drop the generic argument. + + _No behavioral changes — the setup still registers the same `ResExitCode` resource (initialised to `0`) and installs the same program-finish hook that overrides the program's exit code when the resource holds a non-zero value. The type simplification is purely ergonomic._ + --- ## Contents diff --git a/docs/_zh_CN/pages/12-exit-code.md b/docs/_zh_CN/pages/12-exit-code.md index 9270b84..1f9e05c 100644 --- a/docs/_zh_CN/pages/12-exit-code.md +++ b/docs/_zh_CN/pages/12-exit-code.md @@ -12,7 +12,7 @@ @@@use mingling::setup::ExitCodeSetup; fn main() { let mut program = ThisProgram::new(); - program.with_setup(ExitCodeSetup::default()); + program.with_setup(ExitCodeSetup); @@@ program.exec_and_exit(); } ``` @@ -55,7 +55,7 @@ fn handle_check(_args: EntryCheck, ec: &mut ResExitCode) { @@@use mingling::setup::ExitCodeSetup; fn main() { let mut program = ThisProgram::new(); - program.with_setup(ExitCodeSetup::default()); + program.with_setup(ExitCodeSetup); // 获取退出码自行处理 let exit_code = program.exec(); diff --git a/docs/pages/12-exit-code.md b/docs/pages/12-exit-code.md index 8fa320c..e9ad9de 100644 --- a/docs/pages/12-exit-code.md +++ b/docs/pages/12-exit-code.md @@ -12,7 +12,7 @@ Providing the shell with a correct exit code when a program terminates is a basi @@@use mingling::setup::ExitCodeSetup; fn main() { let mut program = ThisProgram::new(); - program.with_setup(ExitCodeSetup::default()); + program.with_setup(ExitCodeSetup); @@@ program.exec_and_exit(); } ``` @@ -55,7 +55,7 @@ fn handle_check(_args: EntryCheck, ec: &mut ResExitCode) { @@@use mingling::setup::ExitCodeSetup; fn main() { let mut program = ThisProgram::new(); - program.with_setup(ExitCodeSetup::default()); + program.with_setup(ExitCodeSetup); // Get exit code and handle it yourself let exit_code = program.exec(); diff --git a/examples/example-exitcode/src/main.rs b/examples/example-exitcode/src/main.rs index c7f731d..f0db05b 100644 --- a/examples/example-exitcode/src/main.rs +++ b/examples/example-exitcode/src/main.rs @@ -28,7 +28,7 @@ fn main() { // --------- IMPORTANT --------- // Register `ExitCodeSetup` for the program to enable exit codes - program.with_setup(ExitCodeSetup::default()); + program.with_setup(ExitCodeSetup); // --------- IMPORTANT --------- program.exec_and_exit(); diff --git a/examples/full-todolist/src/main.rs b/examples/full-todolist/src/main.rs index 9c7bfe8..cac2e13 100644 --- a/examples/full-todolist/src/main.rs +++ b/examples/full-todolist/src/main.rs @@ -60,7 +60,7 @@ fn main() { let mut program = ThisProgram::new(); // Setups - program.with_setup(ExitCodeSetup::default()); + program.with_setup(ExitCodeSetup); program.with_setup(StructuralRendererSetup); program.with_setup(HelpFlagSetup::new(&arg![help: Flag, 'h'])); diff --git a/mingling/src/example_docs.rs b/mingling/src/example_docs.rs index c9955b5..055615d 100644 --- a/mingling/src/example_docs.rs +++ b/mingling/src/example_docs.rs @@ -1318,7 +1318,7 @@ pub mod example_error_handling {} /// /// // --------- IMPORTANT --------- /// // Register `ExitCodeSetup` for the program to enable exit codes -/// program.with_setup(ExitCodeSetup::default()); +/// program.with_setup(ExitCodeSetup); /// // --------- IMPORTANT --------- /// /// program.exec_and_exit(); diff --git a/mingling/src/setups/exit_code.rs b/mingling/src/setups/exit_code.rs index e31e511..0558892 100644 --- a/mingling/src/setups/exit_code.rs +++ b/mingling/src/setups/exit_code.rs @@ -1,8 +1,5 @@ -// Doc Not Optimize -use std::marker::PhantomData; - use mingling_core::{ - ProgramCollect, + Program, ProgramCollect, hook::{ProgramControlUnit, ProgramControls, ProgramHook}, setup::ProgramSetup, this, @@ -10,30 +7,49 @@ use mingling_core::{ use crate::res::ResExitCode; -/// Provides the ability to control the program's exit code, which is returned when the program ends. +/// `ExitCodeSetup` — Setup for controlling the program's exit code /// -/// - Use `mingling::update_exit_code` to update the exit code. -/// - Use `mingling::current_exit_code` to query the current exit code. -pub struct ExitCodeSetup<C> { - _collect: PhantomData<C>, -} - -impl<C> Default for ExitCodeSetup<C> -where - C: ProgramCollect<Enum = C> + 'static, -{ - fn default() -> Self { - Self { - _collect: PhantomData, - } - } -} +/// This Setup registers an [`ResExitCode`] resource that tracks the desired exit +/// code for the program. When the program finishes, a hook reads this resource +/// and overrides the program's exit code if it has been modified from its +/// default value of `0`. +/// +/// # Usage +/// +/// This Setup can be registered using the +/// [`Program`](https://docs.rs/mingling/latest/mingling/struct.Program.html) +/// `with_setup` method, for example: +/// +/// ```rust +/// # use mingling::MockProgramCollect as ThisProgram; +/// use mingling::Program; +/// use mingling::setup::ExitCodeSetup; +/// +/// let mut program = Program::<ThisProgram>::new(); +/// program.with_setup(ExitCodeSetup); +/// ``` +/// +/// # Behavior +/// +/// - Registers an [`ResExitCode`] resource initialised to `0`. +/// - Installs a program-finish hook that: +/// - Reads the current [`ResExitCode`] value. +/// - Overrides the program's exit code with that value if it is non-zero. +/// - Leaves the exit code untouched if the resource still holds its default +/// value of `0`. +/// +/// # Notes +/// +/// - Use [`update_exit_code`](crate::update_exit_code) to set a custom exit code +/// during program execution. +/// - Use [`current_exit_code`](crate::current_exit_code) to query the current value. +pub struct ExitCodeSetup; -impl<C> ProgramSetup<C> for ExitCodeSetup<C> +impl<C> ProgramSetup<C> for ExitCodeSetup where C: ProgramCollect<Enum = C> + 'static, { - fn setup(self, program: &mut crate::Program<C>) { + fn setup(self, program: &mut Program<C>) { // Insert resource program.with_resource(ResExitCode { exit_code: 0 }); diff --git a/mingling_cli/src/bin/cli.rs b/mingling_cli/src/bin/cli.rs index f641749..d0c9e8b 100644 --- a/mingling_cli/src/bin/cli.rs +++ b/mingling_cli/src/bin/cli.rs @@ -10,8 +10,8 @@ async fn main() { // Setups program.with_setup(HelpFlagSetup::default()); - program.with_setup(ExitCodeSetup::default()); - program.with_setup(DirectoryEnvironmentSetup::default()); + program.with_setup(ExitCodeSetup); + program.with_setup(DirectoryEnvironmentSetup); program.with_setup(MinglingMetadataSetup); program.with_setup(MlingConfigSetup); |
