1use chrono::{DateTime, NaiveDate, Utc};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
5pub struct VolcanoReport {
6 pub date: NaiveDate,
7 pub report_time: DateTime<Utc>,
8
9 pub exhalations: u32,
11 pub volcanotectonic_events: u32,
12 pub tremor_minutes_total: u32,
13 pub tremor_high_frequency_minutes: Option<u32>,
14 pub tremor_harmonic_minutes: Option<u32>,
15 pub explosions: u32,
16
17 pub so2_emissions_tons_per_day: Option<f64>,
19 pub so2_measurement_date: Option<NaiveDate>,
20
21 pub alert_level: AlertLevel,
23 pub alert_phase: String, pub wind_direction: Option<WindDirection>,
27
28 pub summary_spanish: String,
30
31 pub image_urls: Vec<String>,
33 pub video_urls: Vec<String>,
34
35 pub source_url: String,
37 pub scraped_at: DateTime<Utc>,
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
41#[serde(rename_all = "UPPERCASE")]
42pub enum AlertLevel {
43 Green,
44 Yellow,
45 Orange,
46 Red,
47}
48
49impl AlertLevel {
50 pub fn from_spanish(text: &str) -> Option<Self> {
51 let text_lower = text.to_lowercase();
52 if text_lower.contains("verde") {
53 Some(AlertLevel::Green)
54 } else if text_lower.contains("amarillo") {
55 Some(AlertLevel::Yellow)
56 } else if text_lower.contains("naranja") {
57 Some(AlertLevel::Orange)
58 } else if text_lower.contains("rojo") {
59 Some(AlertLevel::Red)
60 } else {
61 None
62 }
63 }
64}
65
66#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
67#[serde(rename_all = "kebab-case")]
68pub enum WindDirection {
69 N,
70 NNE,
71 NE,
72 ENE,
73 E,
74 ESE,
75 SE,
76 SSE,
77 S,
78 SSW,
79 SW,
80 WSW,
81 W,
82 WNW,
83 NW,
84 NNW,
85}
86
87impl WindDirection {
88 pub fn from_spanish(text: &str) -> Option<Self> {
89 let normalized = text.to_lowercase().replace([' ', '-'], "");
90
91 if normalized.contains("nortenoroeste") || normalized.contains("nornoroeste") {
93 return Some(WindDirection::NNW);
94 }
95 if normalized.contains("noroeste") {
96 return Some(WindDirection::NW);
97 }
98 if normalized.contains("oestenoroeste") || normalized.contains("oestennoroeste") {
99 return Some(WindDirection::WNW);
100 }
101 if normalized.contains("oestesuroeste") || normalized.contains("oestessuroeste") {
102 return Some(WindDirection::WSW);
103 }
104 if normalized.contains("suroeste") {
105 return Some(WindDirection::SW);
106 }
107 if normalized.contains("sursuroeste") || normalized.contains("surssuroeste") {
108 return Some(WindDirection::SSW);
109 }
110 if normalized.contains("sursureste") || normalized.contains("surssureste") {
111 return Some(WindDirection::SSE);
112 }
113 if normalized.contains("sureste") {
114 return Some(WindDirection::SE);
115 }
116 if normalized.contains("estesureste") || normalized.contains("estessureste") {
117 return Some(WindDirection::ESE);
118 }
119 if normalized.contains("estenoreste") {
120 return Some(WindDirection::ENE);
121 }
122 if normalized.contains("noreste") {
123 return Some(WindDirection::NE);
124 }
125 if normalized.contains("nortenoreste") || normalized.contains("nornoreste") {
126 return Some(WindDirection::NNE);
127 }
128
129 if normalized.contains("norte") {
131 return Some(WindDirection::N);
132 }
133 if normalized.contains("oeste") || normalized.contains("oeste") {
134 return Some(WindDirection::W);
135 }
136 if normalized.contains("sur") {
137 return Some(WindDirection::S);
138 }
139 if normalized.contains("este") {
140 return Some(WindDirection::E);
141 }
142
143 None
144 }
145}
146
147#[cfg(test)]
148mod tests {
149 use super::*;
150
151 #[test]
152 fn test_alert_level_from_spanish() {
153 assert_eq!(
154 AlertLevel::from_spanish("Amarillo Fase 2"),
155 Some(AlertLevel::Yellow)
156 );
157 assert_eq!(
158 AlertLevel::from_spanish("VERDE"),
159 Some(AlertLevel::Green)
160 );
161 assert_eq!(AlertLevel::from_spanish("rojo"), Some(AlertLevel::Red));
162 assert_eq!(
163 AlertLevel::from_spanish("NARANJA"),
164 Some(AlertLevel::Orange)
165 );
166 assert_eq!(AlertLevel::from_spanish("unknown"), None);
167 }
168
169 #[test]
170 fn test_wind_direction_from_spanish() {
171 assert_eq!(
172 WindDirection::from_spanish("norte-noroeste"),
173 Some(WindDirection::NNW)
174 );
175 assert_eq!(
176 WindDirection::from_spanish("NORESTE"),
177 Some(WindDirection::NE)
178 );
179 assert_eq!(WindDirection::from_spanish("sur"), Some(WindDirection::S));
180 assert_eq!(
181 WindDirection::from_spanish("oeste-suroeste"),
182 Some(WindDirection::WSW)
183 );
184 }
185
186 #[test]
187 fn test_volcano_report_serialization() {
188 let report = VolcanoReport {
189 date: NaiveDate::from_ymd_opt(2025, 10, 6).unwrap(),
190 report_time: Utc::now(),
191 exhalations: 15,
192 volcanotectonic_events: 0,
193 tremor_minutes_total: 53,
194 tremor_high_frequency_minutes: Some(39),
195 tremor_harmonic_minutes: Some(14),
196 explosions: 0,
197 so2_emissions_tons_per_day: Some(2603.0),
198 so2_measurement_date: Some(NaiveDate::from_ymd_opt(2024, 10, 10).unwrap()),
199 alert_level: AlertLevel::Yellow,
200 alert_phase: "AMARILLO FASE 2".to_string(),
201 wind_direction: Some(WindDirection::NNW),
202 summary_spanish: "Test summary".to_string(),
203 image_urls: vec!["https://example.com/img.jpg".to_string()],
204 video_urls: vec!["https://example.com/vid.mp4".to_string()],
205 source_url: "https://www.cenapred.unam.mx/...".to_string(),
206 scraped_at: Utc::now(),
207 };
208
209 let json = serde_json::to_string_pretty(&report).unwrap();
210 assert!(json.contains("\"exhalations\": 15"));
211 assert!(json.contains("\"alert_level\": \"YELLOW\""));
212 }
213}