ratatui_toolkit/services/theme/loader/
load_theme_file.rs

1//! Load theme from a JSON file path.
2
3use std::fs;
4use std::path::Path;
5
6use crate::services::theme::app_theme::AppTheme;
7use crate::services::theme::loader::load_theme_str::load_theme_str;
8use crate::services::theme::ThemeVariant;
9
10/// Loads an [`AppTheme`] from a JSON file path.
11///
12/// Reads the file contents and delegates to [`load_theme_str`] for parsing.
13///
14/// # Arguments
15///
16/// * `path` - Path to the JSON theme file
17/// * `variant` - Which theme variant (dark/light) to use
18///
19/// # Returns
20///
21/// `Ok(AppTheme)` if the file can be read and parsed successfully,
22/// `Err` with a description if reading or parsing fails.
23///
24/// # Errors
25///
26/// Returns an error if:
27/// - The file cannot be read
28/// - The JSON is malformed
29/// - Required color keys are missing
30/// - Color values cannot be resolved
31///
32/// # Example
33///
34/// ```rust,no_run
35/// use ratatui_toolkit::services::theme::{loader, ThemeVariant};
36///
37/// let theme = loader::load_theme_file("themes/gruvbox.json", ThemeVariant::Dark)
38///     .expect("Failed to load theme");
39/// ```
40pub fn load_theme_file<P: AsRef<Path>>(path: P, variant: ThemeVariant) -> Result<AppTheme, String> {
41    let path = path.as_ref();
42
43    let contents =
44        fs::read_to_string(path).map_err(|e| format!("Failed to read file {:?}: {}", path, e))?;
45
46    load_theme_str(&contents, variant)
47}