diff options
Diffstat (limited to 'src/systems')
| -rw-r--r-- | src/systems/cmd/cmd_system.rs | 34 | ||||
| -rw-r--r-- | src/systems/cmd/macros.rs | 10 | ||||
| -rw-r--r-- | src/systems/render/render_system.rs | 5 |
3 files changed, 36 insertions, 13 deletions
diff --git a/src/systems/cmd/cmd_system.rs b/src/systems/cmd/cmd_system.rs index 030e711..1fe6e66 100644 --- a/src/systems/cmd/cmd_system.rs +++ b/src/systems/cmd/cmd_system.rs @@ -42,12 +42,34 @@ where return Err(CmdProcessError::RendererOverrideButRequestHelp); } - let (data, type_name) = Self::process(args, ctx).await?; + let (data, type_id) = Self::process(args, ctx).await?; let renderer_override = renderer_override.as_str(); // Serialize the data based on its concrete type - let render_result = include!("../render/_override_renderer_entry.rs"); + let render_result: Result<JVRenderResult, CmdRenderError> = if type_id + == std::any::TypeId::of::<crate::cmds::out::hex::JVHexOutput>() + { + let concrete_data = data + .downcast::<crate::cmds::out::hex::JVHexOutput>() + .map_err(|_| CmdProcessError::DowncastFailed)?; + include!("../render/_override_renderer_dispatcher.rs") + } else if type_id + == std::any::TypeId::of::<crate::cmds::out::mappings::JVMappingsOutput>() + { + let concrete_data = data + .downcast::<crate::cmds::out::mappings::JVMappingsOutput>() + .map_err(|_| CmdProcessError::DowncastFailed)?; + include!("../render/_override_renderer_dispatcher.rs") + } else if type_id == std::any::TypeId::of::<crate::cmds::out::status::JVStatusOutput>() + { + let concrete_data = data + .downcast::<crate::cmds::out::status::JVStatusOutput>() + .map_err(|_| CmdProcessError::DowncastFailed)?; + include!("../render/_override_renderer_dispatcher.rs") + } else { + return Err(CmdProcessError::NoMatchingCommand); + }; match render_result { Ok(r) => Ok(r), @@ -71,8 +93,8 @@ where return Ok(r); } - let (data, id_str) = Self::process(args, ctx).await?; - match render(data, id_str).await { + let (data, id) = Self::process(args, ctx).await?; + match render(data, id).await { Ok(r) => Ok(r), Err(e) => Err(CmdProcessError::Render(e)), } @@ -82,7 +104,7 @@ where fn process( args: Vec<String>, ctx: JVCommandContext, - ) -> impl Future<Output = Result<(Box<dyn Any + Send + 'static>, String), CmdProcessError>> + Send + ) -> impl Future<Output = Result<(Box<dyn Any + Send + 'static>, TypeId), CmdProcessError>> + Send { async move { let mut full_args = vec!["jv".to_string()]; @@ -131,7 +153,7 @@ where fn exec( input: Input, collect: Collect, - ) -> impl Future<Output = Result<(Box<dyn Any + Send + 'static>, String), CmdExecuteError>> + Send; + ) -> impl Future<Output = Result<(Box<dyn Any + Send + 'static>, TypeId), CmdExecuteError>> + Send; /// Get output type mapping fn get_output_type_mapping() -> HashMap<String, TypeId>; diff --git a/src/systems/cmd/macros.rs b/src/systems/cmd/macros.rs index 23a277c..454ad30 100644 --- a/src/systems/cmd/macros.rs +++ b/src/systems/cmd/macros.rs @@ -12,6 +12,7 @@ /// Then paste the following content into your code /// /// ```ignore +/// use std::any::TypeId; /// use cmd_system_macros::exec; /// use crate::{ /// cmd_output, @@ -71,7 +72,7 @@ /// async fn exec( /// input: In, /// collect: Collect, -/// ) -> Result<(Box<dyn std::any::Any + Send + 'static>, String), CmdExecuteError> { +/// ) -> Result<(Box<dyn std::any::Any + Send + 'static>, TypeId), CmdExecuteError> { /// todo!(); /// /// // Use the following method to return results @@ -82,6 +83,7 @@ /// Of course, you can also use the comment-free version /// /// ```ignore +/// use std::any::TypeId; /// use cmd_system_macros::exec; /// use crate::{ /// cmd_output, @@ -115,7 +117,7 @@ /// async fn exec( /// input: In, /// collect: Collect, -/// ) -> Result<(Box<dyn std::any::Any + Send + 'static>, String), CmdExecuteError> { +/// ) -> Result<(Box<dyn std::any::Any + Send + 'static>, TypeId), CmdExecuteError> { /// todo!(); /// cmd_output!(output, JVCustomOutput) /// } @@ -145,7 +147,7 @@ macro_rules! command_template { input: In, collect: Collect, ) -> Result< - (Box<dyn std::any::Any + Send + 'static>, String), + (Box<dyn std::any::Any + Send + 'static>, std::any::TypeId), $crate::systems::cmd::errors::CmdExecuteError, > { exec(input, collect).await @@ -163,7 +165,7 @@ macro_rules! cmd_output { ($v:expr, $t:ty) => { Ok(( Box::new($v) as Box<dyn std::any::Any + Send + 'static>, - stringify!($t).to_string(), + std::any::TypeId::of::<$t>(), )) }; } diff --git a/src/systems/render/render_system.rs b/src/systems/render/render_system.rs index 7371e7a..36467bb 100644 --- a/src/systems/render/render_system.rs +++ b/src/systems/render/render_system.rs @@ -1,4 +1,4 @@ -use std::any::Any; +use std::any::{Any, TypeId}; use crate::systems::{ cmd::errors::CmdRenderError, @@ -7,8 +7,7 @@ use crate::systems::{ pub async fn render( data: Box<dyn Any + Send + 'static>, - type_name: String, + type_id: TypeId, ) -> Result<JVRenderResult, CmdRenderError> { - let type_name_str = type_name.as_str(); include!("_specific_renderer_matching.rs") } |
