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 +++++++++++++-------------------- mingling_core/src/program/setup.rs | 4 -- mingling_core/src/program/string_vec.rs | 56 ++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 51 deletions(-) create mode 100644 mingling_core/src/program/string_vec.rs (limited to 'mingling_core/src/program') 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 { diff --git a/mingling_core/src/program/setup.rs b/mingling_core/src/program/setup.rs index 7b534f3..f095ed3 100644 --- a/mingling_core/src/program/setup.rs +++ b/mingling_core/src/program/setup.rs @@ -1,5 +1,3 @@ -use std::fmt::Display; - use crate::{ProgramCollect, program::Program}; mod basic; @@ -13,7 +11,6 @@ pub use general_renderer::*; pub trait ProgramSetup where C: ProgramCollect, - G: Display, { fn setup(&mut self, program: &mut Program); } @@ -21,7 +18,6 @@ where impl Program where C: ProgramCollect, - G: Display, { /// Load and execute init logic pub fn with_setup + 'static>(&mut self, mut setup: S) -> S { diff --git a/mingling_core/src/program/string_vec.rs b/mingling_core/src/program/string_vec.rs new file mode 100644 index 0000000..478ad74 --- /dev/null +++ b/mingling_core/src/program/string_vec.rs @@ -0,0 +1,56 @@ +#[derive(Debug, Clone)] +pub struct StringVec { + vec: Vec, +} + +impl std::ops::Deref for StringVec { + type Target = Vec; + + fn deref(&self) -> &Self::Target { + &self.vec + } +} + +impl From for Vec { + fn from(val: StringVec) -> Self { + val.vec + } +} + +impl From<[&str; N]> for StringVec { + fn from(slice: [&str; N]) -> Self { + StringVec { + vec: slice.iter().map(|&s| s.to_string()).collect(), + } + } +} + +impl From<&[&str]> for StringVec { + fn from(slice: &[&str]) -> Self { + StringVec { + vec: slice.iter().map(|&s| s.to_string()).collect(), + } + } +} + +impl From> for StringVec { + fn from(vec: Vec) -> Self { + StringVec { vec } + } +} + +impl From<&[String]> for StringVec { + fn from(slice: &[String]) -> Self { + StringVec { + vec: slice.to_vec(), + } + } +} + +impl From> for StringVec { + fn from(vec: Vec<&str>) -> Self { + StringVec { + vec: vec.iter().map(|&s| s.to_string()).collect(), + } + } +} -- cgit