aboutsummaryrefslogtreecommitdiff
path: root/mingling_core
diff options
context:
space:
mode:
authorWeicao-CatilGrass <1992414357@qq.com>2026-05-13 07:28:02 +0800
committerWeicao-CatilGrass <1992414357@qq.com>2026-05-13 07:28:02 +0800
commit95c6b979ca399671eed8bf9c72f53cfe5d46f431 (patch)
tree8225b39bee4ad3b7ebc8a36ab978d1329f667eb8 /mingling_core
parentfef888b75b2544765aa06808c14490a2af827313 (diff)
Migrate exit code control to resource-based system
Diffstat (limited to 'mingling_core')
-rw-r--r--mingling_core/src/lib.rs10
-rw-r--r--mingling_core/src/program.rs11
-rw-r--r--mingling_core/src/program/exec.rs4
-rw-r--r--mingling_core/src/program/hook.rs147
-rw-r--r--mingling_core/src/program/setup.rs13
-rw-r--r--mingling_core/src/program/setup/basic.rs31
-rw-r--r--mingling_core/src/program/setup/exit_code_control.rs35
-rw-r--r--mingling_core/src/program/setup/general_renderer.rs57
8 files changed, 10 insertions, 298 deletions
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<ProgramHook<C>>,
- pub(crate) anonymous_hooks: Vec<ProgramAnonymousHook>,
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<C> Program<C>
where
- C: ProgramCollect<Enum = C> + std::fmt::Display,
+ C: ProgramCollect<Enum = C>,
{
/// Sets the current program instance and runs the provided async function.
async fn set_instance_and_run<F, Fut>(self, f: F) -> Fut::Output
@@ -261,7 +258,7 @@ where
#[cfg(not(feature = "async"))]
impl<C> Program<C>
where
- C: ProgramCollect<Enum = C> + Display,
+ C: ProgramCollect<Enum = C>,
{
/// Sets the current program instance and runs the provided function.
fn set_instance_and_run<F, R>(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<Self::Enum>;
type RendererNotFound: Groupped<Self::Enum>;
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<C>(
program: &'static Program<C>,
) -> Result<RenderResult, ProgramInternalExecuteError>
where
- C: ProgramCollect<Enum = C> + std::fmt::Display,
+ C: ProgramCollect<Enum = C>,
{
// Run hooks
program.run_hook_on_begin();
@@ -105,7 +105,7 @@ where
#[cfg(not(feature = "async"))]
pub fn exec<C>(program: &'static Program<C>) -> Result<RenderResult, ProgramInternalExecuteError>
where
- C: ProgramCollect<Enum = C> + std::fmt::Display,
+ C: ProgramCollect<Enum = C>,
{
// 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,48 +32,10 @@ where
pub finish: Option<fn() -> i32>,
}
-#[derive(Default)]
-pub struct ProgramAnonymousHook {
- /// Executes when the program starts running
- pub begin: Option<fn()>,
-
- /// Executes before the program dispatches
- pub pre_dispatch: Option<fn(args: &Vec<String>)>,
-
- /// Executes after the program dispatches
- pub post_dispatch: Option<fn(entry_name: &str)>,
-
- /// Executes before the type enters the chain
- pub pre_chain: Option<fn(input_name: &str, raw: &dyn Any)>,
-
- /// Executes after the chain processing for the type ends
- pub post_chain: Option<fn(output_name: &str)>,
-
- /// Executes before the type enters the renderer
- pub pre_render: Option<fn(input_name: &str, raw: &dyn Any)>,
-
- /// Executes after the type enters the renderer
- pub post_render: Option<fn(result: &RenderResult)>,
-
- /// Executes before the program ends
- pub finish: Option<fn() -> i32>,
-}
-
impl<C> Program<C>
where
C: ProgramCollect<Enum = C>,
{
- /// 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<C> Program<C>
-where
- C: ProgramCollect<Enum = C> + Display,
-{
/// Adds a typed hook to the program. The hook will be called at the appropriate
/// lifecycle events.
pub fn with_hook(&mut self, hook: ProgramHook<C>) {
@@ -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<String>) {
@@ -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<C>) {
@@ -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<String>)) -> 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<C>
where
C: ProgramCollect<Enum = C>,
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<C> ProgramSetup<C> for BasicProgramSetup
-where
- C: ProgramCollect<Enum = C>,
-{
- fn setup(&mut self, program: &mut Program<C>) {
- 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<C> ProgramSetup<C> for ExitCodeSetup
-where
- C: ProgramCollect<Enum = C>,
-{
- fn setup(&mut self, program: &mut crate::Program<C>) {
- 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<C> ProgramSetup<C> for GeneralRendererSimpleSetup
-where
- C: ProgramCollect<Enum = C>,
-{
- fn setup(&mut self, program: &mut Program<C>) {
- 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<C> ProgramSetup<C> for GeneralRendererSetup
-where
- C: ProgramCollect<Enum = C>,
-{
- fn setup(&mut self, program: &mut Program<C>) {
- 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;
- });
- }
-}