diff options
Diffstat (limited to 'mingling_core/src/any.rs')
| -rw-r--r-- | mingling_core/src/any.rs | 88 |
1 files changed, 8 insertions, 80 deletions
diff --git a/mingling_core/src/any.rs b/mingling_core/src/any.rs index 57eddfb..b077fba 100644 --- a/mingling_core/src/any.rs +++ b/mingling_core/src/any.rs @@ -1,5 +1,3 @@ -use std::fmt::Display; - #[cfg(feature = "general_renderer")] use serde::Serialize; @@ -19,19 +17,13 @@ pub mod group; /// - Under the `general_renderer` feature, the passed value must ensure it implements `serde::Serialize` /// - It is recommended to use the `pack!` macro from [mingling_macros](https://crates.io/crates/mingling_macros) to create types that can be converted to `AnyOutput`, which guarantees runtime safety #[derive(Debug)] -pub struct AnyOutput<G> -where - G: Display, -{ +pub struct AnyOutput<G> { pub(crate) inner: Box<dyn std::any::Any + Send + 'static>, pub type_id: std::any::TypeId, pub member_id: G, } -impl<G> AnyOutput<G> -where - G: Display, -{ +impl<G> AnyOutput<G> { /// Create an AnyOutput from a `Send + Groupped<G> + Serialize` type #[cfg(feature = "general_renderer")] pub fn new<T>(value: T) -> Self @@ -96,10 +88,7 @@ where } } -impl<G> std::ops::Deref for AnyOutput<G> -where - G: Display, -{ +impl<G> std::ops::Deref for AnyOutput<G> { type Target = dyn std::any::Any + Send + 'static; fn deref(&self) -> &Self::Target { @@ -107,10 +96,7 @@ where } } -impl<G> std::ops::DerefMut for AnyOutput<G> -where - G: Display, -{ +impl<G> std::ops::DerefMut for AnyOutput<G> { fn deref_mut(&mut self) -> &mut Self::Target { &mut *self.inner } @@ -122,13 +108,7 @@ where /// - Returns `Ok((`[`AnyOutput`](./struct.AnyOutput.html)`, `[`Next::Chain`](./enum.Next.html)`))` to continue execution with this type next /// - Returns `Ok((`[`AnyOutput`](./struct.AnyOutput.html)`, `[`Next::Renderer`](./enum.Next.html)`))` to render this type next and output to the terminal /// - Returns `Err(`[`ChainProcessError`](./error/enum.ChainProcessError.html)`]` to terminate the program directly -pub enum ChainProcess<G> -where - G: Display, -{ - Ok((AnyOutput<G>, Next)), - Err(ChainProcessError), -} +pub type ChainProcess<G> = Result<(AnyOutput<G>, Next), ChainProcessError>; /// Indicates the next step after processing /// @@ -139,60 +119,8 @@ pub enum Next { Renderer, } -impl<G> ChainProcess<G> -where - G: Display, -{ - /// Returns true if the result is Ok (has a next step) - pub fn is_next(&self) -> bool { - matches!(self, Self::Ok(_)) - } - - /// Returns true if the result is an error - pub fn is_err(&self) -> bool { - matches!(self, Self::Err(_)) - } - - /// Returns the next step if the result is Ok - pub fn next(&self) -> Option<&Next> { - match self { - Self::Ok((_, next)) => Some(next), - Self::Err(_) => None, - } - } - - /// Returns the error if the result is Err - pub fn err(&self) -> Option<&ChainProcessError> { - match self { - Self::Ok(_) => None, - Self::Err(err) => Some(err), - } - } - - /// Unwraps the result, panics if it's an error - pub fn unwrap(self) -> (AnyOutput<G>, Next) { - match self { - Self::Ok(tuple) => tuple, - Self::Err(_) => panic!("called `ChainProcess2::unwrap()` on an `Error` value"), - } - } - - /// Returns the Ok value or a provided default - pub fn unwrap_or(self, default: (AnyOutput<G>, Next)) -> (AnyOutput<G>, Next) { - match self { - Self::Ok(tuple) => tuple, - Self::Err(_) => default, - } - } - - /// Returns the Ok value or computes it from the error - pub fn unwrap_or_else<F>(self, f: F) -> (AnyOutput<G>, Next) - where - F: FnOnce(ChainProcessError) -> (AnyOutput<G>, Next), - { - match self { - Self::Ok(tuple) => tuple, - Self::Err(err) => f(err), - } +impl<G> From<AnyOutput<G>> for ChainProcess<G> { + fn from(value: AnyOutput<G>) -> Self { + ChainProcess::Ok((value, Next::Chain)) } } |
