From 7ce68cd11516bd7cf037ecea99a92aee7c31b2c3 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sat, 28 Mar 2026 00:47:46 +0800 Subject: Add initial Mingling framework codebase --- mingling/src/any.rs | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 mingling/src/any.rs (limited to 'mingling/src/any.rs') diff --git a/mingling/src/any.rs b/mingling/src/any.rs new file mode 100644 index 0000000..a74cd54 --- /dev/null +++ b/mingling/src/any.rs @@ -0,0 +1,72 @@ +#[cfg(feature = "serde_renderer")] +use serde::Serialize; + +use crate::error::ChainProcessError; + +pub type ChainProcess = Result; + +#[derive(Debug)] +pub struct AnyOutput { + inner: Box, + type_id: std::any::TypeId, +} + +impl AnyOutput { + #[cfg(feature = "serde_renderer")] + pub fn new(value: T) -> Self + where + T: Send + Serialize + 'static, + { + Self { + inner: Box::new(value), + type_id: std::any::TypeId::of::(), + } + } + + #[cfg(not(feature = "serde_renderer"))] + pub fn new(value: T) -> Self + where + T: Send + 'static, + { + Self { + inner: Box::new(value), + type_id: std::any::TypeId::of::(), + } + } + + pub fn downcast(self) -> Result { + if self.type_id == std::any::TypeId::of::() { + Ok(*self.inner.downcast::().unwrap()) + } else { + Err(self) + } + } + + pub fn is(&self) -> bool { + self.type_id == std::any::TypeId::of::() + } + + /// Route the output to the next Chain + pub fn route_chain(self) -> ChainProcess { + Ok(self) + } + + /// Route the output to the Renderer, ending execution + pub fn route_renderer(self) -> ChainProcess { + Err(ChainProcessError::Broken(self)) + } +} + +impl std::ops::Deref for AnyOutput { + type Target = dyn std::any::Any + Send + 'static; + + fn deref(&self) -> &Self::Target { + &*self.inner + } +} + +impl std::ops::DerefMut for AnyOutput { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut *self.inner + } +} -- cgit