pacsea 0.8.2

A fast, friendly TUI for browsing and installing Arch and AUR packages with built-in news and security scanning
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
//! Locale file loading and parsing.

use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

use crate::i18n::translations::TranslationMap;

/// What: Load a locale YAML file and parse it into a `TranslationMap`.
///
/// Inputs:
/// - `locale`: Locale code (e.g., "de-DE")
/// - `locales_dir`: Path to locales directory
///
/// Output:
/// - `Result<TranslationMap, String>` containing translations or error
///
/// # Errors
/// - Returns `Err` when the locale code is empty or has an invalid format
/// - Returns `Err` when the locale file does not exist in the locales directory
/// - Returns `Err` when the locale file cannot be read (I/O error)
/// - Returns `Err` when the locale file is empty
/// - Returns `Err` when the YAML content cannot be parsed
///
/// Details:
/// - Loads file from `locales_dir/{locale}.yml`
/// - Parses YAML structure into nested `HashMap`
/// - Returns error if file not found or invalid YAML
/// - Validates locale format before attempting to load
pub fn load_locale_file(locale: &str, locales_dir: &Path) -> Result<TranslationMap, String> {
    // Validate locale format
    if locale.is_empty() {
        return Err("Locale code cannot be empty".to_string());
    }

    if !is_valid_locale_format(locale) {
        return Err(format!(
            "Invalid locale code format: '{locale}'. Expected format: language[-region] (e.g., 'en-US', 'de-DE')"
        ));
    }

    let file_path = locales_dir.join(format!("{locale}.yml"));

    if !file_path.exists() {
        return Err(format!(
            "Locale file not found: {}. Available locales can be checked in the locales/ directory.",
            file_path.display()
        ));
    }

    let contents = fs::read_to_string(&file_path)
        .map_err(|e| format!("Failed to read locale file {}: {e}", file_path.display()))?;

    if contents.trim().is_empty() {
        return Err(format!("Locale file is empty: {}", file_path.display()));
    }

    parse_locale_yaml(&contents).map_err(|e| {
        format!(
            "Failed to parse locale file {}: {}. Please check YAML syntax.",
            file_path.display(),
            e
        )
    })
}

/// What: Validate locale code format (same as resolver).
///
/// Inputs:
/// - `locale`: Locale code to validate
///
/// Output:
/// - `true` if format looks valid, `false` otherwise
fn is_valid_locale_format(locale: &str) -> bool {
    if locale.is_empty() || locale.len() > 20 {
        return false;
    }

    locale.chars().all(|c| c.is_alphanumeric() || c == '-')
        && !locale.starts_with('-')
        && !locale.ends_with('-')
        && !locale.contains("--")
}

/// What: Parse YAML content into a `TranslationMap`.
///
/// Inputs:
/// - `yaml_content`: YAML file content as string
///
/// Output:
/// - `Result<TranslationMap, String>` containing parsed translations
///
/// Details:
/// - Expects top-level key matching locale code (e.g., "de-DE:")
/// - Flattens nested structure into dot-notation keys
fn parse_locale_yaml(yaml_content: &str) -> Result<TranslationMap, String> {
    let doc: serde_norway::Value =
        serde_norway::from_str(yaml_content).map_err(|e| format!("Failed to parse YAML: {e}"))?;

    let mut translations = HashMap::new();

    // Get the top-level locale key (e.g., "de-DE")
    if let Some(locale_obj) = doc.as_mapping() {
        for (_locale_key, locale_value) in locale_obj {
            // Flatten the nested structure (skip the top-level locale key)
            flatten_yaml_value(locale_value, "", &mut translations);
        }
    }

    Ok(translations)
}

/// What: Recursively flatten YAML structure into dot-notation keys.
///
/// Inputs:
/// - `value`: Current YAML value
/// - `prefix`: Current key prefix (e.g., "app.titles")
/// - `translations`: Map to populate
///
/// Details:
/// - Converts nested maps to dot-notation (e.g., app.titles.search)
/// - Handles arrays by preserving them as YAML values
fn flatten_yaml_value(
    value: &serde_norway::Value,
    prefix: &str,
    translations: &mut TranslationMap,
) {
    match value {
        serde_norway::Value::Mapping(map) => {
            for (key, val) in map {
                if let Some(key_str) = key.as_str() {
                    let new_prefix = if prefix.is_empty() {
                        key_str.to_string()
                    } else {
                        format!("{prefix}.{key_str}")
                    };
                    flatten_yaml_value(val, &new_prefix, translations);
                }
            }
        }
        serde_norway::Value::String(s) => {
            translations.insert(prefix.to_string(), s.clone());
        }
        serde_norway::Value::Sequence(_seq) => {
            // Store arrays as YAML strings for now
            // Can be enhanced later to handle arrays properly
            if let Ok(yaml_str) = serde_norway::to_string(value) {
                translations.insert(prefix.to_string(), yaml_str.trim().to_string());
            }
        }
        _ => {
            // Convert other types to string representation
            let val_str = value.as_str().map_or_else(
                || {
                    value.as_i64().map_or_else(
                        || {
                            value.as_f64().map_or_else(
                                || value.as_bool().map_or_else(String::new, |b| b.to_string()),
                                |n| n.to_string(),
                            )
                        },
                        |n| n.to_string(),
                    )
                },
                std::string::ToString::to_string,
            );
            translations.insert(prefix.to_string(), val_str);
        }
    }
}

/// Locale loader that caches loaded translations.
pub struct LocaleLoader {
    /// Directory containing locale translation files.
    locales_dir: PathBuf,
    /// Cache of loaded translations by locale name.
    cache: HashMap<String, TranslationMap>,
}

impl LocaleLoader {
    /// What: Create a new `LocaleLoader`.
    ///
    /// Inputs:
    /// - `locales_dir`: Path to locales directory
    ///
    /// Output:
    /// - `LocaleLoader` instance
    #[must_use]
    pub fn new(locales_dir: PathBuf) -> Self {
        Self {
            locales_dir,
            cache: HashMap::new(),
        }
    }

    /// What: Load locale file, using cache if available.
    ///
    /// Inputs:
    /// - `locale`: Locale code to load
    ///
    /// Output:
    /// - `Result<TranslationMap, String>` containing translations
    ///
    /// # Errors
    /// - Returns `Err` when the locale file cannot be loaded (see `load_locale_file` for specific error conditions)
    ///
    /// # Panics
    /// - Panics if the cache is modified between the `contains_key` check and the `get` call (should not happen in single-threaded usage)
    ///
    /// Details:
    /// - Caches loaded translations to avoid re-reading files
    /// - Returns cached version if available
    /// - Logs warnings for missing or invalid locale files
    pub fn load(&mut self, locale: &str) -> Result<TranslationMap, String> {
        if self.cache.contains_key(locale) {
            Ok(self
                .cache
                .get(locale)
                .expect("locale should be in cache after contains_key check")
                .clone())
        } else {
            match load_locale_file(locale, &self.locales_dir) {
                Ok(translations) => {
                    let key_count = translations.len();
                    tracing::debug!(
                        "Loaded locale '{}' with {} translation keys",
                        locale,
                        key_count
                    );
                    self.cache.insert(locale.to_string(), translations.clone());
                    Ok(translations)
                }
                Err(e) => {
                    tracing::warn!("Failed to load locale '{}': {}", locale, e);
                    Err(e)
                }
            }
        }
    }

    /// What: Get locales directory path.
    #[must_use]
    pub fn locales_dir(&self) -> &Path {
        &self.locales_dir
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_parse_locale_yaml() {
        let yaml = r#"
de-DE:
  app:
    titles:
      search: "Suche"
      help: "Hilfe"
"#;
        let result = parse_locale_yaml(yaml).expect("Failed to parse test locale YAML");
        assert_eq!(result.get("app.titles.search"), Some(&"Suche".to_string()));
        assert_eq!(result.get("app.titles.help"), Some(&"Hilfe".to_string()));
    }

    #[test]
    fn test_parse_locale_yaml_nested() {
        let yaml = r#"
en-US:
  app:
    modals:
      preflight:
        title_install: " Preflight: Install "
        tabs:
          summary: "Summary"
          deps: "Deps"
"#;
        let result = parse_locale_yaml(yaml).expect("Failed to parse test locale YAML");
        assert_eq!(
            result.get("app.modals.preflight.title_install"),
            Some(&" Preflight: Install ".to_string())
        );
        assert_eq!(
            result.get("app.modals.preflight.tabs.summary"),
            Some(&"Summary".to_string())
        );
        assert_eq!(
            result.get("app.modals.preflight.tabs.deps"),
            Some(&"Deps".to_string())
        );
    }

    #[test]
    fn test_parse_locale_yaml_invalid() {
        let yaml = "invalid: yaml: content: [";
        assert!(parse_locale_yaml(yaml).is_err());
    }

    #[test]
    fn test_load_locale_file() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory for test");
        let locales_dir = temp_dir.path();

        // Create a test locale file
        let locale_file = locales_dir.join("test-LOCALE.yml");
        let yaml_content = r#"
test-LOCALE:
  app:
    titles:
      search: "Test Search"
"#;
        fs::write(&locale_file, yaml_content).expect("Failed to write test locale file");

        let result =
            load_locale_file("test-LOCALE", locales_dir).expect("Failed to load test locale file");
        assert_eq!(
            result.get("app.titles.search"),
            Some(&"Test Search".to_string())
        );
    }

    #[test]
    fn test_load_locale_file_not_found() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory for test");
        let locales_dir = temp_dir.path();

        let result = load_locale_file("nonexistent", locales_dir);
        assert!(result.is_err());
        assert!(
            result
                .expect_err("Expected error for nonexistent locale file")
                .contains("not found")
        );
    }

    #[test]
    fn test_load_locale_file_invalid_format() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory for test");
        let locales_dir = temp_dir.path();

        // Test with invalid locale format
        let result = load_locale_file("invalid-format-", locales_dir);
        assert!(result.is_err());
        assert!(
            result
                .expect_err("Expected error for invalid locale format")
                .contains("Invalid locale code format")
        );
    }

    #[test]
    fn test_load_locale_file_empty() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory for test");
        let locales_dir = temp_dir.path();

        // Create an empty locale file
        let locale_file = locales_dir.join("empty.yml");
        fs::write(&locale_file, "").expect("Failed to write empty test locale file");

        let result = load_locale_file("empty", locales_dir);
        assert!(result.is_err());
        assert!(
            result
                .expect_err("Expected error for empty locale file")
                .contains("empty")
        );
    }

    #[test]
    fn test_locale_loader_caching() {
        let temp_dir = TempDir::new().expect("Failed to create temp directory for test");
        let locales_dir = temp_dir.path();

        // Create a test locale file
        let locale_file = locales_dir.join("cache-test.yml");
        let yaml_content = r#"
cache-test:
  app:
    titles:
      search: "Cached"
"#;
        fs::write(&locale_file, yaml_content).expect("Failed to write test locale file");

        let mut loader = LocaleLoader::new(locales_dir.to_path_buf());

        // First load
        let result1 = loader
            .load("cache-test")
            .expect("Failed to load locale in test");
        assert_eq!(
            result1.get("app.titles.search"),
            Some(&"Cached".to_string())
        );

        // Second load should use cache
        let result2 = loader
            .load("cache-test")
            .expect("Failed to load cached locale in test");
        assert_eq!(
            result2.get("app.titles.search"),
            Some(&"Cached".to_string())
        );

        // Both should be the same reference (cached)
        assert_eq!(result1.len(), result2.len());
    }

    #[test]
    fn test_is_valid_locale_format() {
        // Valid formats
        assert!(is_valid_locale_format("en-US"));
        assert!(is_valid_locale_format("de-DE"));
        assert!(is_valid_locale_format("zh-Hans-CN"));
        assert!(is_valid_locale_format("en"));

        // Invalid formats
        assert!(!is_valid_locale_format(""));
        assert!(!is_valid_locale_format("-en-US"));
        assert!(!is_valid_locale_format("en-US-"));
        assert!(!is_valid_locale_format("en--US"));
        assert!(!is_valid_locale_format("en US"));
    }
}