use crate::{ProgramCollect, program::Program}; pub trait ProgramSetup where C: ProgramCollect, { fn setup(self, program: &mut Program); } impl Program where C: ProgramCollect, { /// Load and execute init logic pub fn with_setup + 'static>(&mut self, setup: S) { S::setup(setup, self); } } #[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!() } } struct TestSetup { called: std::rc::Rc>, } impl ProgramSetup for TestSetup { fn setup(self, _program: &mut Program) { self.called.set(true); } } #[test] fn test_with_setup_calls_setup() { let called = std::rc::Rc::new(std::cell::Cell::new(false)); let setup = TestSetup { called: std::rc::Rc::clone(&called), }; let mut program: Program = Program::new_with_args(["test"]); program.with_setup(setup); assert!(called.get()); } }