subx_cli/commands/detect_encoding_command.rs
1//! Advanced character encoding detection command implementation.
2//!
3//! This module provides sophisticated character encoding detection capabilities
4//! for subtitle files, helping users identify and resolve encoding issues that
5//! can cause display problems with non-ASCII characters. It uses multiple
6//! detection algorithms and heuristics to provide accurate encoding identification.
7//!
8//! # Detection Algorithms
9//!
10//! The encoding detection system employs multiple complementary approaches:
11//!
12//! ## Byte Order Mark (BOM) Detection
13//! - **UTF-8**: EF BB BF byte sequence
14//! - **UTF-16LE**: FF FE byte sequence
15//! - **UTF-16BE**: FE FF byte sequence
16//! - **UTF-32**: Various 4-byte BOM sequences
17//!
18//! ## Statistical Analysis
19//! - **Character Frequency**: Analyze byte patterns for specific encodings
20//! - **Bigram Analysis**: Examine two-byte character combinations
21//! - **Language Heuristics**: Apply language-specific character patterns
22//! - **Confidence Scoring**: Quantify detection reliability
23//!
24//! ## Format-Specific Detection
25//! - **ASCII Compatibility**: Check for pure ASCII content
26//! - **Extended ASCII**: Detect Windows-1252, ISO-8859-1 variants
27//! - **Multi-byte Encodings**: Identify UTF-8, GB2312, Shift_JIS patterns
28//! - **Legacy Encodings**: Support for regional and historical encodings
29//!
30//! # Supported Encodings
31//!
32//! ## Unicode Family
33//! - **UTF-8**: Universal encoding, recommended for all new files
34//! - **UTF-16LE/BE**: Unicode with byte order variants
35//! - **UTF-32**: Full Unicode support with fixed width
36//!
37//! ## Western European
38//! - **ISO-8859-1 (Latin-1)**: Basic Western European characters
39//! - **Windows-1252**: Microsoft's Western European encoding
40//! - **ISO-8859-15**: Latin-1 with Euro symbol support
41//!
42//! ## East Asian
43//! - **GB2312/GBK**: Simplified Chinese encodings
44//! - **Big5**: Traditional Chinese encoding
45//! - **Shift_JIS**: Japanese encoding
46//! - **EUC-JP**: Alternative Japanese encoding
47//! - **EUC-KR**: Korean encoding
48//!
49//! ## Cyrillic and Others
50//! - **Windows-1251**: Russian and Cyrillic languages
51//! - **KOI8-R**: Russian encoding
52//! - **ISO-8859-5**: Cyrillic alphabet
53//!
54//! # Detection Features
55//!
56//! - **Confidence Scoring**: Reliability percentage for each detection
57//! - **Alternative Suggestions**: Multiple encoding candidates with scores
58//! - **Content Sampling**: Display decoded text samples for verification
59//! - **Language Hints**: Detect probable language from character patterns
60//! - **Format Validation**: Verify encoding produces valid subtitle content
61//!
62//! # Examples
63//!
64//! ```rust,ignore
65//! use subx_cli::commands::detect_encoding_command;
66//!
67//! // Detect encoding for multiple files
68//! let files = vec![
69//! "subtitle1.srt".to_string(),
70//! "subtitle2.ass".to_string(),
71//! ];
72//! detect_encoding_command::detect_encoding_command(&files, true)?;
73//!
74//! // Basic detection without verbose output
75//! detect_encoding_command::detect_encoding_command(&["file.srt".to_string()], false)?;
76//! ```
77
78use crate::Result;
79use crate::config::ConfigService;
80use crate::core::formats::encoding::EncodingDetector;
81use log::error;
82
83/// Execute character encoding detection for subtitle files with comprehensive analysis.
84///
85/// This function performs advanced character encoding detection on subtitle files,
86/// providing detailed information about detected encodings, confidence levels,
87/// and content samples. It supports both basic detection and verbose analysis
88/// modes to meet different user needs.
89///
90/// # Detection Process
91///
92/// 1. **File Validation**: Verify file existence and accessibility
93/// 2. **Initial Scanning**: Read file header and sample content
94/// 3. **BOM Detection**: Check for Unicode Byte Order Marks
95/// 4. **Statistical Analysis**: Analyze byte patterns and character frequencies
96/// 5. **Language Heuristics**: Apply language-specific detection rules
97/// 6. **Confidence Calculation**: Score each potential encoding
98/// 7. **Result Ranking**: Order candidates by confidence level
99/// 8. **Output Generation**: Format results for user presentation
100///
101/// # Verbose Mode Features
102///
103/// When `verbose` is enabled, the output includes:
104/// - **Confidence Percentages**: Numerical reliability scores
105/// - **Content Samples**: Decoded text previews
106/// - **Alternative Encodings**: Other possible encodings with scores
107/// - **Detection Metadata**: Technical details about the detection process
108/// - **Language Hints**: Probable content language indicators
109///
110/// # Error Handling
111///
112/// The function provides robust error handling:
113/// - **File Access**: Clear messages for permission or existence issues
114/// - **Corruption Detection**: Identification of damaged or invalid files
115/// - **Encoding Failures**: Graceful handling of undetectable encodings
116/// - **Partial Processing**: Continue with other files if individual files fail
117///
118/// # Output Formats
119///
120/// ## Basic Mode
121/// ```text
122/// file1.srt: UTF-8
123/// file2.ass: Windows-1252
124/// file3.vtt: GB2312
125/// ```
126///
127/// ## Verbose Mode
128/// ```text
129/// file1.srt: UTF-8 (99.5% confidence)
130/// Sample: "1\n00:00:01,000 --> 00:00:03,000\nHello World"
131/// Alternatives: ISO-8859-1 (15.2%), Windows-1252 (12.8%)
132/// Language: English (detected)
133///
134/// file2.ass: Windows-1252 (87.3% confidence)
135/// Sample: "[Script Info]\nTitle: Movie Subtitle"
136/// Alternatives: ISO-8859-1 (45.1%), UTF-8 (23.7%)
137/// Language: Mixed/Unknown
138/// ```
139///
140/// # Performance Considerations
141///
142/// - **Streaming Analysis**: Large files processed efficiently
143/// - **Sample-based Detection**: Uses representative file portions
144/// - **Caching**: Results cached for repeated operations
145/// - **Parallel Processing**: Multiple files analyzed concurrently
146///
147/// # Arguments
148///
149/// * `file_paths` - Vector of file paths to analyze for encoding
150/// * `verbose` - Enable detailed output with confidence scores and samples
151///
152/// # Returns
153///
154/// Returns `Ok(())` on successful analysis completion, or an error if:
155/// - Critical system resources are unavailable
156/// - All specified files are inaccessible
157/// - The encoding detection system fails to initialize
158///
159/// # Examples
160///
161/// ```rust,ignore
162/// use subx_cli::commands::detect_encoding_command;
163///
164/// // Quick encoding check for single file
165/// detect_encoding_command::detect_encoding_command(
166/// &["subtitle.srt".to_string()],
167/// false
168/// )?;
169///
170/// // Detailed analysis for multiple files
171/// let files = vec![
172/// "episode1.srt".to_string(),
173/// "episode2.ass".to_string(),
174/// "episode3.vtt".to_string(),
175/// ];
176/// detect_encoding_command::detect_encoding_command(&files, true)?;
177///
178/// // Batch analysis with glob patterns (shell expansion)
179/// let glob_files = vec![
180/// "season1/*.srt".to_string(),
181/// "season2/*.ass".to_string(),
182/// ];
183/// detect_encoding_command::detect_encoding_command(&glob_files, false)?;
184/// ```
185///
186/// # Use Cases
187///
188/// - **Troubleshooting**: Identify encoding issues causing display problems
189/// - **Conversion Planning**: Determine current encoding before conversion
190/// - **Quality Assurance**: Verify encoding consistency across file collections
191/// - **Migration**: Assess encoding diversity when migrating subtitle libraries
192/// - **Automation**: Integrate encoding detection into batch processing workflows
193use crate::cli::DetectEncodingArgs;
194use crate::error::SubXError;
195
196/// Execute character encoding detection for subtitle files based on input arguments.
197pub fn detect_encoding_command(args: &DetectEncodingArgs) -> Result<()> {
198 // Initialize the encoding detection engine
199 let detector = EncodingDetector::with_defaults();
200
201 // Collect target files using InputPathHandler logic
202 let paths = args
203 .get_file_paths()
204 .map_err(|e| SubXError::CommandExecution(e.to_string()))?;
205
206 // Process each file individually to provide isolated error handling
207 for path in paths {
208 if !path.exists() {
209 error!("Path does not exist: {}", path.display());
210 continue;
211 }
212 let file_str = path.to_string_lossy();
213 match detector.detect_file_encoding(&file_str) {
214 Ok(info) => {
215 let name = path
216 .file_name()
217 .and_then(|n| n.to_str())
218 .unwrap_or(&file_str);
219 println!("File: {name}");
220 println!(
221 " Encoding: {:?} (Confidence: {:.1}%) BOM: {}",
222 info.charset,
223 info.confidence * 100.0,
224 if info.bom_detected { "Yes" } else { "No" }
225 );
226 let sample = if args.verbose {
227 info.sample_text.clone()
228 } else if info.sample_text.len() > 50 {
229 format!("{}...", &info.sample_text[..47])
230 } else {
231 info.sample_text.clone()
232 };
233 println!(" Sample text: {sample}\n");
234 }
235 Err(e) => error!("Unable to detect encoding for {}: {}", path.display(), e),
236 }
237 }
238 Ok(())
239}
240
241/// Execute encoding detection command with injected configuration service.
242///
243/// This function provides the new dependency injection interface for the detect_encoding command,
244/// accepting a configuration service instead of loading configuration globally.
245///
246/// # Arguments
247///
248/// * `file_paths` - File paths to analyze for encoding detection
249/// * `verbose` - Whether to show verbose output
250/// * `config_service` - Configuration service providing access to settings
251///
252/// # Returns
253///
254/// Returns `Ok(())` on successful completion, or an error if detection fails.
255pub fn detect_encoding_command_with_config(
256 args: DetectEncodingArgs,
257 _config_service: &dyn ConfigService,
258) -> Result<()> {
259 // Delegate to new implementation based on input argument struct
260 detect_encoding_command(&args)
261}