From 3785202ec5b949cba5501b20729b16f4c29ea626 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 22 Apr 2026 13:27:43 +0800 Subject: Add new_with_args and dispatch_args_dynamic to Program Remove Display bound from Group type parameter and delete dispatcher_render macro. This is a breaking change. --- mingling_core/src/program/exec.rs | 76 +++++++++++++++------------------------ 1 file changed, 29 insertions(+), 47 deletions(-) (limited to 'mingling_core/src/program/exec.rs') diff --git a/mingling_core/src/program/exec.rs b/mingling_core/src/program/exec.rs index 8ab2036..68a694e 100644 --- a/mingling_core/src/program/exec.rs +++ b/mingling_core/src/program/exec.rs @@ -1,7 +1,5 @@ #![allow(clippy::borrowed_box)] -use std::fmt::Display; - use crate::{ AnyOutput, ChainProcess, Dispatcher, Next, Program, ProgramCollect, RenderResult, error::ProgramInternalExecuteError, @@ -16,30 +14,10 @@ pub async fn exec( ) -> Result where C: ProgramCollect, - G: Display, { - let mut current; + let mut current = dispatch_args_dynamic(program, program.args.clone())?; let mut stop_next = false; - // Match user input - match match_user_input(program, program.args.clone()) { - Ok((dispatcher, args)) => { - // Entry point - current = match dispatcher.begin(args) { - ChainProcess::Ok((any, Next::Renderer)) => { - return Ok(render::(program, any)); - } - ChainProcess::Ok((any, Next::Chain)) => any, - ChainProcess::Err(e) => return Err(e.into()), - }; - } - Err(ProgramInternalExecuteError::DispatcherNotFound) => { - // No matching Dispatcher is found - current = C::build_dispatcher_not_found(program.args.clone()); - } - Err(e) => return Err(e), - }; - loop { let final_exec = stop_next; @@ -76,30 +54,10 @@ where pub fn exec(program: &Program) -> Result where C: ProgramCollect, - G: Display, { - let mut current; + let mut current = dispatch_args_dynamic(program, program.args.clone())?; let mut stop_next = false; - // Match user input - match match_user_input(program, program.args.clone()) { - Ok((dispatcher, args)) => { - // Entry point - current = match dispatcher.begin(args) { - ChainProcess::Ok((any, Next::Renderer)) => { - return Ok(render::(program, any)); - } - ChainProcess::Ok((any, Next::Chain)) => any, - ChainProcess::Err(e) => return Err(e.into()), - }; - } - Err(ProgramInternalExecuteError::DispatcherNotFound) => { - // No matching Dispatcher is found - current = C::build_dispatcher_not_found(program.args.clone()); - } - Err(e) => return Err(e), - }; - loop { let final_exec = stop_next; @@ -132,15 +90,39 @@ where Ok(RenderResult::default()) } +/// Dynamically dispatch input arguments to registered entry types +pub(crate) fn dispatch_args_dynamic( + program: &Program, + args: Vec, +) -> Result, ProgramInternalExecuteError> +where + C: ProgramCollect, +{ + let next = match match_user_input(program, args) { + Ok((dispatcher, args)) => { + // Entry point + match dispatcher.begin(args) { + ChainProcess::Ok((any, _)) => any, + ChainProcess::Err(e) => return Err(e.into()), + } + } + Err(ProgramInternalExecuteError::DispatcherNotFound) => { + // No matching Dispatcher is found + C::build_dispatcher_not_found(program.args.clone()) + } + Err(e) => return Err(e), + }; + Ok(next) +} + /// Match user input against registered dispatchers and return the matched dispatcher and remaining arguments. #[allow(clippy::type_complexity)] -pub fn match_user_input( +pub(crate) fn match_user_input( program: &Program, args: Vec, ) -> Result<(&(dyn Dispatcher + Send + Sync), Vec), ProgramInternalExecuteError> where C: ProgramCollect, - G: Display, { let nodes = program.get_nodes(); let command = format!("{} ", args.join(" ")); @@ -180,7 +162,7 @@ where #[inline(always)] #[allow(unused_variables)] -fn render, G: Display>( +fn render, G>( program: &Program, any: AnyOutput, ) -> RenderResult { -- cgit