ecad_processor/models/
station.rs1use serde::{Deserialize, Serialize};
2use validator::Validate;
3
4#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
5pub struct StationMetadata {
6 pub staid: u32,
7
8 #[validate(length(min = 1))]
9 pub name: String,
10
11 pub country: String,
12
13 #[validate(range(min = -90.0, max = 90.0))]
14 pub latitude: f64,
15
16 #[validate(range(min = -180.0, max = 180.0))]
17 pub longitude: f64,
18
19 pub elevation: Option<i32>,
20}
21
22impl StationMetadata {
23 pub fn new(
24 staid: u32,
25 name: String,
26 country: String,
27 latitude: f64,
28 longitude: f64,
29 elevation: Option<i32>,
30 ) -> Self {
31 Self {
32 staid,
33 name,
34 country,
35 latitude,
36 longitude,
37 elevation,
38 }
39 }
40
41 pub fn is_uk_station(&self) -> bool {
42 self.country.to_uppercase() == "GB"
43 || self.country.to_uppercase() == "UK"
44 || self.country.to_uppercase() == "UNITED KINGDOM"
45 }
46
47 pub fn is_within_uk_bounds(&self) -> bool {
48 self.latitude >= 49.5
49 && self.latitude <= 61.0
50 && self.longitude >= -8.0
51 && self.longitude <= 2.0
52 }
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_station_validation() {
61 let station = StationMetadata::new(
62 12345,
63 "London Weather Station".to_string(),
64 "GB".to_string(),
65 51.5074,
66 -0.1278,
67 Some(35),
68 );
69
70 assert!(station.validate().is_ok());
71 assert!(station.is_uk_station());
72 assert!(station.is_within_uk_bounds());
73 }
74
75 #[test]
76 fn test_invalid_coordinates() {
77 let station = StationMetadata::new(
78 12345,
79 "Invalid Station".to_string(),
80 "GB".to_string(),
81 91.0, -0.1278,
83 None,
84 );
85
86 assert!(station.validate().is_err());
87 }
88}