|
| 1 | +// This file is part of the uutils coreutils package. |
| 2 | +// |
| 3 | +// For the full copyright and license information, please view the LICENSE |
| 4 | +// file that was distributed with this source code. |
| 5 | + |
| 6 | +use std::collections::HashMap; |
| 7 | +use std::fs::{self, File}; |
| 8 | +use std::io::Write; |
| 9 | +use std::path::Path; |
| 10 | + |
| 11 | +/// Generate embedded locale strings from .ftl files |
| 12 | +/// |
| 13 | +/// # Arguments |
| 14 | +/// * `out_dir` - The output directory for generated files |
| 15 | +/// * `project_root` - The root directory of the project |
| 16 | +/// |
| 17 | +/// # Errors |
| 18 | +/// |
| 19 | +/// Returns an error if file operations fail or if there are I/O issues |
| 20 | +pub fn generate_embedded_locale_strings( |
| 21 | + out_dir: &str, |
| 22 | + project_root: &Path, |
| 23 | +) -> Result<(), Box<dyn std::error::Error>> { |
| 24 | + let mut all_strings = HashMap::new(); |
| 25 | + |
| 26 | + // Collect all English strings from all utilities |
| 27 | + let uu_dir = project_root.join("src/uu"); |
| 28 | + if uu_dir.exists() { |
| 29 | + for entry in fs::read_dir(&uu_dir)? { |
| 30 | + let entry = entry?; |
| 31 | + let path = entry.path(); |
| 32 | + if path.is_dir() { |
| 33 | + let locale_path = path.join("locales/en-US.ftl"); |
| 34 | + if locale_path.exists() { |
| 35 | + let content = fs::read_to_string(&locale_path)?; |
| 36 | + parse_fluent_content(&content, &mut all_strings); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Also collect common locale strings from uucore if they exist |
| 43 | + let common_locale_path = project_root.join("src/uucore/locales/en-US.ftl"); |
| 44 | + if common_locale_path.exists() { |
| 45 | + let content = fs::read_to_string(&common_locale_path)?; |
| 46 | + parse_fluent_content(&content, &mut all_strings); |
| 47 | + } |
| 48 | + |
| 49 | + // Generate Rust code with embedded strings |
| 50 | + generate_embedded_rust_code(out_dir, &all_strings)?; |
| 51 | + Ok(()) |
| 52 | +} |
| 53 | + |
| 54 | +/// Parse Fluent file content and extract key-value pairs |
| 55 | +fn parse_fluent_content(content: &str, all_strings: &mut HashMap<String, String>) { |
| 56 | + for line in content.lines() { |
| 57 | + let line = line.trim(); |
| 58 | + if line.is_empty() || line.starts_with('#') { |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + if let Some(equals_pos) = line.find(" = ") { |
| 63 | + let key = line[..equals_pos].trim(); |
| 64 | + let value = line[equals_pos + 3..].trim(); |
| 65 | + all_strings.insert(key.to_string(), value.to_string()); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/// Generate Rust code with embedded strings as individual consts and lookup function |
| 71 | +/// |
| 72 | +/// # Errors |
| 73 | +/// |
| 74 | +/// Returns an error if file creation or writing operations fail |
| 75 | +fn generate_embedded_rust_code( |
| 76 | + out_dir: &str, |
| 77 | + all_strings: &HashMap<String, String>, |
| 78 | +) -> Result<(), Box<dyn std::error::Error>> { |
| 79 | + let mut embedded_file = File::create(Path::new(out_dir).join("embedded_locale.rs"))?; |
| 80 | + |
| 81 | + writeln!(embedded_file, "// Generated at compile time - do not edit")?; |
| 82 | + writeln!(embedded_file)?; |
| 83 | + |
| 84 | + // Generate individual const strings |
| 85 | + for (key, value) in all_strings { |
| 86 | + let const_name = create_rust_identifier(key); |
| 87 | + |
| 88 | + // Escape the value for Rust string literal |
| 89 | + let escaped_value = value |
| 90 | + .replace('\\', "\\\\") |
| 91 | + .replace('"', "\\\"") |
| 92 | + .replace('\n', "\\n"); |
| 93 | + writeln!( |
| 94 | + embedded_file, |
| 95 | + "pub const {const_name}: &str = {escaped_value:?};" |
| 96 | + )?; |
| 97 | + } |
| 98 | + |
| 99 | + writeln!(embedded_file)?; |
| 100 | + |
| 101 | + // Generate lookup function |
| 102 | + writeln!( |
| 103 | + embedded_file, |
| 104 | + "pub fn get_embedded_string(id: &str) -> Option<&'static str> {{" |
| 105 | + )?; |
| 106 | + writeln!(embedded_file, "match id {{")?; |
| 107 | + |
| 108 | + for key in all_strings.keys() { |
| 109 | + let const_name = create_rust_identifier(key); |
| 110 | + writeln!(embedded_file, " {key:?} => Some({const_name}),")?; |
| 111 | + } |
| 112 | + |
| 113 | + writeln!(embedded_file, " _ => None,")?; |
| 114 | + writeln!(embedded_file, " }}")?; |
| 115 | + writeln!(embedded_file, "}}")?; |
| 116 | + |
| 117 | + embedded_file.flush()?; |
| 118 | + Ok(()) |
| 119 | +} |
| 120 | + |
| 121 | +/// Create a valid Rust identifier from a Fluent key |
| 122 | +fn create_rust_identifier(key: &str) -> String { |
| 123 | + let const_name = key |
| 124 | + .chars() |
| 125 | + .map(|c| if c.is_alphanumeric() { c } else { '_' }) |
| 126 | + .collect::<String>() |
| 127 | + .to_uppercase(); |
| 128 | + |
| 129 | + // Ensure it doesn't start with a number |
| 130 | + if const_name.chars().next().unwrap_or('_').is_numeric() { |
| 131 | + format!("S_{const_name}") |
| 132 | + } else { |
| 133 | + const_name |
| 134 | + } |
| 135 | +} |
0 commit comments