weathervane 0.9.1

Weather data, air quality, and alerts from public APIs. Fetches, parses, and returns clean Rust types.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Location search, detection, and bookmarking.

use serde::{Deserialize, Serialize};

use crate::client::http_client;
use crate::error::{Error, Result};

/// Location search result from geocoding.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LocationResult {
    /// Decimal latitude.
    pub latitude: f64,
    /// Decimal longitude.
    pub longitude: f64,
    /// Human-readable name (e.g. "Portland, Oregon, United States").
    pub display_name: String,
    /// Country name as returned by the geocoding API.
    pub country: String,
}

impl LocationResult {
    fn from_geocoding_result(result: &GeocodingResult) -> Self {
        let country = result.country.clone().unwrap_or_default();
        let display_name = match (&result.admin1, &result.country) {
            (Some(admin), Some(c)) => format!("{}, {}, {}", result.name, admin, c),
            (None, Some(c)) => format!("{}, {}", result.name, c),
            _ => result.name.clone(),
        };

        Self {
            latitude: result.latitude,
            longitude: result.longitude,
            display_name,
            country,
        }
    }
}

/// A bookmarked location for quick switching.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SavedLocation {
    /// User-facing label for this bookmark.
    pub name: String,
    /// Decimal latitude.
    pub latitude: f64,
    /// Decimal longitude.
    pub longitude: f64,
}

impl SavedLocation {
    /// Checks if this saved location matches the given coordinates.
    pub fn matches_coords(&self, lat: f64, lon: f64) -> bool {
        (self.latitude - lat).abs() < 0.01 && (self.longitude - lon).abs() < 0.01
    }
}

/// Result of automatic IP-based location detection.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DetectedLocation {
    /// Decimal latitude from IP geolocation.
    pub latitude: f64,
    /// Decimal longitude from IP geolocation.
    pub longitude: f64,
    /// Best-effort city/country name from the IP lookup.
    pub display_name: String,
    /// Country name, used to determine default units.
    pub country: String,
}

/// Searches for a location by city name using Open-Meteo Geocoding API.
pub async fn search_city(city_name: &str) -> Result<Vec<LocationResult>> {
    let url = format!(
        "https://geocoding-api.open-meteo.com/v1/search?name={}&count=10&language=en&format=json",
        urlencoding::encode(city_name)
    );

    let response = http_client()?.get(&url).send().await?.error_for_status()?;
    let data: GeocodingResponse = response.json().await?;

    if let Some(results) = data.results {
        if !results.is_empty() {
            let locations: Vec<LocationResult> = results
                .iter()
                .map(LocationResult::from_geocoding_result)
                .collect();

            tracing::debug!("Found {} location(s)", locations.len());
            return Ok(locations);
        }
    }

    Err(Error::NoResults {
        query: city_name.to_string(),
    })
}

/// Detects user location automatically using IP-based geolocation.
pub async fn detect_location() -> Result<DetectedLocation> {
    let url = "http://ip-api.com/json/?fields=status,lat,lon,city,regionName,country";

    let response = http_client()?.get(url).send().await?.error_for_status()?;
    let data: IpApiResponse = response.json().await?;

    detected_from_ip_api(data)
}

/// Lives in its own function so the IP-API success branch, coord-range guard, and
/// display_name arm selection can be unit-tested against fixtures without a live network.
fn detected_from_ip_api(data: IpApiResponse) -> Result<DetectedLocation> {
    if data.status == "success" {
        if let (Some(lat), Some(lon)) = (data.lat, data.lon) {
            // range-contains rejects NaN and infinity by IEEE 754 ordering
            if !(-90.0..=90.0).contains(&lat) || !(-180.0..=180.0).contains(&lon) {
                tracing::debug!("detect_location: coordinates out of valid range");
                return Err(Error::LocationDetection);
            }

            let country = data.country.clone().unwrap_or_default();
            let display_name = match (data.city, data.region_name, data.country) {
                (Some(city), _, Some(c)) => format!("{}, {}", city, c),
                (_, Some(region), Some(c)) => format!("{}, {}", region, c),
                (_, _, Some(c)) => c,
                _ => "Unknown".to_string(),
            };

            tracing::debug!("Auto-detected location");
            return Ok(DetectedLocation {
                latitude: lat,
                longitude: lon,
                display_name,
                country,
            });
        }
    }

    Err(Error::LocationDetection)
}

/// Returns true if the country uses imperial units (Fahrenheit, mph, miles).
/// Only US, Liberia, and Myanmar officially use imperial.
pub fn uses_imperial_units(country: &str) -> bool {
    matches!(country, "United States" | "Liberia" | "Myanmar")
}

/// Open-Meteo Geocoding API response.
#[derive(Debug, Deserialize)]
struct GeocodingResponse {
    results: Option<Vec<GeocodingResult>>,
}

#[derive(Debug, Deserialize)]
struct GeocodingResult {
    name: String,
    latitude: f64,
    longitude: f64,
    country: Option<String>,
    admin1: Option<String>,
}

/// IP-API.com response for geolocation.
#[derive(Debug, Deserialize)]
struct IpApiResponse {
    status: String,
    lat: Option<f64>,
    lon: Option<f64>,
    city: Option<String>,
    #[serde(rename = "regionName")]
    region_name: Option<String>,
    country: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn location_result_from_geocoding_with_admin_and_country() {
        let result = GeocodingResult {
            name: "Portland".to_string(),
            latitude: 45.5152,
            longitude: -122.6784,
            country: Some("United States".to_string()),
            admin1: Some("Oregon".to_string()),
        };

        let loc = LocationResult::from_geocoding_result(&result);

        assert_eq!(loc.display_name, "Portland, Oregon, United States");
        assert_eq!(loc.country, "United States");
        assert_eq!(loc.latitude, 45.5152);
        assert_eq!(loc.longitude, -122.6784);
    }

    #[test]
    fn location_result_from_geocoding_without_admin_uses_two_part_display() {
        let result = GeocodingResult {
            name: "Portland".to_string(),
            latitude: 45.5152,
            longitude: -122.6784,
            country: Some("United States".to_string()),
            admin1: None,
        };

        let loc = LocationResult::from_geocoding_result(&result);

        assert_eq!(loc.display_name, "Portland, United States");
    }

    #[test]
    fn location_result_from_geocoding_without_country_uses_name_only() {
        let result = GeocodingResult {
            name: "Portland".to_string(),
            latitude: 45.5152,
            longitude: -122.6784,
            country: None,
            admin1: Some("Oregon".to_string()),
        };

        let loc = LocationResult::from_geocoding_result(&result);

        assert_eq!(loc.display_name, "Portland");
        assert_eq!(loc.country, "");
    }

    #[test]
    fn location_result_from_geocoding_without_admin_or_country_uses_name_only() {
        let result = GeocodingResult {
            name: "Portland".to_string(),
            latitude: 45.5152,
            longitude: -122.6784,
            country: None,
            admin1: None,
        };

        let loc = LocationResult::from_geocoding_result(&result);

        assert_eq!(loc.display_name, "Portland");
        assert_eq!(loc.country, "");
    }

    #[test]
    fn detected_from_ip_api_success_with_city_and_country() {
        let data: IpApiResponse = serde_json::from_str(
            r#"{"status": "success", "lat": 45.5152, "lon": -122.6784, "city": "Portland", "regionName": "Oregon", "country": "United States"}"#,
        )
        .unwrap();

        let loc = detected_from_ip_api(data).unwrap();

        assert_eq!(loc.display_name, "Portland, United States");
        assert_eq!(loc.country, "United States");
        assert_eq!(loc.latitude, 45.5152);
        assert_eq!(loc.longitude, -122.6784);
    }

    #[test]
    fn detected_from_ip_api_success_without_city_uses_region_and_country() {
        let data: IpApiResponse = serde_json::from_str(
            r#"{"status": "success", "lat": 45.5152, "lon": -122.6784, "city": null, "regionName": "Oregon", "country": "United States"}"#,
        )
        .unwrap();

        let loc = detected_from_ip_api(data).unwrap();

        assert_eq!(loc.display_name, "Oregon, United States");
    }

    #[test]
    fn detected_from_ip_api_success_with_only_country_uses_country_alone() {
        let data: IpApiResponse = serde_json::from_str(
            r#"{"status": "success", "lat": 45.5152, "lon": -122.6784, "city": null, "regionName": null, "country": "United States"}"#,
        )
        .unwrap();

        let loc = detected_from_ip_api(data).unwrap();

        assert_eq!(loc.display_name, "United States");
    }

    #[test]
    fn detected_from_ip_api_success_with_no_names_falls_back_to_unknown() {
        let data: IpApiResponse = serde_json::from_str(
            r#"{"status": "success", "lat": 45.5, "lon": -122.5, "city": null, "regionName": null, "country": null}"#,
        )
        .unwrap();

        let loc = detected_from_ip_api(data).unwrap();

        assert_eq!(loc.display_name, "Unknown");
        assert_eq!(loc.country, "");
    }

    #[test]
    fn detected_from_ip_api_returns_location_detection_error_when_status_fail() {
        let data: IpApiResponse = serde_json::from_str(
            r#"{"status": "fail", "lat": 45.5, "lon": -122.5, "city": null, "regionName": null, "country": null}"#,
        )
        .unwrap();

        let result = detected_from_ip_api(data);

        assert!(matches!(result, Err(Error::LocationDetection)));
    }

    #[test]
    fn detected_from_ip_api_returns_location_detection_error_when_lat_missing() {
        let data: IpApiResponse = serde_json::from_str(
            r#"{"status": "success", "lat": null, "lon": -122.5, "city": null, "regionName": null, "country": null}"#,
        )
        .unwrap();

        let result = detected_from_ip_api(data);

        assert!(matches!(result, Err(Error::LocationDetection)));
    }

    #[test]
    fn detected_from_ip_api_returns_location_detection_error_when_lat_out_of_range() {
        let data: IpApiResponse = serde_json::from_str(
            r#"{"status": "success", "lat": 91.0, "lon": 0.0, "city": null, "regionName": null, "country": null}"#,
        )
        .unwrap();

        let result = detected_from_ip_api(data);

        assert!(matches!(result, Err(Error::LocationDetection)));
    }

    #[test]
    fn saved_location_matches_within_window() {
        let s = SavedLocation {
            name: "Home".to_string(),
            latitude: 45.5152,
            longitude: -122.6784,
        };

        assert!(s.matches_coords(45.5152, -122.6784));
        assert!(s.matches_coords(45.5200, -122.6800));
    }

    #[test]
    fn saved_location_does_not_match_outside_window_lat() {
        let s = SavedLocation {
            name: "Home".to_string(),
            latitude: 45.5152,
            longitude: -122.6784,
        };

        assert!(!s.matches_coords(45.53, -122.6784));
    }

    #[test]
    fn saved_location_does_not_match_outside_window_lon() {
        let s = SavedLocation {
            name: "Home".to_string(),
            latitude: 45.5152,
            longitude: -122.6784,
        };

        assert!(!s.matches_coords(45.5152, -122.66));
    }

    #[test]
    fn uses_imperial_units_returns_true_for_imperial_countries() {
        assert!(uses_imperial_units("United States"));
        assert!(uses_imperial_units("Liberia"));
        assert!(uses_imperial_units("Myanmar"));
    }

    #[test]
    fn uses_imperial_units_returns_false_for_metric_countries() {
        assert!(!uses_imperial_units("United Kingdom"));
        assert!(!uses_imperial_units("Canada"));
        assert!(!uses_imperial_units("Germany"));
        assert!(!uses_imperial_units("Japan"));
    }

    #[test]
    fn uses_imperial_units_returns_false_for_empty_string() {
        assert!(!uses_imperial_units(""));
    }

    /// Pins the IEEE 754 semantics detect_location() depends on (D-05, D-06).
    ///
    /// `RangeInclusive::contains` on `f64` uses `PartialOrd`, which returns `false`
    /// for any comparison involving NaN. A future refactor to `lat <= 90.0 && lat >= -90.0`
    /// would silently re-admit NaN (that form is true-for-NaN). These three tests lock
    /// the predicate form so any such regression fails CI.
    #[test]
    fn nan_coords_are_rejected() {
        // Pins the IEEE 754 semantics detect_location() depends on (D-05, D-06).
        assert!(
            !(-90.0_f64..=90.0_f64).contains(&f64::NAN),
            "range-contains must reject NaN lat"
        );
        assert!(
            !(-180.0_f64..=180.0_f64).contains(&f64::NAN),
            "range-contains must reject NaN lon"
        );
    }

    #[test]
    fn infinite_coords_are_rejected() {
        // Pins the IEEE 754 semantics detect_location() depends on (D-05, D-06).
        assert!(
            !(-90.0_f64..=90.0_f64).contains(&f64::INFINITY),
            "range-contains must reject +inf lat"
        );
        assert!(
            !(-90.0_f64..=90.0_f64).contains(&f64::NEG_INFINITY),
            "range-contains must reject -inf lat"
        );
        assert!(
            !(-180.0_f64..=180.0_f64).contains(&f64::INFINITY),
            "range-contains must reject +inf lon"
        );
        assert!(
            !(-180.0_f64..=180.0_f64).contains(&f64::NEG_INFINITY),
            "range-contains must reject -inf lon"
        );
    }

    #[test]
    fn out_of_range_coords_are_rejected() {
        // Out-of-range values are rejected.
        assert!(
            !(-90.0_f64..=90.0_f64).contains(&91.0_f64),
            "lat 91 must be rejected"
        );
        assert!(
            !(-180.0_f64..=180.0_f64).contains(&181.0_f64),
            "lon 181 must be rejected"
        );
        // Sanity anchor: valid coords are accepted (guard is not over-rejecting).
        assert!(
            (-90.0_f64..=90.0_f64).contains(&45.5_f64),
            "lat 45.5 must be accepted"
        );
        assert!(
            (-180.0_f64..=180.0_f64).contains(&-122.6_f64),
            "lon -122.6 must be accepted"
        );
    }
}