From 9d491352d161ee629cc47459537344ba0ea4bb35 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sat, 20 Jun 2026 01:11:25 +0800 Subject: Add shared `MockProgramCollect` and conditional `Groupped` bounds Extract duplicate `MockCollect` implementations into a reusable `MockProgramCollect` type. Conditionally require `Serialize` on the `Groupped` trait when the `general_renderer` feature is enabled. --- mingling_core/src/program/collection.rs | 3 + mingling_core/src/program/collection/mock.rs | 105 +++++++++++++++++++++++++++ mingling_core/src/program/setup.rs | 63 +--------------- 3 files changed, 112 insertions(+), 59 deletions(-) create mode 100644 mingling_core/src/program/collection/mock.rs (limited to 'mingling_core/src/program') diff --git a/mingling_core/src/program/collection.rs b/mingling_core/src/program/collection.rs index d3d18d6..078f736 100644 --- a/mingling_core/src/program/collection.rs +++ b/mingling_core/src/program/collection.rs @@ -12,6 +12,9 @@ use crate::{GeneralRendererSetting, error::GeneralRendererSerializeError}; #[cfg(feature = "comp")] use crate::{ShellContext, Suggest}; +mod mock; +pub use mock::*; + /// Collected program context /// /// Note: It is recommended to use the `gen_program!()` macro from [mingling_macros](https://crates.io/crates/mingling_macros) to automatically create this type diff --git a/mingling_core/src/program/collection/mock.rs b/mingling_core/src/program/collection/mock.rs new file mode 100644 index 0000000..e1b3aa4 --- /dev/null +++ b/mingling_core/src/program/collection/mock.rs @@ -0,0 +1,105 @@ +pub use mock::*; + +mod mock { + #[cfg(feature = "async")] + use std::pin::Pin; + + #[cfg(feature = "dispatch_tree")] + use crate::Dispatcher; + + use crate::{AnyOutput, ChainProcess, Groupped, ProgramCollect, RenderResult}; + + #[cfg(feature = "general_renderer")] + use crate::{GeneralRendererSetting, error::GeneralRendererSerializeError}; + + #[cfg(feature = "comp")] + use crate::{ShellContext, Suggest}; + + #[cfg(feature = "general_renderer")] + use serde::Serialize; + + #[cfg_attr(feature = "general_renderer", derive(Serialize))] + #[allow(unused)] + pub enum MockProgramCollect { + Foo, + Bar, + } + + impl Groupped for MockProgramCollect { + fn member_id() -> MockProgramCollect { + MockProgramCollect::Foo + } + } + + impl ProgramCollect for MockProgramCollect { + type Enum = MockProgramCollect; + type ErrorDispatcherNotFound = MockProgramCollect; + type ErrorRendererNotFound = MockProgramCollect; + type ResultEmpty = MockProgramCollect; + + #[cfg(feature = "dispatch_tree")] + fn dispatch_args_trie( + _raw: &[String], + ) -> Result, crate::error::ProgramInternalExecuteError> { + unreachable!() + } + + #[cfg(feature = "dispatch_tree")] + fn get_nodes() -> Vec<(String, &'static (dyn Dispatcher + Send + Sync))> { + unreachable!() + } + + fn build_renderer_not_found(_member_id: Self::Enum) -> AnyOutput { + unreachable!() + } + + fn build_dispatcher_not_found(_args: Vec) -> AnyOutput { + unreachable!() + } + + fn build_empty_result() -> AnyOutput { + unreachable!() + } + + fn render(_any: AnyOutput, _r: &mut RenderResult) { + unreachable!() + } + + fn render_help(_any: AnyOutput, _r: &mut RenderResult) { + unreachable!() + } + + #[cfg(feature = "async")] + fn do_chain( + _any: AnyOutput, + ) -> Pin> + Send>> { + unreachable!() + } + + #[cfg(not(feature = "async"))] + fn do_chain(_any: AnyOutput) -> ChainProcess { + unreachable!() + } + + #[cfg(feature = "comp")] + fn do_comp(_any: &AnyOutput, _ctx: &ShellContext) -> Suggest { + unreachable!() + } + + fn has_renderer(_any: &AnyOutput) -> bool { + unreachable!() + } + + fn has_chain(_any: &AnyOutput) -> bool { + unreachable!() + } + + #[cfg(feature = "general_renderer")] + fn general_render( + _any: AnyOutput, + _setting: &GeneralRendererSetting, + ) -> Result { + unreachable!() + } + } +} diff --git a/mingling_core/src/program/setup.rs b/mingling_core/src/program/setup.rs index 2bfced1..f248fb6 100644 --- a/mingling_core/src/program/setup.rs +++ b/mingling_core/src/program/setup.rs @@ -20,69 +20,14 @@ where #[cfg(test)] mod tests { use super::*; - use crate::{AnyOutput, ChainProcess, Groupped, RenderResult}; - - /// Minimal mock collector that satisfies `C: ProgramCollect` - /// by setting `Enum = Self`. - #[derive(Debug, Clone, PartialEq)] - struct MockCollect; - - impl Groupped for MockCollect { - fn member_id() -> MockCollect { - MockCollect - } - } - - impl ProgramCollect for MockCollect { - type Enum = MockCollect; - type ErrorDispatcherNotFound = MockCollect; - type ErrorRendererNotFound = MockCollect; - type ResultEmpty = MockCollect; - - fn build_renderer_not_found(_member_id: MockCollect) -> AnyOutput { - unimplemented!() - } - fn build_dispatcher_not_found(_args: Vec) -> AnyOutput { - unimplemented!() - } - fn build_empty_result() -> AnyOutput { - unimplemented!() - } - fn render(_any: AnyOutput, _r: &mut RenderResult) { - unimplemented!() - } - fn render_help(_any: AnyOutput, _r: &mut RenderResult) { - unimplemented!() - } - fn do_chain(_any: AnyOutput) -> ChainProcess { - unimplemented!() - } - #[cfg(feature = "comp")] - fn do_comp(_any: &AnyOutput, _ctx: &crate::ShellContext) -> crate::Suggest { - unimplemented!() - } - fn has_renderer(_any: &AnyOutput) -> bool { - unimplemented!() - } - fn has_chain(_any: &AnyOutput) -> bool { - unimplemented!() - } - - #[cfg(feature = "general_renderer")] - fn general_render( - _any: AnyOutput, - _setting: &crate::GeneralRendererSetting, - ) -> Result { - unimplemented!() - } - } + use crate::MockProgramCollect; struct TestSetup { called: std::rc::Rc>, } - impl ProgramSetup for TestSetup { - fn setup(self, _program: &mut Program) { + impl ProgramSetup for TestSetup { + fn setup(self, _program: &mut Program) { self.called.set(true); } } @@ -93,7 +38,7 @@ mod tests { let setup = TestSetup { called: std::rc::Rc::clone(&called), }; - let mut program: Program = Program::new_with_args(["test"]); + let mut program: Program = Program::new_with_args(["test"]); program.with_setup(setup); assert!(called.get()); } -- cgit