1use std::path::Path;
2
3use crate::error::ThemeError;
4use crate::template::ThemeTemplates;
5
6const DEFAULT_THEME_CSS: &str = include_str!("../assets/default.css");
7
8#[derive(Debug, Clone)]
14pub struct Theme {
15 pub css: String,
17
18 pub templates: ThemeTemplates,
20}
21
22impl Theme {
23 pub fn default_theme() -> Self {
28 Self {
29 css: DEFAULT_THEME_CSS.to_string(),
30 templates: ThemeTemplates::default(),
31 }
32 }
33
34 pub fn from_path(path: &str) -> Result<Self, ThemeError> {
45 let dir = Path::new(path);
46 if !dir.exists() {
47 return Err(ThemeError::NotFound(path.to_string()));
48 }
49
50 let css_path = dir.join("tokens.css");
51 let css = std::fs::read_to_string(css_path)?;
52
53 let templates_path = dir.join("theme.json");
54 let templates = if templates_path.exists() {
55 let json = std::fs::read_to_string(&templates_path)?;
56 serde_json::from_str(&json)?
57 } else {
58 ThemeTemplates::default()
59 };
60
61 Ok(Self { css, templates })
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[test]
70 fn default_theme_returns_non_empty_css_with_color_primary() {
71 let theme = Theme::default_theme();
72 assert!(!theme.css.is_empty());
73 assert!(theme.css.contains("--color-primary"));
74 }
75
76 #[test]
77 fn default_theme_returns_all_none_templates() {
78 let theme = Theme::default_theme();
79 assert!(theme.templates.browse.is_none());
80 assert!(theme.templates.focus.is_none());
81 assert!(theme.templates.collect.is_none());
82 assert!(theme.templates.process.is_none());
83 assert!(theme.templates.summarize.is_none());
84 assert!(theme.templates.analyze.is_none());
85 assert!(theme.templates.track.is_none());
86 }
87
88 #[test]
89 fn from_path_loads_tokens_css_and_theme_json() {
90 let dir = tempfile::tempdir().unwrap();
91 let css_content = "@theme { --color-primary: oklch(55% 0.2 250); }";
92 let json_content = r#"{"browse": {"display": {"slots": ["title", "fields"]}}}"#;
93 std::fs::write(dir.path().join("tokens.css"), css_content).unwrap();
94 std::fs::write(dir.path().join("theme.json"), json_content).unwrap();
95
96 let theme = Theme::from_path(dir.path().to_str().unwrap()).unwrap();
97 assert_eq!(theme.css, css_content);
98 assert!(theme.templates.browse.is_some());
99 let browse = theme.templates.browse.unwrap();
100 assert_eq!(browse.display.slots, vec!["title", "fields"]);
101 }
102
103 #[test]
104 fn from_path_works_without_theme_json() {
105 let dir = tempfile::tempdir().unwrap();
106 let css_content = "@theme { --color-primary: oklch(55% 0.2 250); }";
107 std::fs::write(dir.path().join("tokens.css"), css_content).unwrap();
108
109 let theme = Theme::from_path(dir.path().to_str().unwrap()).unwrap();
110 assert_eq!(theme.css, css_content);
111 assert!(theme.templates.browse.is_none());
112 }
113
114 #[test]
115 fn from_path_returns_not_found_for_nonexistent_directory() {
116 let result = Theme::from_path("/nonexistent/path/that/does/not/exist");
117 assert!(matches!(result, Err(ThemeError::NotFound(_))));
118 }
119
120 #[test]
121 fn from_path_returns_json_error_for_invalid_theme_json() {
122 let dir = tempfile::tempdir().unwrap();
123 std::fs::write(dir.path().join("tokens.css"), "@theme {}").unwrap();
124 std::fs::write(dir.path().join("theme.json"), "not valid json{{").unwrap();
125
126 let result = Theme::from_path(dir.path().to_str().unwrap());
127 assert!(matches!(result, Err(ThemeError::Json(_))));
128 }
129
130 #[test]
131 fn from_path_returns_io_error_when_tokens_css_missing() {
132 let dir = tempfile::tempdir().unwrap();
133 std::fs::write(dir.path().join("theme.json"), "{}").unwrap();
135
136 let result = Theme::from_path(dir.path().to_str().unwrap());
137 assert!(matches!(result, Err(ThemeError::Io(_))));
138 }
139}