pub async fn dispatch_command(
command: Commands,
config_service: Arc<dyn ConfigService>,
) -> Result<()>Expand description
Central command dispatcher to avoid code duplication.
This module provides a unified way to dispatch commands, eliminating duplication between CLI and library API paths.
§Design Principles
- Single Responsibility: Each command dispatcher handles exactly one command type
- Consistency: Both CLI and App API use the same command execution logic
- Error Handling: Unified error handling across all command paths
- Testability: Easy to test individual command dispatch without full CLI setup
§Architecture
The dispatcher acts as a bridge between:
- CLI argument parsing (from
clap) - Command execution logic (in
commandsmodule) - Configuration dependency injection
This eliminates the previous duplication where both cli::run_with_config()
and App::handle_command() had identical match statements.
§Examples
use subx_cli::commands::dispatcher::dispatch_command;
use subx_cli::cli::{Commands, MatchArgs};
use subx_cli::config::TestConfigService;
use std::sync::Arc;
let config_service = Arc::new(TestConfigService::with_defaults());
let match_args = MatchArgs {
path: Some("/path/to/files".into()),
input_paths: vec![],
dry_run: true,
confidence: 80,
recursive: false,
backup: false,
copy: false,
move_files: false,
};
dispatch_command(Commands::Match(match_args), config_service).await?;