diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-03-29 21:33:04 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-03-29 21:33:04 +0800 |
| commit | 25a164f74c011e6e78846f226cbd7a8bd87db92f (patch) | |
| tree | fc7b9ab606446291c77b48a31cba628af2fa5873 /mingling/src/program | |
| parent | 1f98228447171b7a65d5fd47f9e989cc55cf7283 (diff) | |
Replace ChainProcess type alias with enum
Diffstat (limited to 'mingling/src/program')
| -rw-r--r-- | mingling/src/program/exec.rs | 56 |
1 files changed, 14 insertions, 42 deletions
diff --git a/mingling/src/program/exec.rs b/mingling/src/program/exec.rs index 8277f69..853bff1 100644 --- a/mingling/src/program/exec.rs +++ b/mingling/src/program/exec.rs @@ -1,8 +1,8 @@ #![allow(clippy::borrowed_box)] use crate::{ - AnyOutput, ChainProcess, Dispatcher, Program, ProgramCollect, RenderResult, - error::{ChainProcessError, ProgramInternalExecuteError}, + AnyOutput, ChainProcess, Dispatcher, Next, Program, ProgramCollect, RenderResult, + error::ProgramInternalExecuteError, hint::{DispatcherNotFound, RendererNotFound}, }; @@ -25,20 +25,20 @@ pub async fn exec<C: ProgramCollect>( // Entry point let (dispatcher, args) = matched; - let mut current = match handle_chain_process::<C>(dispatcher.begin(args)) { - Ok(Next::RenderResult(render_result)) => return Ok(render_result), - Ok(Next::AnyOutput(any)) => any, - Err(e) => return Err(e), + let mut current = match dispatcher.begin(args) { + ChainProcess::Ok((any, Next::Renderer)) => return Ok(render::<C>(any)), + ChainProcess::Ok((any, Next::Chain)) => any, + ChainProcess::Err(e) => return Err(e.into()), }; loop { current = { // If a chain exists, execute as a chain if C::has_chain(¤t) { - match handle_chain_process::<C>(C::do_chain(current).await) { - Ok(Next::RenderResult(render_result)) => return Ok(render_result), - Ok(Next::AnyOutput(any)) => any, - Err(e) => return Err(e), + match C::do_chain(current).await { + ChainProcess::Ok((any, Next::Renderer)) => return Ok(render::<C>(any)), + ChainProcess::Ok((any, Next::Chain)) => any, + ChainProcess::Err(e) => return Err(e.into()), } } // If no chain exists, attempt to render @@ -51,22 +51,14 @@ pub async fn exec<C: ProgramCollect>( else { let disp: Box<dyn Dispatcher> = Box::new(RendererNotFound); - match handle_chain_process::<C>(disp.begin(vec![format!("{:?}", current.type_id)])) - { - Ok(Next::AnyOutput(any)) => any, - Ok(Next::RenderResult(result)) => return Ok(result), - Err(e) => return Err(e), + match disp.begin(vec![format!("{:?}", current.type_id)]) { + ChainProcess::Ok((any, Next::Renderer)) => return Ok(render::<C>(any)), + ChainProcess::Ok((any, Next::Chain)) => any, + ChainProcess::Err(e) => return Err(e.into()), } } }; - - // If the dispatcher cannot find the next chain, end execution - if C::has_chain(¤t) { - break; - } } - - Ok(RenderResult::default()) } /// Match user input against registered dispatchers and return the matched dispatcher and remaining arguments. @@ -116,21 +108,6 @@ fn render<C: ProgramCollect>(any: AnyOutput) -> RenderResult { render_result } -fn handle_chain_process<C: ProgramCollect>( - process: ChainProcess, -) -> Result<Next, ProgramInternalExecuteError> { - match process { - Ok(any) => Ok(Next::AnyOutput(any)), - Err(e) => match e { - ChainProcessError::Broken(any_output) => { - let render_result = render::<C>(any_output); - Ok(Next::RenderResult(render_result)) - } - _ => Err(e.into()), - }, - } -} - // Get all registered dispatcher names from the program fn get_nodes<C: ProgramCollect>(program: &Program<C>) -> Vec<(String, &Box<dyn Dispatcher>)> { program @@ -147,8 +124,3 @@ fn get_nodes<C: ProgramCollect>(program: &Program<C>) -> Vec<(String, &Box<dyn D }) .collect() } - -enum Next { - RenderResult(RenderResult), - AnyOutput(AnyOutput), -} |
