summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-02-26 00:51:45 +0800
committer魏曹先生 <1992414357@qq.com>2026-02-26 01:02:42 +0800
commite3aeb6a1c9955eee2c396230317e63becd24ec6a (patch)
tree5c70b2a73fb32ed1a717463c81a99e346d3c25e6 /src
parentae06e0b30a328508f48c108fabcccdf1d23c5643 (diff)
Replace string type identifiers with TypeId for command output matching
Diffstat (limited to 'src')
-rw-r--r--src/cmds/cmd/hexdump.rs4
-rw-r--r--src/cmds/cmd/sheetdump.rs4
-rw-r--r--src/cmds/cmd/status.rs4
-rw-r--r--src/systems/cmd/cmd_system.rs34
-rw-r--r--src/systems/cmd/macros.rs10
-rw-r--r--src/systems/render/render_system.rs5
6 files changed, 44 insertions, 17 deletions
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<Collect, CmdPrep
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> {
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<Collect, CmdPrep
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> {
let mappings = collect.sheet.mappings();
let mut mappings_vec = mappings.iter().cloned().collect::<Vec<LocalMapping>>();
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<Collect, CmdPre
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> {
let mut wrong_modified_items: HashMap<ModifiedRelativePathBuf, JVStatusWrongModifyReason> =
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<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")
}