|
1 | | -/// Temporary scaffold for Task 1; normalize behavior will be implemented in later tasks. |
2 | | -pub fn run_normalize_command() -> Result<(), String> { |
| 1 | +use crate::path_glob; |
| 2 | +use crate::validation::{validate_file_path, validate_output_path}; |
| 3 | +use langcodec::{ |
| 4 | + Codec, FormatType, KeyStyle, NormalizeOptions as EngineNormalizeOptions, normalize_codec, |
| 5 | +}; |
| 6 | + |
| 7 | +#[derive(Debug, Clone)] |
| 8 | +pub struct NormalizeCliOptions { |
| 9 | + pub inputs: Vec<String>, |
| 10 | + pub output: Option<String>, |
| 11 | + pub dry_run: bool, |
| 12 | + pub check: bool, |
| 13 | + pub no_placeholders: bool, |
| 14 | + pub key_style: String, |
| 15 | +} |
| 16 | + |
| 17 | +fn parse_key_style(input: &str) -> Result<KeyStyle, String> { |
| 18 | + match input.trim().to_ascii_lowercase().as_str() { |
| 19 | + "none" => Ok(KeyStyle::None), |
| 20 | + "snake" => Ok(KeyStyle::Snake), |
| 21 | + "kebab" => Ok(KeyStyle::Kebab), |
| 22 | + "camel" => Ok(KeyStyle::Camel), |
| 23 | + other => Err(format!( |
| 24 | + "Invalid --key-style '{}'. Expected one of: none, snake, kebab, camel", |
| 25 | + other |
| 26 | + )), |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +fn infer_output_format_from_path(path: &str) -> Result<FormatType, String> { |
| 31 | + langcodec::infer_format_from_extension(path) |
| 32 | + .ok_or_else(|| format!("Cannot infer format from path: {}", path)) |
| 33 | +} |
| 34 | + |
| 35 | +fn pick_single_resource(codec: &Codec) -> Result<&langcodec::Resource, String> { |
| 36 | + if codec.resources.len() == 1 { |
| 37 | + Ok(&codec.resources[0]) |
| 38 | + } else { |
| 39 | + Err( |
| 40 | + "Multiple languages present; single-language output requires exactly one resource" |
| 41 | + .to_string(), |
| 42 | + ) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +fn write_back(codec: &Codec, input_path: &str, output_path: &Option<String>) -> Result<(), String> { |
| 47 | + let input_owned = input_path.to_string(); |
| 48 | + let out = output_path.as_ref().unwrap_or(&input_owned); |
| 49 | + let fmt = infer_output_format_from_path(out)?; |
| 50 | + |
| 51 | + match fmt { |
| 52 | + FormatType::Strings(_) | FormatType::AndroidStrings(_) => { |
| 53 | + let resource = pick_single_resource(codec)?; |
| 54 | + Codec::write_resource_to_file(resource, out) |
| 55 | + .map_err(|e| format!("Error writing output: {}", e)) |
| 56 | + } |
| 57 | + FormatType::Xcstrings | FormatType::CSV | FormatType::TSV => { |
| 58 | + langcodec::converter::convert_resources_to_format(codec.resources.clone(), out, fmt) |
| 59 | + .map_err(|e| format!("Error writing output: {}", e)) |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +pub fn run_normalize_command(opts: NormalizeCliOptions) -> Result<(), String> { |
| 65 | + let expanded = path_glob::expand_input_globs(&opts.inputs) |
| 66 | + .map_err(|e| format!("Failed to expand input patterns: {}", e))?; |
| 67 | + if expanded.is_empty() { |
| 68 | + return Err("No input files matched the provided patterns".to_string()); |
| 69 | + } |
| 70 | + if expanded.len() > 1 { |
| 71 | + return Err("Normalize currently supports exactly one input file".to_string()); |
| 72 | + } |
| 73 | + |
| 74 | + let input = &expanded[0]; |
| 75 | + validate_file_path(input)?; |
| 76 | + if let Some(output) = &opts.output { |
| 77 | + validate_output_path(output)?; |
| 78 | + } |
| 79 | + |
| 80 | + let mut codec = Codec::new(); |
| 81 | + codec |
| 82 | + .read_file_by_extension(input, None) |
| 83 | + .map_err(|e| format!("Failed to read input '{}': {}", input, e))?; |
| 84 | + |
| 85 | + let key_style = parse_key_style(&opts.key_style)?; |
| 86 | + let report = normalize_codec( |
| 87 | + &mut codec, |
| 88 | + &EngineNormalizeOptions { |
| 89 | + normalize_placeholders: !opts.no_placeholders, |
| 90 | + key_style, |
| 91 | + }, |
| 92 | + ) |
| 93 | + .map_err(|e| e.to_string())?; |
| 94 | + |
| 95 | + if !report.changed { |
| 96 | + println!("No changes needed: {}", input); |
| 97 | + return Ok(()); |
| 98 | + } |
| 99 | + |
| 100 | + if opts.check { |
| 101 | + println!("would change: {}", input); |
| 102 | + return Err(format!("would change: {}", input)); |
| 103 | + } |
| 104 | + |
| 105 | + if opts.dry_run { |
| 106 | + println!("DRY-RUN: would change {}", input); |
| 107 | + return Ok(()); |
| 108 | + } |
| 109 | + |
| 110 | + write_back(&codec, input, &opts.output)?; |
| 111 | + println!("✅ Normalized: {}", opts.output.as_deref().unwrap_or(input)); |
3 | 112 | Ok(()) |
4 | 113 | } |
0 commit comments