ratatui_toolkit/services/theme/persistence/
mod.rs

1//! Theme persistence for saving and loading user theme preferences.
2//!
3//! This module provides functions to save and load the user's selected theme
4//! to a configuration file, allowing the preference to persist across sessions.
5//!
6//! # Default Config Location
7//!
8//! By default, the config is stored at:
9//! - Linux/macOS: `~/.config/ratatui-toolkit/theme.json`
10//! - Windows: `%APPDATA%\ratatui-toolkit\theme.json`
11//!
12//! # Example
13//!
14//! ```rust,no_run
15//! use ratatui_toolkit::services::theme::persistence::{save_theme, load_saved_theme};
16//!
17//! // Save the current theme
18//! save_theme("ayu", None).ok();
19//!
20//! // Load the saved theme on startup
21//! if let Some(theme_name) = load_saved_theme(None) {
22//!     println!("Loaded theme: {}", theme_name);
23//! }
24//! ```
25
26mod clear_saved_theme;
27mod default_config_dir;
28mod default_config_path;
29mod load_saved_theme;
30mod save_theme;
31mod theme_config;
32
33// Re-export public API
34pub use clear_saved_theme::clear_saved_theme;
35pub use default_config_dir::default_config_dir;
36pub use default_config_path::default_config_path;
37pub use load_saved_theme::load_saved_theme;
38pub use save_theme::save_theme;
39pub use theme_config::ThemeConfig;
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44    use std::fs;
45    use tempfile::tempdir;
46
47    #[test]
48    fn test_save_and_load_theme() {
49        let dir = tempdir().unwrap();
50        let path = dir.path().join("theme.json");
51
52        save_theme("dracula", Some(path.clone())).unwrap();
53
54        let loaded = load_saved_theme(Some(path.clone()));
55        assert_eq!(loaded, Some("dracula".to_string()));
56    }
57
58    #[test]
59    fn test_load_nonexistent() {
60        let dir = tempdir().unwrap();
61        let path = dir.path().join("nonexistent.json");
62
63        let loaded = load_saved_theme(Some(path));
64        assert_eq!(loaded, None);
65    }
66
67    #[test]
68    fn test_clear_theme() {
69        let dir = tempdir().unwrap();
70        let path = dir.path().join("theme.json");
71
72        save_theme("ayu", Some(path.clone())).unwrap();
73        assert!(path.exists());
74
75        clear_saved_theme(Some(path.clone())).unwrap();
76        assert!(!path.exists());
77    }
78}