From e3aeb6a1c9955eee2c396230317e63becd24ec6a Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Thu, 26 Feb 2026 00:51:45 +0800 Subject: Replace string type identifiers with TypeId for command output matching --- src/cmds/cmd/hexdump.rs | 4 +++- src/cmds/cmd/sheetdump.rs | 4 +++- src/cmds/cmd/status.rs | 4 ++-- src/systems/cmd/cmd_system.rs | 34 ++++++++++++++++++++++++++++------ src/systems/cmd/macros.rs | 10 ++++++---- src/systems/render/render_system.rs | 5 ++--- 6 files changed, 44 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/cmds/cmd/hexdump.rs b/src/cmds/cmd/hexdump.rs index a0f4a21..82f5d45 100644 --- a/src/cmds/cmd/hexdump.rs +++ b/src/cmds/cmd/hexdump.rs @@ -1,3 +1,5 @@ +use std::any::TypeId; + use crate::{ cmd_output, cmds::{ @@ -36,7 +38,7 @@ async fn collect(args: &Arg, _ctx: &JVCommandContext) -> Result Result<(Box, String), CmdExecuteError> { +) -> Result<(Box, TypeId), CmdExecuteError> { let output = JVHexOutput { data: collect.data }; cmd_output!(output, JVHexOutput) } diff --git a/src/cmds/cmd/sheetdump.rs b/src/cmds/cmd/sheetdump.rs index 3b0953a..defb8ff 100644 --- a/src/cmds/cmd/sheetdump.rs +++ b/src/cmds/cmd/sheetdump.rs @@ -1,3 +1,5 @@ +use std::any::TypeId; + use crate::{ cmd_output, cmds::{ @@ -46,7 +48,7 @@ async fn collect(args: &Arg, _ctx: &JVCommandContext) -> Result Result<(Box, String), CmdExecuteError> { +) -> Result<(Box, TypeId), CmdExecuteError> { let mappings = collect.sheet.mappings(); let mut mappings_vec = mappings.iter().cloned().collect::>(); if input.sort { diff --git a/src/cmds/cmd/status.rs b/src/cmds/cmd/status.rs index c1f2a96..5d5f4a3 100644 --- a/src/cmds/cmd/status.rs +++ b/src/cmds/cmd/status.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, time::SystemTime}; +use std::{any::TypeId, collections::HashMap, time::SystemTime}; use crate::{ cmd_output, @@ -84,7 +84,7 @@ async fn collect(_args: &Arg, _ctx: &JVCommandContext) -> Result Result<(Box, String), CmdExecuteError> { +) -> Result<(Box, TypeId), CmdExecuteError> { let mut wrong_modified_items: HashMap = HashMap::new(); 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 = if type_id + == std::any::TypeId::of::() + { + let concrete_data = data + .downcast::() + .map_err(|_| CmdProcessError::DowncastFailed)?; + include!("../render/_override_renderer_dispatcher.rs") + } else if type_id + == std::any::TypeId::of::() + { + let concrete_data = data + .downcast::() + .map_err(|_| CmdProcessError::DowncastFailed)?; + include!("../render/_override_renderer_dispatcher.rs") + } else if type_id == std::any::TypeId::of::() + { + let concrete_data = data + .downcast::() + .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, ctx: JVCommandContext, - ) -> impl Future, String), CmdProcessError>> + Send + ) -> impl Future, 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, String), CmdExecuteError>> + Send; + ) -> impl Future, TypeId), CmdExecuteError>> + Send; /// Get output type mapping fn get_output_type_mapping() -> HashMap; 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, String), CmdExecuteError> { +/// ) -> Result<(Box, 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, String), CmdExecuteError> { +/// ) -> Result<(Box, TypeId), CmdExecuteError> { /// todo!(); /// cmd_output!(output, JVCustomOutput) /// } @@ -145,7 +147,7 @@ macro_rules! command_template { input: In, collect: Collect, ) -> Result< - (Box, String), + (Box, 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, - 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, - type_name: String, + type_id: TypeId, ) -> Result { - let type_name_str = type_name.as_str(); include!("_specific_renderer_matching.rs") } -- cgit