use chrono::{DateTime, NaiveDate, Utc};
use serde::{Deserialize, Serialize};
pub const SCHEMA_VERSION: u32 = 1;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct VolcanoReport {
#[serde(default = "default_schema_version")]
pub schema_version: u32,
pub date: NaiveDate,
#[serde(default)]
pub exhalations: Option<u32>,
#[serde(default)]
pub volcanotectonic_events: Option<u32>,
#[serde(default)]
pub tremor_minutes_total: Option<u32>,
#[serde(default)]
pub tremor_high_frequency_minutes: Option<u32>,
#[serde(default)]
pub tremor_harmonic_minutes: Option<u32>,
#[serde(default)]
pub explosions: Option<u32>,
#[serde(default)]
pub so2_emissions_tons_per_day: Option<f64>,
#[serde(default)]
pub so2_measurement_date: Option<NaiveDate>,
#[serde(default)]
pub alert_level: Option<AlertLevel>,
#[serde(default)]
pub alert_phase: Option<String>,
#[serde(default)]
pub wind_direction: Option<WindDirection>,
#[serde(default)]
pub summary_spanish: Option<String>,
#[serde(default)]
pub ashfall_reports: Vec<String>,
#[serde(default)]
pub image_urls: Vec<String>,
#[serde(default)]
pub video_urls: Vec<String>,
#[serde(default)]
pub source_url: Option<String>,
#[serde(default)]
pub ingested_at: Option<DateTime<Utc>>,
#[serde(default)]
pub partial: bool,
}
impl VolcanoReport {
pub fn is_full(&self) -> bool {
!self.partial
}
}
fn default_schema_version() -> u32 {
SCHEMA_VERSION
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FeedIndex {
#[serde(default = "default_schema_version")]
pub schema_version: u32,
pub updated_at: DateTime<Utc>,
pub earliest: NaiveDate,
pub latest: NaiveDate,
pub count: usize,
#[serde(default)]
pub dates: Vec<NaiveDate>,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "UPPERCASE")]
pub enum AlertLevel {
Green,
Yellow,
Orange,
Red,
}
impl AlertLevel {
pub fn from_spanish(text: &str) -> Option<Self> {
let text_lower = text.to_lowercase();
if text_lower.contains("verde") {
Some(AlertLevel::Green)
} else if text_lower.contains("amarillo") {
Some(AlertLevel::Yellow)
} else if text_lower.contains("naranja") {
Some(AlertLevel::Orange)
} else if text_lower.contains("rojo") {
Some(AlertLevel::Red)
} else {
None
}
}
pub fn emoji(self) -> &'static str {
match self {
AlertLevel::Green => "🟢",
AlertLevel::Yellow => "🟡",
AlertLevel::Orange => "🟠",
AlertLevel::Red => "🔴",
}
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "UPPERCASE")]
pub enum WindDirection {
N,
NNE,
NE,
ENE,
E,
ESE,
SE,
SSE,
S,
SSW,
SW,
WSW,
W,
WNW,
NW,
NNW,
}
impl WindDirection {
pub fn from_spanish(text: &str) -> Option<Self> {
let normalized = text.to_lowercase().replace([' ', '-', '_'], "");
const PATTERNS: &[(&str, WindDirection)] = &[
("nornoroeste", WindDirection::NNW),
("nortenoroeste", WindDirection::NNW),
("nornoreste", WindDirection::NNE),
("nortenoreste", WindDirection::NNE),
("oestenoroeste", WindDirection::WNW),
("oestesuroeste", WindDirection::WSW),
("estesureste", WindDirection::ESE),
("estenoreste", WindDirection::ENE),
("sursuroeste", WindDirection::SSW),
("sursureste", WindDirection::SSE),
("noroeste", WindDirection::NW),
("suroeste", WindDirection::SW),
("sureste", WindDirection::SE),
("noreste", WindDirection::NE),
("norte", WindDirection::N),
("oeste", WindDirection::W),
("este", WindDirection::E),
("sur", WindDirection::S),
];
PATTERNS
.iter()
.find(|(pattern, _)| normalized.contains(pattern))
.map(|(_, dir)| *dir)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_alert_level_from_spanish() {
assert_eq!(
AlertLevel::from_spanish("Amarillo Fase 2"),
Some(AlertLevel::Yellow)
);
assert_eq!(AlertLevel::from_spanish("VERDE"), Some(AlertLevel::Green));
assert_eq!(AlertLevel::from_spanish("rojo"), Some(AlertLevel::Red));
assert_eq!(
AlertLevel::from_spanish("NARANJA"),
Some(AlertLevel::Orange)
);
assert_eq!(AlertLevel::from_spanish("unknown"), None);
}
#[test]
fn test_wind_direction_from_spanish() {
assert_eq!(
WindDirection::from_spanish("norte-noroeste"),
Some(WindDirection::NNW)
);
assert_eq!(
WindDirection::from_spanish("NORESTE"),
Some(WindDirection::NE)
);
assert_eq!(WindDirection::from_spanish("sur"), Some(WindDirection::S));
assert_eq!(
WindDirection::from_spanish("oeste-suroeste"),
Some(WindDirection::WSW)
);
assert_eq!(
WindDirection::from_spanish("Dirección del viento SURESTE"),
Some(WindDirection::SE)
);
assert_eq!(WindDirection::from_spanish("OESTE"), Some(WindDirection::W));
}
#[test]
fn test_wind_direction_prefers_most_specific() {
assert_eq!(
WindDirection::from_spanish("oeste noroeste"),
Some(WindDirection::WNW)
);
assert_eq!(
WindDirection::from_spanish("sur suroeste"),
Some(WindDirection::SSW)
);
assert_eq!(
WindDirection::from_spanish("norte noreste"),
Some(WindDirection::NNE)
);
assert_eq!(
WindDirection::from_spanish("este sureste"),
Some(WindDirection::ESE)
);
}
#[test]
fn test_wind_direction_serializes_uppercase() {
let json = serde_json::to_string(&WindDirection::NNW).unwrap();
assert_eq!(json, "\"NNW\"");
let parsed: WindDirection = serde_json::from_str("\"W\"").unwrap();
assert_eq!(parsed, WindDirection::W);
}
#[test]
fn test_report_deserializes_from_feed_json() {
let json = r#"{
"schema_version": 1,
"date": "2026-08-04",
"exhalations": 160,
"volcanotectonic_events": 0,
"tremor_minutes_total": 0,
"tremor_high_frequency_minutes": null,
"tremor_harmonic_minutes": null,
"explosions": 0,
"so2_emissions_tons_per_day": null,
"so2_measurement_date": null,
"alert_level": "YELLOW",
"alert_phase": "AMARILLO FASE 2",
"wind_direction": "W",
"summary_spanish": "Se detectaron 160 exhalaciones.",
"ashfall_reports": ["Atlautla, Estado de México"],
"image_urls": [],
"video_urls": [],
"source_url": "https://www.cenapred.unam.mx/",
"ingested_at": "2026-08-05T17:04:00Z",
"a_field_from_the_future": 42
}"#;
let report: VolcanoReport = serde_json::from_str(json).unwrap();
assert_eq!(report.exhalations, Some(160));
assert_eq!(report.explosions, Some(0));
assert_eq!(report.alert_level, Some(AlertLevel::Yellow));
assert_eq!(report.wind_direction, Some(WindDirection::W));
assert_eq!(report.so2_emissions_tons_per_day, None);
assert_eq!(report.ashfall_reports, vec!["Atlautla, Estado de México"]);
assert!(report.is_full());
}
#[test]
fn test_partial_record_deserializes() {
let json = r#"{
"schema_version": 1,
"date": "2022-04-13",
"exhalations": 22,
"volcanotectonic_events": 0,
"tremor_minutes_total": 0,
"tremor_high_frequency_minutes": null,
"tremor_harmonic_minutes": null,
"explosions": 0,
"so2_emissions_tons_per_day": null,
"so2_measurement_date": null,
"alert_level": null,
"alert_phase": null,
"wind_direction": null,
"summary_spanish": null,
"ashfall_reports": [],
"image_urls": [],
"video_urls": [],
"source_url": "https://www.cenapred.unam.mx/",
"ingested_at": "2026-08-06T00:00:00Z",
"partial": true
}"#;
let report: VolcanoReport = serde_json::from_str(json).unwrap();
assert!(report.partial);
assert!(!report.is_full());
assert_eq!(report.exhalations, Some(22));
assert_eq!(report.alert_level, None);
assert_eq!(report.alert_phase, None);
assert_eq!(report.summary_spanish, None);
}
#[test]
fn test_absent_counter_is_none_not_zero() {
let json = r#"{
"date": "2001-01-05",
"alert_level": "YELLOW",
"alert_phase": "AMARILLO FASE 2",
"ingested_at": "2026-08-05T17:04:00Z"
}"#;
let report: VolcanoReport = serde_json::from_str(json).unwrap();
assert_eq!(report.exhalations, None);
assert_eq!(report.tremor_minutes_total, None);
assert!(report.image_urls.is_empty());
assert_eq!(report.schema_version, SCHEMA_VERSION);
assert!(report.is_full());
}
}