From d4634b01e3f33b3ee52b1501f5ade739a1796d08 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sat, 25 Apr 2026 19:02:17 +0800 Subject: Remove redundant generic parameter from Program struct --- mingling_core/src/program/exec.rs | 41 ++++++++++------------ mingling_core/src/program/flag.rs | 9 ++--- mingling_core/src/program/setup.rs | 8 ++--- mingling_core/src/program/setup/basic.rs | 7 ++-- .../src/program/setup/general_renderer.rs | 12 +++---- 5 files changed, 31 insertions(+), 46 deletions(-) (limited to 'mingling_core/src/program') diff --git a/mingling_core/src/program/exec.rs b/mingling_core/src/program/exec.rs index 68a694e..c1eada5 100644 --- a/mingling_core/src/program/exec.rs +++ b/mingling_core/src/program/exec.rs @@ -9,11 +9,9 @@ use crate::{ pub mod error; #[cfg(feature = "async")] -pub async fn exec( - program: &Program, -) -> Result +pub async fn exec(program: &Program) -> Result where - C: ProgramCollect, + C: ProgramCollect, { let mut current = dispatch_args_dynamic(program, program.args.clone())?; let mut stop_next = false; @@ -26,7 +24,7 @@ where if C::has_chain(¤t) { match C::do_chain(current).await { ChainProcess::Ok((any, Next::Renderer)) => { - return Ok(render::(program, any)); + return Ok(render::(program, any)); } ChainProcess::Ok((any, Next::Chain)) => any, ChainProcess::Err(e) => return Err(e.into()), @@ -34,7 +32,7 @@ where } // If no chain exists, attempt to render else if C::has_renderer(¤t) { - return Ok(render::(program, current)); + return Ok(render::(program, current)); } // No renderer exists else { @@ -51,9 +49,9 @@ where } #[cfg(not(feature = "async"))] -pub fn exec(program: &Program) -> Result +pub fn exec(program: &Program) -> Result where - C: ProgramCollect, + C: ProgramCollect, { let mut current = dispatch_args_dynamic(program, program.args.clone())?; let mut stop_next = false; @@ -66,7 +64,7 @@ where if C::has_chain(¤t) { match C::do_chain(current) { ChainProcess::Ok((any, Next::Renderer)) => { - return Ok(render::(program, any)); + return Ok(render::(program, any)); } ChainProcess::Ok((any, Next::Chain)) => any, ChainProcess::Err(e) => return Err(e.into()), @@ -74,7 +72,7 @@ where } // If no chain exists, attempt to render else if C::has_renderer(¤t) { - return Ok(render::(program, current)); + return Ok(render::(program, current)); } // No renderer exists else { @@ -91,12 +89,12 @@ where } /// Dynamically dispatch input arguments to registered entry types -pub(crate) fn dispatch_args_dynamic( - program: &Program, +pub(crate) fn dispatch_args_dynamic( + program: &Program, args: Vec, -) -> Result, ProgramInternalExecuteError> +) -> Result, ProgramInternalExecuteError> where - C: ProgramCollect, + C: ProgramCollect, { let next = match match_user_input(program, args) { Ok((dispatcher, args)) => { @@ -117,18 +115,18 @@ where /// Match user input against registered dispatchers and return the matched dispatcher and remaining arguments. #[allow(clippy::type_complexity)] -pub(crate) fn match_user_input( - program: &Program, +pub(crate) fn match_user_input( + program: &Program, args: Vec, -) -> Result<(&(dyn Dispatcher + Send + Sync), Vec), ProgramInternalExecuteError> +) -> Result<(&(dyn Dispatcher + Send + Sync), Vec), ProgramInternalExecuteError> where - C: ProgramCollect, + C: ProgramCollect, { let nodes = program.get_nodes(); let command = format!("{} ", args.join(" ")); // Find all nodes that match the command prefix - let matching_nodes: Vec<&(String, &(dyn Dispatcher + Send + Sync))> = nodes + let matching_nodes: Vec<&(String, &(dyn Dispatcher + Send + Sync))> = nodes .iter() // Also add a space to the node string to ensure consistent matching logic .filter(|(node_str, _)| command.starts_with(&format!("{} ", node_str))) @@ -162,10 +160,7 @@ where #[inline(always)] #[allow(unused_variables)] -fn render, G>( - program: &Program, - any: AnyOutput, -) -> RenderResult { +fn render>(program: &Program, any: AnyOutput) -> RenderResult { #[cfg(not(feature = "general_renderer"))] { let mut render_result = RenderResult::default(); diff --git a/mingling_core/src/program/flag.rs b/mingling_core/src/program/flag.rs index f44cf33..0ab8d1c 100644 --- a/mingling_core/src/program/flag.rs +++ b/mingling_core/src/program/flag.rs @@ -1,5 +1,3 @@ -use std::fmt::Display; - use crate::{Program, ProgramCollect}; /// A wrapper for a collection of static string slices representing command-line flags or arguments. @@ -474,15 +472,14 @@ mod tests { } } -impl Program +impl Program where C: ProgramCollect, - G: Display, { /// Registers a global argument (with value) and its handler. pub fn global_argument(&mut self, arguments: A, mut do_fn: F) where - F: FnMut(&mut Program, String), + F: FnMut(&mut Program, String), A: Into, { let flag = arguments.into(); @@ -498,7 +495,7 @@ where /// Registers a global flag (boolean) and its handler. pub fn global_flag(&mut self, flag: A, mut do_fn: F) where - F: FnMut(&mut Program), + F: FnMut(&mut Program), A: Into, { let flag = flag.into(); diff --git a/mingling_core/src/program/setup.rs b/mingling_core/src/program/setup.rs index f095ed3..86228b9 100644 --- a/mingling_core/src/program/setup.rs +++ b/mingling_core/src/program/setup.rs @@ -8,19 +8,19 @@ mod general_renderer; #[cfg(feature = "general_renderer")] pub use general_renderer::*; -pub trait ProgramSetup +pub trait ProgramSetup where C: ProgramCollect, { - fn setup(&mut self, program: &mut Program); + fn setup(&mut self, program: &mut Program); } -impl Program +impl Program where C: ProgramCollect, { /// Load and execute init logic - pub fn with_setup + 'static>(&mut self, mut setup: S) -> S { + pub fn with_setup + 'static>(&mut self, mut setup: S) -> S { S::setup(&mut setup, self); setup } diff --git a/mingling_core/src/program/setup/basic.rs b/mingling_core/src/program/setup/basic.rs index 8316a33..43c14b9 100644 --- a/mingling_core/src/program/setup/basic.rs +++ b/mingling_core/src/program/setup/basic.rs @@ -1,5 +1,3 @@ -use std::fmt::Display; - use crate::{ ProgramCollect, program::{Program, setup::ProgramSetup}, @@ -12,12 +10,11 @@ use crate::{ /// - Collects `--confirm` flag to skip user confirmation pub struct BasicProgramSetup; -impl ProgramSetup for BasicProgramSetup +impl ProgramSetup for BasicProgramSetup where C: ProgramCollect, - G: Display, { - fn setup(&mut self, program: &mut Program) { + 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; diff --git a/mingling_core/src/program/setup/general_renderer.rs b/mingling_core/src/program/setup/general_renderer.rs index 035f813..2d2ee79 100644 --- a/mingling_core/src/program/setup/general_renderer.rs +++ b/mingling_core/src/program/setup/general_renderer.rs @@ -1,5 +1,3 @@ -use std::fmt::Display; - use crate::{ ProgramCollect, program::{Program, setup::ProgramSetup}, @@ -10,12 +8,11 @@ use crate::{ /// - Adds a `--renderer` global argument to specify the renderer type pub struct GeneralRendererSimpleSetup; -impl ProgramSetup for GeneralRendererSimpleSetup +impl ProgramSetup for GeneralRendererSimpleSetup where C: ProgramCollect, - G: Display, { - fn setup(&mut self, program: &mut Program) { + fn setup(&mut self, program: &mut Program) { program.global_argument("--renderer", |p, renderer| { p.general_renderer_name = renderer.into(); }); @@ -33,12 +30,11 @@ where /// * `--ron-pretty` for pretty-printed RON output pub struct GeneralRendererSetup; -impl ProgramSetup for GeneralRendererSetup +impl ProgramSetup for GeneralRendererSetup where C: ProgramCollect, - G: Display, { - fn setup(&mut self, program: &mut Program) { + fn setup(&mut self, program: &mut Program) { program.global_flag("--json", |p| { p.general_renderer_name = crate::GeneralRendererSetting::Json }); -- cgit