ass_core/utils/errors/
format.rs1use super::CoreError;
8
9use core::fmt;
10
11#[cfg(not(feature = "std"))]
12extern crate alloc;
13#[cfg(not(feature = "std"))]
14use alloc::format;
15
16pub fn invalid_color<T: fmt::Display>(format: T) -> CoreError {
34 CoreError::InvalidColor(format!("{format}"))
35}
36
37pub fn invalid_numeric<T: fmt::Display>(value: T, reason: &str) -> CoreError {
47 CoreError::InvalidNumeric(format!("'{value}': {reason}"))
48}
49
50pub fn invalid_time<T: fmt::Display>(time: T, reason: &str) -> CoreError {
60 CoreError::InvalidTime(format!("'{time}': {reason}"))
61}
62
63pub fn validate_color_format(color: &str) -> Result<(), CoreError> {
78 let trimmed = color.trim();
79
80 if trimmed.is_empty() {
81 return Err(invalid_color("Empty color value"));
82 }
83
84 if trimmed.starts_with("&H") || trimmed.starts_with("&h") {
86 let hex_part = if trimmed.ends_with('&') {
87 &trimmed[2..trimmed.len() - 1]
88 } else {
89 &trimmed[2..]
90 };
91
92 if hex_part.len() != 6 && hex_part.len() != 8 {
93 return Err(invalid_color(format!(
94 "Hex color '{trimmed}' must be 6 or 8 characters after &H"
95 )));
96 }
97
98 if !hex_part.chars().all(|c| c.is_ascii_hexdigit()) {
99 return Err(invalid_color(format!(
100 "Invalid hex digits in color '{trimmed}'"
101 )));
102 }
103 } else {
104 if trimmed.parse::<u32>().is_err() {
106 return Err(invalid_color(format!(
107 "Color '{trimmed}' is neither valid hex (&HBBGGRR) nor decimal"
108 )));
109 }
110 }
111
112 Ok(())
113}
114
115#[cfg(test)]
116mod tests {
117 use super::*;
118
119 #[test]
120 fn color_error_creation() {
121 let error = invalid_color("invalid");
122 assert!(matches!(error, CoreError::InvalidColor(_)));
123 }
124
125 #[test]
126 fn numeric_error_creation() {
127 let error = invalid_numeric("abc", "not a number");
128 assert!(matches!(error, CoreError::InvalidNumeric(_)));
129 }
130
131 #[test]
132 fn time_error_creation() {
133 let error = invalid_time("invalid", "wrong format");
134 assert!(matches!(error, CoreError::InvalidTime(_)));
135 }
136
137 #[test]
138 fn validate_hex_color() {
139 assert!(validate_color_format("&H00FF00FF").is_ok());
140 assert!(validate_color_format("&H00FF00FF&").is_ok());
141 assert!(validate_color_format("&h00ff00ff").is_ok());
142 assert!(validate_color_format("&HFFFFFF").is_ok());
143 }
144
145 #[test]
146 fn validate_decimal_color() {
147 assert!(validate_color_format("16777215").is_ok()); assert!(validate_color_format("0").is_ok()); }
150
151 #[test]
152 fn invalid_hex_color() {
153 assert!(validate_color_format("&HGG0000").is_err());
154 assert!(validate_color_format("&H123").is_err()); assert!(validate_color_format("&H123456789").is_err()); }
157
158 #[test]
159 fn invalid_color_format() {
160 assert!(validate_color_format("").is_err());
161 assert!(validate_color_format("invalid").is_err());
162 assert!(validate_color_format("#FF0000").is_err()); }
164}