From 95c6b979ca399671eed8bf9c72f53cfe5d46f431 Mon Sep 17 00:00:00 2001 From: Weicao-CatilGrass <1992414357@qq.com> Date: Wed, 13 May 2026 07:28:02 +0800 Subject: Migrate exit code control to resource-based system --- mingling_core/src/lib.rs | 10 +- mingling_core/src/program.rs | 11 +- mingling_core/src/program/exec.rs | 4 +- mingling_core/src/program/hook.rs | 147 +-------------------- mingling_core/src/program/setup.rs | 13 -- mingling_core/src/program/setup/basic.rs | 31 ----- .../src/program/setup/exit_code_control.rs | 35 ----- .../src/program/setup/general_renderer.rs | 57 -------- 8 files changed, 10 insertions(+), 298 deletions(-) delete mode 100644 mingling_core/src/program/setup/basic.rs delete mode 100644 mingling_core/src/program/setup/exit_code_control.rs delete mode 100644 mingling_core/src/program/setup/general_renderer.rs (limited to 'mingling_core') diff --git a/mingling_core/src/lib.rs b/mingling_core/src/lib.rs index e1c188f..817cc7a 100644 --- a/mingling_core/src/lib.rs +++ b/mingling_core/src/lib.rs @@ -46,11 +46,6 @@ pub use crate::program::*; pub use crate::renderer::render_result::*; -/// `Mingling`'s Program initialization system -pub mod setup { - pub use crate::program::setup::*; -} - #[cfg(feature = "builds")] #[doc(hidden)] pub mod builds; @@ -72,5 +67,6 @@ pub mod comp; #[cfg(feature = "comp")] pub use crate::comp::*; -pub use crate::setup::exit_code_control::current_exit_code; -pub use crate::setup::exit_code_control::update_exit_code; +pub mod setup { + pub use crate::program::setup::ProgramSetup; +} diff --git a/mingling_core/src/program.rs b/mingling_core/src/program.rs index b8a409e..0d8506a 100644 --- a/mingling_core/src/program.rs +++ b/mingling_core/src/program.rs @@ -11,11 +11,10 @@ use crate::{ AnyOutput, ChainProcess, GlobalResources, Groupped, RenderResult, asset::dispatcher::Dispatcher, error::{ChainProcessError, ProgramExecuteError}, - hook::{ProgramAnonymousHook, ProgramHook}, + hook::ProgramHook, }; use std::{ collections::HashMap, - fmt::Display, sync::{Arc, Mutex, OnceLock}, }; @@ -77,7 +76,6 @@ where pub general_renderer_name: GeneralRendererSetting, pub(crate) hooks: Vec>, - pub(crate) anonymous_hooks: Vec, pub(crate) resources: GlobalResources, } @@ -120,7 +118,6 @@ where general_renderer_name: GeneralRendererSetting::Disable, hooks: Vec::new(), - anonymous_hooks: Vec::new(), resources: Arc::new(Mutex::new(HashMap::new())), } @@ -176,7 +173,7 @@ where #[cfg(feature = "async")] impl Program where - C: ProgramCollect + std::fmt::Display, + C: ProgramCollect, { /// Sets the current program instance and runs the provided async function. async fn set_instance_and_run(self, f: F) -> Fut::Output @@ -261,7 +258,7 @@ where #[cfg(not(feature = "async"))] impl Program where - C: ProgramCollect + Display, + C: ProgramCollect, { /// Sets the current program instance and runs the provided function. fn set_instance_and_run(self, f: F) -> R @@ -345,7 +342,7 @@ where /// Note: It is recommended to use the `gen_program!()` macro from [mingling_macros](https://crates.io/crates/mingling_macros) to automatically create this type pub trait ProgramCollect { /// Enum type representing internal IDs for the program - type Enum: Display; + type Enum; type DispatcherNotFound: Groupped; type RendererNotFound: Groupped; diff --git a/mingling_core/src/program/exec.rs b/mingling_core/src/program/exec.rs index acd1bd6..c5ba628 100644 --- a/mingling_core/src/program/exec.rs +++ b/mingling_core/src/program/exec.rs @@ -13,7 +13,7 @@ pub async fn exec( program: &'static Program, ) -> Result where - C: ProgramCollect + std::fmt::Display, + C: ProgramCollect, { // Run hooks program.run_hook_on_begin(); @@ -105,7 +105,7 @@ where #[cfg(not(feature = "async"))] pub fn exec(program: &'static Program) -> Result where - C: ProgramCollect + std::fmt::Display, + C: ProgramCollect, { // Run hooks program.run_hook_on_begin(); diff --git a/mingling_core/src/program/hook.rs b/mingling_core/src/program/hook.rs index a5c5d38..afc253c 100644 --- a/mingling_core/src/program/hook.rs +++ b/mingling_core/src/program/hook.rs @@ -1,4 +1,4 @@ -use std::{any::Any, fmt::Display}; +use std::any::Any; use crate::{AnyOutput, Program, ProgramCollect, RenderResult}; @@ -32,47 +32,9 @@ where pub finish: Option i32>, } -#[derive(Default)] -pub struct ProgramAnonymousHook { - /// Executes when the program starts running - pub begin: Option, - - /// Executes before the program dispatches - pub pre_dispatch: Option)>, - - /// Executes after the program dispatches - pub post_dispatch: Option, - - /// Executes before the type enters the chain - pub pre_chain: Option, - - /// Executes after the chain processing for the type ends - pub post_chain: Option, - - /// Executes before the type enters the renderer - pub pre_render: Option, - - /// Executes after the type enters the renderer - pub post_render: Option, - - /// Executes before the program ends - pub finish: Option i32>, -} - impl Program where C: ProgramCollect, -{ - /// Adds an anonymous hook to the program. The hook will be called at the appropriate - /// lifecycle events, but receives string representations instead of typed references. - pub fn with_hook_anonymous(&mut self, hook: ProgramAnonymousHook) { - self.anonymous_hooks.push(hook); - } -} - -impl Program -where - C: ProgramCollect + Display, { /// Adds a typed hook to the program. The hook will be called at the appropriate /// lifecycle events. @@ -90,11 +52,6 @@ where begin() } } - for anonymous_hook in &self.anonymous_hooks { - if let Some(begin) = anonymous_hook.begin { - begin() - } - } } pub(crate) fn run_hook_pre_dispatch(&self, args: &Vec) { @@ -107,11 +64,6 @@ where pre_dispatch(args) } } - for anonymous_hook in &self.anonymous_hooks { - if let Some(pre_dispatch) = anonymous_hook.pre_dispatch { - pre_dispatch(args) - } - } } pub(crate) fn run_hook_post_dispatch(&self, entry: &C) { @@ -124,11 +76,6 @@ where post_dispatch(entry) } } - for anonymous_hook in &self.anonymous_hooks { - if let Some(post_dispatch) = anonymous_hook.post_dispatch { - post_dispatch(entry.to_string().as_str()) - } - } } pub(crate) fn run_hook_pre_chain(&self, input: &C, raw: &dyn Any) { @@ -141,11 +88,6 @@ where pre_chain(input, raw) } } - for anonymous_hook in &self.anonymous_hooks { - if let Some(pre_chain) = anonymous_hook.pre_chain { - pre_chain(input.to_string().as_str(), raw) - } - } } pub(crate) fn run_hook_post_chain(&self, output: &AnyOutput) { @@ -158,11 +100,6 @@ where post_chain(output) } } - for anonymous_hook in &self.anonymous_hooks { - if let Some(post_chain) = anonymous_hook.post_chain { - post_chain(output.member_id.to_string().as_str()) - } - } } pub(crate) fn run_hook_pre_render(&self, input: &C, raw: &dyn Any) { @@ -175,11 +112,6 @@ where pre_render(input, raw) } } - for anonymous_hook in &self.anonymous_hooks { - if let Some(pre_render) = anonymous_hook.pre_render { - pre_render(input.to_string().as_str(), raw) - } - } } pub(crate) fn run_hook_post_render(&self, result: &RenderResult) { @@ -192,11 +124,6 @@ where post_render(result) } } - for anonymous_hook in &self.anonymous_hooks { - if let Some(post_render) = anonymous_hook.post_render { - post_render(result) - } - } } pub(crate) fn run_hook_finish(&self) -> i32 { @@ -213,14 +140,6 @@ where } } } - for anonymous_hook in &self.anonymous_hooks { - if let Some(finish) = anonymous_hook.finish { - exit_code = finish(); - if exit_code != 0 { - return exit_code; - } - } - } exit_code } } @@ -291,67 +210,3 @@ where self } } - -impl ProgramAnonymousHook { - /// Creates a new empty hook set with no handlers. - pub fn empty() -> Self { - Self { - begin: None, - pre_dispatch: None, - post_dispatch: None, - pre_chain: None, - post_chain: None, - pre_render: None, - post_render: None, - finish: None, - } - } - - /// Sets the handler for the `begin` event. - pub fn on_begin(mut self, handler: fn()) -> Self { - let _ = self.begin.insert(handler); - self - } - - /// Sets the handler for the `pre_dispatch` event. - pub fn on_pre_dispatch(mut self, handler: fn(args: &Vec)) -> Self { - let _ = self.pre_dispatch.insert(handler); - self - } - - /// Sets the handler for the `post_dispatch` event. - pub fn on_post_dispatch(mut self, handler: fn(entry_name: &str)) -> Self { - let _ = self.post_dispatch.insert(handler); - self - } - - /// Sets the handler for the `pre_chain` event. - pub fn on_pre_chain(mut self, handler: fn(input_name: &str, raw: &dyn Any)) -> Self { - let _ = self.pre_chain.insert(handler); - self - } - - /// Sets the handler for the `post_chain` event. - pub fn on_post_chain(mut self, handler: fn(output_name: &str)) -> Self { - let _ = self.post_chain.insert(handler); - self - } - - /// Sets the handler for the `pre_render` event. - pub fn on_pre_render(mut self, handler: fn(input_name: &str, raw: &dyn Any)) -> Self { - let _ = self.pre_render.insert(handler); - self - } - - /// Sets the handler for the `post_render` event. - pub fn on_post_render(mut self, handler: fn(result: &RenderResult)) -> Self { - let _ = self.post_render.insert(handler); - self - } - - /// Sets the handler for the `finish` event. - pub fn on_finish(mut self, handler: fn() -> i32) -> Self { - let _ = self.finish.insert(handler); - self - } -} diff --git a/mingling_core/src/program/setup.rs b/mingling_core/src/program/setup.rs index 289dfee..0fe8070 100644 --- a/mingling_core/src/program/setup.rs +++ b/mingling_core/src/program/setup.rs @@ -1,18 +1,5 @@ use crate::{ProgramCollect, program::Program}; -mod basic; -pub use basic::*; - -#[doc(hidden)] -pub mod exit_code_control; -pub use exit_code_control::ExitCodeSetup; - -#[cfg(feature = "general_renderer")] -mod general_renderer; - -#[cfg(feature = "general_renderer")] -pub use general_renderer::*; - pub trait ProgramSetup where C: ProgramCollect, diff --git a/mingling_core/src/program/setup/basic.rs b/mingling_core/src/program/setup/basic.rs deleted file mode 100644 index 9bd4056..0000000 --- a/mingling_core/src/program/setup/basic.rs +++ /dev/null @@ -1,31 +0,0 @@ -use crate::{ - ProgramCollect, - program::{Program, setup::ProgramSetup}, -}; - -/// Performs basic program initialization: -/// -/// - Collects `--quiet` flag to control message rendering -/// - Collects `--help` flag to enable help mode -/// - Collects `--confirm` flag to skip user confirmation -pub struct BasicProgramSetup; - -impl ProgramSetup for BasicProgramSetup -where - C: ProgramCollect, -{ - fn setup(&mut self, program: &mut Program) { - program.global_flag(["--quiet", "-q"], |p| { - p.stdout_setting.render_output = false; - p.stdout_setting.error_output = false; - }); - - program.global_flag(["--help", "-h"], |p| { - p.user_context.help = true; - }); - - program.global_flag(["--confirm", "-C"], |p| { - p.user_context.confirm = true; - }); - } -} diff --git a/mingling_core/src/program/setup/exit_code_control.rs b/mingling_core/src/program/setup/exit_code_control.rs deleted file mode 100644 index 20cd9b2..0000000 --- a/mingling_core/src/program/setup/exit_code_control.rs +++ /dev/null @@ -1,35 +0,0 @@ -use std::sync::atomic::{AtomicI32, Ordering}; - -use crate::{ProgramCollect, hook::ProgramAnonymousHook, setup::ProgramSetup}; - -static EXIT_CODE: AtomicI32 = AtomicI32::new(0); - -/// 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; - -impl ProgramSetup for ExitCodeSetup -where - C: ProgramCollect, -{ - fn setup(&mut self, program: &mut crate::Program) { - program.with_hook_anonymous(ProgramAnonymousHook::empty().on_finish(current_exit_code)); - } -} - -/// Updates the program's exit code. -/// -/// This function sets the value that will be returned when the program exits. -/// The new code will take effect immediately and be used when the program finishes. -pub fn update_exit_code(code: i32) { - EXIT_CODE.store(code, Ordering::SeqCst); -} - -/// Returns the current exit code. -/// -/// This function queries the value that will be returned when the program exits. -pub fn current_exit_code() -> i32 { - EXIT_CODE.load(Ordering::SeqCst) -} diff --git a/mingling_core/src/program/setup/general_renderer.rs b/mingling_core/src/program/setup/general_renderer.rs deleted file mode 100644 index d2666da..0000000 --- a/mingling_core/src/program/setup/general_renderer.rs +++ /dev/null @@ -1,57 +0,0 @@ -use crate::{ - ProgramCollect, - program::{Program, setup::ProgramSetup}, -}; - -/// Sets up the general renderer for the program: -/// -/// - Adds a `--renderer` global argument to specify the renderer type -pub struct GeneralRendererSimpleSetup; - -impl ProgramSetup for GeneralRendererSimpleSetup -where - C: ProgramCollect, -{ - fn setup(&mut self, program: &mut Program) { - program.global_argument("--renderer", |p, renderer| { - p.general_renderer_name = renderer.into(); - }); - } -} - -/// Sets up the general renderer for the program: -/// -/// - Adds global flags to specify the renderer type: -/// * `--json` for JSON output -/// * `--json-pretty` for pretty-printed JSON output -/// * `--yaml` for YAML output -/// * `--toml` for TOML output -/// * `--ron` for RON output -/// * `--ron-pretty` for pretty-printed RON output -pub struct GeneralRendererSetup; - -impl ProgramSetup for GeneralRendererSetup -where - C: ProgramCollect, -{ - fn setup(&mut self, program: &mut Program) { - program.global_flag("--json", |p| { - p.general_renderer_name = crate::GeneralRendererSetting::Json - }); - program.global_flag("--json-pretty", |p| { - p.general_renderer_name = crate::GeneralRendererSetting::JsonPretty; - }); - program.global_flag("--yaml", |p| { - p.general_renderer_name = crate::GeneralRendererSetting::Yaml; - }); - program.global_flag("--toml", |p| { - p.general_renderer_name = crate::GeneralRendererSetting::Toml; - }); - program.global_flag("--ron", |p| { - p.general_renderer_name = crate::GeneralRendererSetting::Ron; - }); - program.global_flag("--ron-pretty", |p| { - p.general_renderer_name = crate::GeneralRendererSetting::RonPretty; - }); - } -} -- cgit