diff options
| -rw-r--r-- | Cargo.lock | 5 | ||||
| -rw-r--r-- | src/cmds/cmd/hexdump.rs | 4 | ||||
| -rw-r--r-- | src/cmds/cmd/sheetdump.rs | 4 | ||||
| -rw-r--r-- | src/cmds/cmd/status.rs | 4 | ||||
| -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 | ||||
| -rw-r--r-- | templates/_override_renderer_dispatcher.rs.template | 1 | ||||
| -rw-r--r-- | templates/_override_renderer_entry.rs.template | 10 | ||||
| -rw-r--r-- | templates/_specific_renderer_matching.rs.template | 16 |
10 files changed, 62 insertions, 31 deletions
@@ -1122,6 +1122,7 @@ dependencies = [ "jvlib", "sha1_hash", "sheet_system", + "storage_system", "tcp_connection", "toml 0.9.8", "vcs_actions", @@ -2064,6 +2065,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" [[package]] +name = "storage_system" +version = "0.1.0" + +[[package]] name = "strip-ansi-escapes" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" 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") } diff --git a/templates/_override_renderer_dispatcher.rs.template b/templates/_override_renderer_dispatcher.rs.template index 64d2f40..b22c957 100644 --- a/templates/_override_renderer_dispatcher.rs.template +++ b/templates/_override_renderer_dispatcher.rs.template @@ -1,3 +1,4 @@ +// Auto generated by build.rs match renderer_override { // MATCH // -- TEMPLATE START -- diff --git a/templates/_override_renderer_entry.rs.template b/templates/_override_renderer_entry.rs.template index 8b2130d..7912cb0 100644 --- a/templates/_override_renderer_entry.rs.template +++ b/templates/_override_renderer_entry.rs.template @@ -1,13 +1,13 @@ // Auto generated by build.rs -match type_name.as_str() { +match type_id { // MATCHING // -- TEMPLATE START -- - "JVOutputTypeName" => { + type_id if type_id == std::any::TypeId::of::<JVOutputType>() => { let concrete_data = data .downcast::<JVOutputType>() .map_err(|_| CmdProcessError::DowncastFailed)?; - include!("../render/_override_renderer_dispatcher.rs") - } + include!("../render/_override_renderer_dispatcher.rs").map_err(CmdProcessError::Render) + }, // -- TEMPLATE END -- - _ => return Err(CmdProcessError::NoMatchingCommand), + _ => Err(CmdProcessError::NoMatchingCommand) } diff --git a/templates/_specific_renderer_matching.rs.template b/templates/_specific_renderer_matching.rs.template index 9b3765f..4f1c7d2 100644 --- a/templates/_specific_renderer_matching.rs.template +++ b/templates/_specific_renderer_matching.rs.template @@ -1,14 +1,12 @@ -match type_name_str { +// Auto generated by build.rs +match type_id { // MATCHING // -- TEMPLATE START -- - "OutputTypeName" => { + type_id if type_id == std::any::TypeId::of::<OutputType>() => { RendererType::render( - &data - .downcast::<OutputType>() - .unwrap(), - ) - .await - } + &data.downcast::<OutputType>() + .unwrap()).await + }, // -- TEMPLATE END -- - _ => Err(CmdRenderError::RendererNotFound(type_name)), + _ => Err(CmdRenderError::RendererNotFound(format!("{:?}", type_id))) } |
