use crate::{Result, cli::Commands, config::ConfigService};
use std::sync::Arc;
pub async fn dispatch_command(
command: Commands,
config_service: Arc<dyn ConfigService>,
) -> Result<()> {
match command {
Commands::Match(args) => {
crate::commands::match_command::execute_with_config(args, config_service).await
}
Commands::Convert(args) => {
crate::commands::convert_command::execute_with_config(args, config_service).await
}
Commands::Sync(args) => {
crate::commands::sync_command::execute_with_config(args, config_service).await
}
Commands::Config(args) => {
crate::commands::config_command::execute_with_config(args, config_service).await
}
Commands::GenerateCompletion(args) => {
let mut cmd = <crate::cli::Cli as clap::CommandFactory>::command();
let cmd_name = cmd.get_name().to_string();
let mut stdout = std::io::stdout();
clap_complete::generate(args.shell, &mut cmd, cmd_name, &mut stdout);
Ok(())
}
Commands::Cache(args) => {
crate::commands::cache_command::execute_with_config(args, config_service).await
}
Commands::DetectEncoding(args) => {
crate::commands::detect_encoding_command::detect_encoding_command_with_config(
args,
config_service.as_ref(),
)?;
Ok(())
}
}
}
pub async fn dispatch_command_with_ref(
command: Commands,
config_service: &dyn ConfigService,
) -> Result<()> {
match command {
Commands::Match(args) => {
args.validate()
.map_err(crate::error::SubXError::CommandExecution)?;
crate::commands::match_command::execute(args, config_service).await
}
Commands::Convert(args) => {
crate::commands::convert_command::execute(args, config_service).await
}
Commands::Sync(args) => crate::commands::sync_command::execute(args, config_service).await,
Commands::Config(args) => {
crate::commands::config_command::execute(args, config_service).await
}
Commands::GenerateCompletion(args) => {
let mut cmd = <crate::cli::Cli as clap::CommandFactory>::command();
let cmd_name = cmd.get_name().to_string();
let mut stdout = std::io::stdout();
clap_complete::generate(args.shell, &mut cmd, cmd_name, &mut stdout);
Ok(())
}
Commands::Cache(args) => crate::commands::cache_command::execute(args).await,
Commands::DetectEncoding(args) => {
crate::commands::detect_encoding_command::detect_encoding_command_with_config(
args,
config_service,
)?;
Ok(())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cli::{ConvertArgs, MatchArgs, OutputSubtitleFormat};
use crate::config::TestConfigService;
#[tokio::test]
async fn test_dispatch_match_command() {
let config_service = Arc::new(TestConfigService::with_ai_settings(
"test_provider",
"test_model",
));
let args = MatchArgs {
path: Some("/tmp/test".into()),
input_paths: vec![],
dry_run: true,
confidence: 80,
recursive: false,
backup: false,
copy: false,
move_files: false,
no_extract: false,
};
let result = dispatch_command(Commands::Match(args), config_service).await;
match result {
Ok(_) => {} Err(e) => {
let error_msg = format!("{:?}", e);
assert!(
error_msg.contains("NotFound")
|| error_msg.contains("No subtitle files found")
|| error_msg.contains("No video files found")
|| error_msg.contains("Config"),
"Unexpected error: {:?}",
e
);
}
}
}
#[tokio::test]
async fn test_dispatch_convert_command() {
let config_service = Arc::new(TestConfigService::with_defaults());
let args = ConvertArgs {
input: Some("/tmp/nonexistent".into()),
input_paths: vec![],
recursive: false,
format: Some(OutputSubtitleFormat::Srt),
output: None,
keep_original: false,
encoding: "utf-8".to_string(),
no_extract: false,
};
let _result = dispatch_command(Commands::Convert(args), config_service).await;
}
#[tokio::test]
async fn test_dispatch_with_ref() {
let config_service = TestConfigService::with_ai_settings("test_provider", "test_model");
let args = MatchArgs {
path: Some("/tmp/test".into()),
input_paths: vec![],
dry_run: true,
confidence: 80,
recursive: false,
backup: false,
copy: false,
move_files: false,
no_extract: false,
};
let result = dispatch_command_with_ref(Commands::Match(args), &config_service).await;
match result {
Ok(_) => {} Err(e) => {
let error_msg = format!("{:?}", e);
assert!(
error_msg.contains("NotFound")
|| error_msg.contains("No subtitle files found")
|| error_msg.contains("No video files found")
|| error_msg.contains("Config"),
"Unexpected error: {:?}",
e
);
}
}
}
}