subx-cli 1.7.4

AI subtitle processing CLI tool, which automatically matches, renames, and converts subtitle files.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! Subtitle translation command implementation.
//!
//! This module wires CLI argument validation, configuration loading, input
//! collection, safe output resolution, and translation engine invocation.
//!
//! # Examples
//!
//! ```rust,ignore
//! use subx_cli::cli::TranslateArgs;
//! use subx_cli::commands::translate_command;
//! use subx_cli::config::TestConfigService;
//!
//! # async fn example() -> subx_cli::Result<()> {
//! let args = TranslateArgs {
//!     paths: vec!["movie.srt".into()],
//!     input_paths: vec![],
//!     recursive: false,
//!     target_language: Some("zh-TW".to_string()),
//!     source_language: None,
//!     glossary: None,
//!     context: None,
//!     output: None,
//!     no_extract: false,
//!     force: false,
//!     replace: false,
//! };
//! let config_service = TestConfigService::with_defaults();
//! translate_command::execute(args, &config_service).await?;
//! # Ok(())
//! # }
//! ```

use crate::cli::TranslateArgs;
use std::path::{Path, PathBuf};

use crate::cli::output::{active_mode, emit_success, is_quiet};
use crate::config::ConfigService;
use crate::core::ComponentFactory;
use crate::core::translation::{TranslationRequest, parse_glossary_text};
use crate::error::SubXError;
use serde::Serialize;

/// Per-file record reported in the `translate` JSON envelope.
///
/// Each entry corresponds to one input subtitle file processed by the
/// command. `applied` is `true` when the translated output was
/// successfully written to disk.
#[derive(Debug, Serialize)]
pub struct TranslatedFile {
    /// Source subtitle path as supplied to the command.
    pub input: String,
    /// Effective output path for the translated subtitle (may equal
    /// `input` when `--replace` is active).
    pub output: String,
    /// Whether the translated content was written successfully.
    pub applied: bool,
}

/// Top-level payload for the `translate` command JSON envelope.
#[derive(Debug, Serialize)]
pub struct TranslatePayload {
    /// Per-file outcomes for every collected input.
    pub translated_files: Vec<TranslatedFile>,
}

/// Resolved translate command inputs after CLI/config defaulting.
///
/// This struct is filled in by [`execute`] before delegating to the
/// translation engine implemented in the `core` slice. It is exposed so the
/// engine slice can consume a stable, validated value type.
#[derive(Debug, Clone)]
pub struct TranslateExecution {
    /// Validated CLI arguments.
    pub args: TranslateArgs,
    /// Effective target language (`args.target_language` if non-empty,
    /// otherwise [`crate::config::TranslationConfig::default_target_language`]).
    pub target_language: String,
    /// Configured translation batch size.
    pub batch_size: usize,
}

struct ResolvedOutput {
    path: PathBuf,
    replaces_source: bool,
}

/// Resolve the effective target language using CLI > config precedence.
///
/// CLI `--target-language` always wins when non-empty. Otherwise, the
/// configured `translation.default_target_language` is used. If neither is
/// available, the function returns an error so the caller can surface a
/// usage-style failure before any AI request is sent.
fn resolve_target_language(
    args: &TranslateArgs,
    default: Option<&str>,
) -> Result<String, SubXError> {
    if let Some(cli) = args.target_language.as_deref() {
        let trimmed = cli.trim();
        if !trimmed.is_empty() {
            return Ok(trimmed.to_string());
        }
    }
    match default {
        Some(d) if !d.trim().is_empty() => Ok(d.trim().to_string()),
        _ => Err(SubXError::CommandExecution(
            "No target language provided. Pass --target-language or set \
             translation.default_target_language in the configuration."
                .to_string(),
        )),
    }
}

/// Execute the `translate` command.
///
/// # Arguments
///
/// * `args` - Parsed CLI arguments.
/// * `config_service` - Configuration service providing translation defaults.
///
/// # Errors
///
/// Returns an error if argument validation, input collection, AI translation,
/// or output writing fails.
pub async fn execute(args: TranslateArgs, config_service: &dyn ConfigService) -> crate::Result<()> {
    args.validate()
        .map_err(|e| SubXError::CommandExecution(e.to_string()))?;

    let config = config_service.get_config()?;
    let target_language =
        resolve_target_language(&args, config.translation.default_target_language.as_deref())?;

    let execution = TranslateExecution {
        args: args.clone(),
        target_language: target_language.clone(),
        batch_size: config.translation.batch_size,
    };

    let handler = execution
        .args
        .get_input_handler()
        .map_err(|e| SubXError::CommandExecution(e.to_string()))?;
    let collected = handler
        .collect_files()
        .map_err(|e| SubXError::CommandExecution(e.to_string()))?;
    let mode = active_mode();
    let json_mode = mode.is_json();
    let quiet = is_quiet();
    if collected.is_empty() {
        // Nothing to do — emit an empty success envelope in JSON mode so
        // callers always receive a valid document.
        if json_mode {
            emit_success(
                mode,
                "translate",
                TranslatePayload {
                    translated_files: Vec::new(),
                },
            );
        }
        return Ok(());
    }

    let glossary_text = match &execution.args.glossary {
        Some(path) => Some(std::fs::read_to_string(path).map_err(|e| {
            SubXError::FileOperationFailed(format!(
                "Failed to read glossary file {}: {e}",
                path.display()
            ))
        })?),
        None => None,
    };
    let glossary_entries = glossary_text
        .as_deref()
        .map(parse_glossary_text)
        .unwrap_or_default();

    let factory = ComponentFactory::new(config_service)?;
    let engine = factory.create_translation_engine()?;
    let mut failures = Vec::new();
    let mut items: Vec<TranslatedFile> = Vec::with_capacity(collected.len());

    for input_path in collected.iter() {
        let output = match resolve_output_path(
            input_path,
            &collected,
            &execution.args,
            &execution.target_language,
        ) {
            Ok(output) => output,
            Err(err) => {
                if !json_mode && !quiet {
                    eprintln!(
                        "✗ Translation setup failed for {}: {}",
                        input_path.display(),
                        err
                    );
                }
                failures.push(format!("{}: {err}", input_path.display()));
                items.push(TranslatedFile {
                    input: input_path.display().to_string(),
                    output: String::new(),
                    applied: false,
                });
                continue;
            }
        };

        if let Err(err) = translate_one_file(
            &engine,
            input_path,
            &output,
            &execution,
            glossary_text.clone(),
            glossary_entries.clone(),
            config.general.backup_enabled,
        )
        .await
        {
            if !json_mode && !quiet {
                eprintln!("✗ Translation failed for {}: {}", input_path.display(), err);
            }
            failures.push(format!("{}: {err}", input_path.display()));
            items.push(TranslatedFile {
                input: input_path.display().to_string(),
                output: output.path.display().to_string(),
                applied: false,
            });
        } else {
            if !json_mode {
                println!(
                    "✓ Translation completed: {} -> {}",
                    input_path.display(),
                    output.path.display()
                );
            }
            items.push(TranslatedFile {
                input: input_path.display().to_string(),
                output: output.path.display().to_string(),
                applied: true,
            });
        }
    }

    if failures.is_empty() {
        if json_mode {
            emit_success(
                mode,
                "translate",
                TranslatePayload {
                    translated_files: items,
                },
            );
        }
        Ok(())
    } else {
        Err(SubXError::CommandExecution(format!(
            "{} translation job(s) failed: {}",
            failures.len(),
            failures.join("; ")
        )))
    }
}

/// Execute the `translate` command with an owned configuration service.
///
/// Mirrors the convention used by the other subcommands so the dispatcher
/// can route both `Arc<dyn ConfigService>` and `&dyn ConfigService` paths.
pub async fn execute_with_config(
    args: TranslateArgs,
    config_service: std::sync::Arc<dyn ConfigService>,
) -> crate::Result<()> {
    execute(args, config_service.as_ref()).await
}

async fn translate_one_file(
    engine: &crate::core::translation::TranslationEngine,
    input_path: &Path,
    output: &ResolvedOutput,
    execution: &TranslateExecution,
    glossary_text: Option<String>,
    glossary_entries: Vec<crate::core::translation::GlossaryEntry>,
    backup_enabled: bool,
) -> crate::Result<()> {
    if output.path.exists() && !execution.args.force && !output.replaces_source {
        return Err(SubXError::FileAlreadyExists(
            output.path.display().to_string(),
        ));
    }

    let subtitle = engine.format_manager().load_subtitle(input_path)?;
    let request = TranslationRequest {
        target_language: execution.target_language.clone(),
        source_language: execution.args.source_language.clone(),
        glossary_text,
        context: execution.args.context.clone(),
        glossary_entries,
    };
    let result = engine.translate_subtitle(subtitle, &request).await?;

    if output.replaces_source && backup_enabled {
        let backup = backup_path(input_path);
        std::fs::copy(input_path, &backup).map_err(|e| {
            SubXError::FileOperationFailed(format!(
                "Failed to create backup {}: {e}",
                backup.display()
            ))
        })?;
    }

    if let Some(parent) = output.path.parent() {
        std::fs::create_dir_all(parent).map_err(|e| {
            SubXError::FileOperationFailed(format!(
                "Failed to create output directory {}: {e}",
                parent.display()
            ))
        })?;
    }
    engine
        .format_manager()
        .save_subtitle(&result.subtitle, &output.path)
}

fn resolve_output_path(
    input_path: &Path,
    collected: &crate::cli::CollectedFiles,
    args: &TranslateArgs,
    target_language: &str,
) -> crate::Result<ResolvedOutput> {
    if args.replace {
        if collected.archive_origin(input_path).is_some() {
            return Err(SubXError::CommandExecution(
                "--replace cannot be used for subtitles extracted from archives".to_string(),
            ));
        }
        return Ok(ResolvedOutput {
            path: input_path.to_path_buf(),
            replaces_source: true,
        });
    }

    let path = match &args.output {
        Some(output) => explicit_output_path(output, input_path, collected.len(), target_language)?,
        None => default_output_path(
            input_path,
            collected.archive_origin(input_path),
            target_language,
        ),
    };
    Ok(ResolvedOutput {
        path,
        replaces_source: false,
    })
}

fn explicit_output_path(
    output: &Path,
    input_path: &Path,
    input_count: usize,
    target_language: &str,
) -> crate::Result<PathBuf> {
    if input_count > 1 {
        if output.exists() && !output.is_dir() {
            return Err(SubXError::CommandExecution(format!(
                "Batch translation output must be a directory: {}",
                output.display()
            )));
        }
        if output.extension().is_some() {
            return Err(SubXError::CommandExecution(format!(
                "Batch translation output must be a directory: {}",
                output.display()
            )));
        }
        return Ok(output.join(translated_file_name(input_path, target_language)));
    }

    if output.is_dir() {
        Ok(output.join(translated_file_name(input_path, target_language)))
    } else {
        Ok(output.to_path_buf())
    }
}

fn default_output_path(
    input_path: &Path,
    archive_origin: Option<&Path>,
    target_language: &str,
) -> PathBuf {
    let base_dir = archive_origin
        .and_then(Path::parent)
        .or_else(|| input_path.parent())
        .unwrap_or_else(|| Path::new("."));
    base_dir.join(translated_file_name(input_path, target_language))
}

fn translated_file_name(input_path: &Path, target_language: &str) -> String {
    let stem = input_path
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("subtitle");
    let ext = input_path
        .extension()
        .and_then(|s| s.to_str())
        .unwrap_or("srt");
    format!("{stem}.{target_language}.{ext}")
}

fn backup_path(input_path: &Path) -> PathBuf {
    let ext = input_path
        .extension()
        .and_then(|s| s.to_str())
        .unwrap_or("");
    if ext.is_empty() {
        input_path.with_extension("backup")
    } else {
        input_path.with_extension(format!("{ext}.backup"))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::TestConfigBuilder;
    use std::path::PathBuf;

    fn base_args() -> TranslateArgs {
        TranslateArgs {
            paths: vec![PathBuf::from("nonexistent.srt")],
            input_paths: vec![],
            recursive: false,
            target_language: Some("zh-TW".to_string()),
            source_language: None,
            glossary: None,
            context: None,
            output: None,
            no_extract: false,
            force: false,
            replace: false,
        }
    }

    #[tokio::test]
    async fn test_validation_runs_before_execution() {
        let mut args = base_args();
        args.target_language = Some("   ".to_string());
        let config_service = TestConfigBuilder::new().build_service();
        let err = execute(args, &config_service)
            .await
            .expect_err("empty target language must fail");
        let msg = format!("{err:?}");
        assert!(msg.contains("target-language"), "unexpected error: {msg}");
    }

    #[tokio::test]
    async fn test_uses_configured_default_target_language() {
        let mut args = base_args();
        args.target_language = None;
        let config_service = TestConfigBuilder::new()
            .with_translation_default_target_language("ja")
            .build_service();
        // The configured default should satisfy target-language resolution.
        // This fixture uses a nonexistent input, so the command fails later
        // during input collection instead of failing target-language resolution.
        let err = execute(args, &config_service)
            .await
            .expect_err("input collection error");
        let msg = format!("{err:?}");
        assert!(msg.contains("Path not found"), "unexpected: {msg}");
    }

    #[tokio::test]
    async fn test_missing_default_and_cli_target_language_fails() {
        let mut args = base_args();
        args.target_language = None;
        let config_service = TestConfigBuilder::new().build_service();
        let err = execute(args, &config_service)
            .await
            .expect_err("no target language must fail");
        let msg = format!("{err:?}");
        assert!(msg.contains("No target language"), "unexpected: {msg}");
    }
}