tailwind_rs_core/
error.rs1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, TailwindError>;
7
8#[derive(Error, Debug)]
10pub enum TailwindError {
11 #[error("Configuration error: {message}")]
13 Config { message: String },
14
15 #[error("Theme error: {message}")]
17 Theme { message: String },
18
19 #[error("Class generation error: {message}")]
21 ClassGeneration { message: String },
22
23 #[error("Build error: {message}")]
25 Build { message: String },
26
27 #[error("Validation error: {message}")]
29 Validation { message: String },
30
31 #[error("File I/O error: {0}")]
33 Io(#[from] std::io::Error),
34
35 #[error("JSON error: {0}")]
37 Json(#[from] serde_json::Error),
38
39 #[error("Generic error: {0}")]
41 Generic(#[from] anyhow::Error),
42}
43
44impl TailwindError {
45 pub fn config(message: impl Into<String>) -> Self {
47 Self::Config {
48 message: message.into(),
49 }
50 }
51
52 pub fn theme(message: impl Into<String>) -> Self {
54 Self::Theme {
55 message: message.into(),
56 }
57 }
58
59 pub fn class_generation(message: impl Into<String>) -> Self {
61 Self::ClassGeneration {
62 message: message.into(),
63 }
64 }
65
66 pub fn build(message: impl Into<String>) -> Self {
68 Self::Build {
69 message: message.into(),
70 }
71 }
72
73 pub fn validation(message: impl Into<String>) -> Self {
75 Self::Validation {
76 message: message.into(),
77 }
78 }
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 #[test]
86 fn test_config_error() {
87 let error = TailwindError::config("Invalid configuration");
88 assert!(matches!(error, TailwindError::Config { .. }));
89 assert!(error.to_string().contains("Invalid configuration"));
90 }
91
92 #[test]
93 fn test_theme_error() {
94 let error = TailwindError::theme("Theme not found");
95 assert!(matches!(error, TailwindError::Theme { .. }));
96 assert!(error.to_string().contains("Theme not found"));
97 }
98
99 #[test]
100 fn test_class_generation_error() {
101 let error = TailwindError::class_generation("Invalid class name");
102 assert!(matches!(error, TailwindError::ClassGeneration { .. }));
103 assert!(error.to_string().contains("Invalid class name"));
104 }
105
106 #[test]
107 fn test_build_error() {
108 let error = TailwindError::build("Build failed");
109 assert!(matches!(error, TailwindError::Build { .. }));
110 assert!(error.to_string().contains("Build failed"));
111 }
112
113 #[test]
114 fn test_io_error_conversion() {
115 let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
116 let tailwind_error: TailwindError = io_error.into();
117 assert!(matches!(tailwind_error, TailwindError::Io(_)));
118 }
119
120 #[test]
121 fn test_json_error_conversion() {
122 let json_error = serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err();
123 let tailwind_error: TailwindError = json_error.into();
124 assert!(matches!(tailwind_error, TailwindError::Json(_)));
125 }
126
127 #[test]
128 fn test_generic_error_conversion() {
129 let anyhow_error = anyhow::anyhow!("Generic error");
130 let tailwind_error: TailwindError = anyhow_error.into();
131 assert!(matches!(tailwind_error, TailwindError::Generic(_)));
132 }
133}