use clap::ArgMatches;
use log::{info, error};
use crate::commands::command_traits::Command;
use crate::tiff::errors::{TiffResult, TiffError};
use crate::utils::logger::Logger;
use crate::compression::{CompressionFactory, CompressionConverter};
pub struct ConvertCommand<'a> {
input_file: String,
output_file: String,
target_compression: u64,
logger: &'a Logger,
}
impl<'a> ConvertCommand<'a> {
pub fn new(args: &ArgMatches, logger: &'a Logger) -> TiffResult<Self> {
let input_file = args.get_one::<String>("input")
.ok_or_else(|| TiffError::GenericError("Missing input file".to_string()))?
.clone();
let output_file = args.get_one::<String>("output")
.ok_or_else(|| TiffError::GenericError("Missing output file path for conversion".to_string()))?
.clone();
let target_compression = if let Some(compression_str) = args.get_one::<String>("compression") {
compression_str.parse::<u64>()
.map_err(|_| TiffError::GenericError(format!("Invalid compression code: {}", compression_str)))?
} else if let Some(compression_name) = args.get_one::<String>("compression-name") {
match CompressionFactory::get_handler_by_name(compression_name) {
Ok(handler) => handler.code(),
Err(_) => return Err(TiffError::GenericError(format!("Unknown compression name: {}", compression_name)))
}
} else {
return Err(TiffError::GenericError("Missing compression specification. Use --compression or --compression-name".to_string()));
};
match CompressionFactory::create_handler(target_compression) {
Ok(handler) => info!("Using compression: {}", handler.name()),
Err(_) => return Err(TiffError::GenericError(format!("Unsupported compression code: {}", target_compression)))
}
Ok(ConvertCommand {
input_file,
output_file,
target_compression,
logger,
})
}
}
impl<'a> Command for ConvertCommand<'a> {
fn execute(&self) -> TiffResult<()> {
info!("Converting file {} to {} with compression code {}",
self.input_file, self.output_file, self.target_compression);
let mut converter = CompressionConverter::new(self.logger);
converter.convert_file(&self.input_file, &self.output_file, self.target_compression)?;
info!("Compression conversion successful");
self.logger.log("Compression conversion successful")?;
Ok(())
}
}