From 25a164f74c011e6e78846f226cbd7a8bd87db92f Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sun, 29 Mar 2026 21:33:04 +0800 Subject: Replace ChainProcess type alias with enum --- mingling/src/any.rs | 64 +++++++++++++++++++++++++++++++++++++++++--- mingling/src/program/exec.rs | 56 ++++++++++---------------------------- 2 files changed, 74 insertions(+), 46 deletions(-) (limited to 'mingling/src') diff --git a/mingling/src/any.rs b/mingling/src/any.rs index 204a7e3..1bce96a 100644 --- a/mingling/src/any.rs +++ b/mingling/src/any.rs @@ -3,8 +3,6 @@ use serde::Serialize; use crate::error::ChainProcessError; -pub type ChainProcess = Result; - #[derive(Debug)] pub struct AnyOutput { inner: Box, @@ -48,12 +46,12 @@ impl AnyOutput { /// Route the output to the next Chain pub fn route_chain(self) -> ChainProcess { - Ok(self) + ChainProcess::Ok((self, Next::Chain)) } /// Route the output to the Renderer, ending execution pub fn route_renderer(self) -> ChainProcess { - Err(ChainProcessError::Broken(self)) + ChainProcess::Ok((self, Next::Renderer)) } } @@ -70,3 +68,61 @@ impl std::ops::DerefMut for AnyOutput { &mut *self.inner } } + +pub enum ChainProcess { + Ok((AnyOutput, Next)), + Err(ChainProcessError), +} + +pub enum Next { + Chain, + Renderer, +} + +impl ChainProcess { + pub fn is_next(&self) -> bool { + matches!(self, Self::Ok(_)) + } + + pub fn is_err(&self) -> bool { + matches!(self, Self::Err(_)) + } + + pub fn next(&self) -> Option<&Next> { + match self { + Self::Ok((_, next)) => Some(next), + Self::Err(_) => None, + } + } + + pub fn err(&self) -> Option<&ChainProcessError> { + match self { + Self::Ok(_) => None, + Self::Err(err) => Some(err), + } + } + + pub fn unwrap(self) -> (AnyOutput, Next) { + match self { + Self::Ok(tuple) => tuple, + Self::Err(_) => panic!("called `ChainProcess2::unwrap()` on an `Error` value"), + } + } + + pub fn unwrap_or(self, default: (AnyOutput, Next)) -> (AnyOutput, Next) { + match self { + Self::Ok(tuple) => tuple, + Self::Err(_) => default, + } + } + + pub fn unwrap_or_else(self, f: F) -> (AnyOutput, Next) + where + F: FnOnce(ChainProcessError) -> (AnyOutput, Next), + { + match self { + Self::Ok(tuple) => tuple, + Self::Err(err) => f(err), + } + } +} 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( // Entry point let (dispatcher, args) = matched; - let mut current = match handle_chain_process::(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::(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::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::(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( else { let disp: Box = Box::new(RendererNotFound); - match handle_chain_process::(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::(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(any: AnyOutput) -> RenderResult { render_result } -fn handle_chain_process( - process: ChainProcess, -) -> Result { - match process { - Ok(any) => Ok(Next::AnyOutput(any)), - Err(e) => match e { - ChainProcessError::Broken(any_output) => { - let render_result = render::(any_output); - Ok(Next::RenderResult(render_result)) - } - _ => Err(e.into()), - }, - } -} - // Get all registered dispatcher names from the program fn get_nodes(program: &Program) -> Vec<(String, &Box)> { program @@ -147,8 +124,3 @@ fn get_nodes(program: &Program) -> Vec<(String, &Box