weatherkit-doomfish 0.3.2

Safe Rust bindings for Apple's WeatherKit framework — weather data on macOS
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
//! WeatherKit service types and query helpers.

use core::ffi::{c_char, c_void};
use std::time::{SystemTime, UNIX_EPOCH};

use serde::Deserialize;

use crate::availability_kind::WeatherAvailability;
use crate::changes::{HistoricalComparisons, WeatherChanges};
use crate::current_weather::CurrentWeather;
use crate::daily_forecast::{DailyForecast, DayForecast};
use crate::error::WeatherKitError;
use crate::ffi;
use crate::hourly_forecast::HourlyForecast;
use crate::minute_forecast::MinuteForecastCollection;
use crate::moon_events::MoonEvents;
use crate::pressure::Pressure;
use crate::private::{error_from_status, parse_json_from_handle};
use crate::statistics::{
    DailyWeatherStatistics, DailyWeatherStatisticsQuery, DailyWeatherStatisticsResult,
    DailyWeatherSummary, DailyWeatherSummaryQuery, DailyWeatherSummaryResult,
    DayPrecipitationStatistics, DayPrecipitationSummary, DayTemperatureStatistics,
    DayTemperatureSummary, HourTemperatureStatistics, HourlyWeatherStatistics,
    HourlyWeatherStatisticsQuery, MonthPrecipitationStatistics, MonthTemperatureStatistics,
    MonthlyWeatherStatistics, MonthlyWeatherStatisticsQuery, MonthlyWeatherStatisticsResult,
};
use crate::sun_events::SunEvents;
use crate::weather_alert::{alerts_from_owned_ptr, WeatherAlert};
use crate::weather_attribution::WeatherAttribution;

/// Represents a WeatherKit location coordinate.
#[derive(Debug, Clone, Copy, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CLLocation {
    /// Matches the WeatherKit latitude value.
    pub latitude: f64,
    /// Matches the WeatherKit longitude value.
    pub longitude: f64,
}

impl CLLocation {
    /// Creates a WeatherKit location value from latitude and longitude.
    pub const fn new(latitude: f64, longitude: f64) -> Self {
        Self {
            latitude,
            longitude,
        }
    }

    fn validate(&self) -> Result<(), WeatherKitError> {
        if !self.latitude.is_finite() || !self.longitude.is_finite() {
            return Err(WeatherKitError::bridge(
                -1,
                "latitude and longitude must be finite numbers",
            ));
        }
        if !(-90.0..=90.0).contains(&self.latitude) {
            return Err(WeatherKitError::bridge(
                -1,
                format!("latitude {} is outside -90..=90", self.latitude),
            ));
        }
        if !(-180.0..=180.0).contains(&self.longitude) {
            return Err(WeatherKitError::bridge(
                -1,
                format!("longitude {} is outside -180..=180", self.longitude),
            ));
        }
        Ok(())
    }
}

/// Represents a WeatherKit date interval.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DateInterval {
    /// Matches the WeatherKit start value.
    pub start: SystemTime,
    /// Matches the WeatherKit end value.
    pub end: SystemTime,
}

impl DateInterval {
    /// Creates a WeatherKit date interval after validating its bounds.
    pub fn new(start: SystemTime, end: SystemTime) -> Result<Self, WeatherKitError> {
        if start > end {
            return Err(WeatherKitError::bridge(
                -1,
                "date interval start must not be after end",
            ));
        }
        Ok(Self { start, end })
    }

    fn start_seconds(&self) -> Result<f64, WeatherKitError> {
        unix_seconds(self.start)
    }

    fn end_seconds(&self) -> Result<f64, WeatherKitError> {
        unix_seconds(self.end)
    }
}

/// Represents WeatherKit response metadata.
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WeatherMetadata {
    /// Matches the WeatherKit date value.
    pub date: String,
    /// Matches the WeatherKit expiration date value.
    pub expiration_date: String,
    /// Matches the WeatherKit location value.
    pub location: CLLocation,
}

/// Represents the aggregate WeatherKit weather payload.
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Weather {
    /// Matches the WeatherKit current weather value.
    pub current_weather: CurrentWeather,
    /// Matches the WeatherKit hourly forecast value.
    pub hourly_forecast: Vec<crate::hourly_forecast::HourForecast>,
    /// Matches the WeatherKit daily forecast value.
    pub daily_forecast: Vec<DayForecast>,
    /// Matches the WeatherKit minute forecast value.
    pub minute_forecast: Option<Vec<crate::minute_forecast::MinuteForecast>>,
    /// Matches the WeatherKit weather alerts value.
    #[serde(default)]
    pub weather_alerts: Vec<WeatherAlert>,
    /// Matches the WeatherKit availability value.
    pub availability: WeatherAvailability,
}

/// Describes a WeatherKit query to fetch.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WeatherQuery {
    /// Requests the WeatherKit current weather payload.
    Current,
    /// Requests the WeatherKit minute forecast payload.
    Minute,
    /// Requests the WeatherKit hourly forecast payload.
    Hourly,
    /// Requests the WeatherKit hourly forecast for the supplied interval.
    HourlyIn(DateInterval),
    /// Requests the WeatherKit daily forecast payload.
    Daily,
    /// Requests the WeatherKit daily forecast for the supplied interval.
    DailyIn(DateInterval),
    /// Requests the WeatherKit weather alerts payload.
    Alerts,
    /// Requests the WeatherKit availability payload.
    Availability,
    /// Requests the WeatherKit weather changes payload.
    Changes,
    /// Requests the WeatherKit historical comparisons payload.
    HistoricalComparisons,
}

/// Wraps a WeatherKit query result.
#[derive(Debug, Clone, PartialEq)]
pub enum WeatherQueryResult {
    /// Carries the WeatherKit current weather payload.
    CurrentWeather(Box<CurrentWeather>),
    /// Carries the optional WeatherKit minute forecast payload.
    MinuteForecast(Option<Box<MinuteForecastCollection>>),
    /// Carries the WeatherKit hourly forecast payload.
    HourlyForecast(Box<HourlyForecast>),
    /// Carries the WeatherKit daily forecast payload.
    DailyForecast(Box<DailyForecast>),
    /// Carries the WeatherKit weather alerts payload.
    WeatherAlerts(Vec<WeatherAlert>),
    /// Carries the WeatherKit availability payload.
    Availability(Box<WeatherAvailability>),
    /// Carries the optional WeatherKit weather changes payload.
    WeatherChanges(Option<Box<WeatherChanges>>),
    /// Carries the optional WeatherKit historical comparisons payload.
    HistoricalComparisons(Option<Box<HistoricalComparisons>>),
}

impl WeatherQuery {
    fn fetch(
        &self,
        service: WeatherService,
        location: &CLLocation,
    ) -> Result<WeatherQueryResult, WeatherKitError> {
        match self {
            Self::Current => service
                .current_weather(location)
                .map(Box::new)
                .map(WeatherQueryResult::CurrentWeather),
            Self::Minute => service
                .minute_forecast(location)
                .map(|forecast| forecast.map(Box::new))
                .map(WeatherQueryResult::MinuteForecast),
            Self::Hourly => service
                .hourly_forecast(location)
                .map(Box::new)
                .map(WeatherQueryResult::HourlyForecast),
            Self::HourlyIn(interval) => service
                .hourly_forecast_in(location, interval.clone())
                .map(Box::new)
                .map(WeatherQueryResult::HourlyForecast),
            Self::Daily => service
                .daily_forecast(location)
                .map(Box::new)
                .map(WeatherQueryResult::DailyForecast),
            Self::DailyIn(interval) => service
                .daily_forecast_in(location, interval.clone())
                .map(Box::new)
                .map(WeatherQueryResult::DailyForecast),
            Self::Alerts => service
                .weather_alerts(location)
                .map(WeatherQueryResult::WeatherAlerts),
            Self::Availability => service
                .availability(location)
                .map(Box::new)
                .map(WeatherQueryResult::Availability),
            Self::Changes => service
                .weather_changes(location)
                .map(|changes| changes.map(Box::new))
                .map(WeatherQueryResult::WeatherChanges),
            Self::HistoricalComparisons => service
                .historical_comparisons(location)
                .map(|comparisons| comparisons.map(Box::new))
                .map(WeatherQueryResult::HistoricalComparisons),
        }
    }
}

/// Wraps a WeatherKit service handle.
#[derive(Debug, Clone, Copy, Default)]
pub struct WeatherService {
    kind: ServiceKind,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
enum ServiceKind {
    #[default]
    Shared,
    Owned,
}

struct ServiceHandle {
    ptr: *mut c_void,
}

type LocationFetchFn =
    unsafe extern "C" fn(*mut c_void, f64, f64, *mut *mut c_void, *mut *mut c_char) -> i32;
type IntervalFetchFn = unsafe extern "C" fn(
    *mut c_void,
    f64,
    f64,
    i32,
    f64,
    f64,
    *mut *mut c_void,
    *mut *mut c_char,
) -> i32;
type ScopedQueryFetchFn = unsafe extern "C" fn(
    *mut c_void,
    f64,
    f64,
    i32,
    i32,
    f64,
    f64,
    i64,
    i64,
    *mut *mut c_void,
    *mut *mut c_char,
) -> i32;
type ServiceFetchFn = unsafe extern "C" fn(*mut c_void, *mut *mut c_void, *mut *mut c_char) -> i32;

#[derive(Debug, Clone, Copy)]
enum QueryScope<'a> {
    None,
    Interval(&'a DateInterval),
    Index { start: i64, end: i64 },
}

impl ServiceHandle {
    fn acquire(kind: ServiceKind) -> Result<Self, WeatherKitError> {
        // SAFETY: both FFI functions return a freshly retained opaque pointer
        // (or null on failure); the null-check immediately below guards use.
        let ptr = unsafe {
            match kind {
                ServiceKind::Shared => ffi::service::wk_weather_service_shared(),
                ServiceKind::Owned => ffi::service::wk_weather_service_new(),
            }
        };
        if ptr.is_null() {
            Err(WeatherKitError::bridge(
                -1,
                "failed to acquire WeatherService handle",
            ))
        } else {
            Ok(Self { ptr })
        }
    }

    fn as_ptr(&self) -> *mut c_void {
        self.ptr
    }
}

impl Drop for ServiceHandle {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            // SAFETY: ptr is non-null, was validated in `acquire`, and is set
            // to null immediately after so it cannot be double-released.
            unsafe {
                ffi::service::wk_weather_service_release(self.ptr);
            }
            self.ptr = core::ptr::null_mut();
        }
    }
}

impl WeatherService {
    /// Returns a WeatherKit service that reuses the shared native service.
    pub const fn shared() -> Self {
        Self {
            kind: ServiceKind::Shared,
        }
    }

    /// Returns a WeatherKit service with an owned native service handle.
    pub const fn new() -> Self {
        Self {
            kind: ServiceKind::Owned,
        }
    }

    pub(crate) const fn is_owned(self) -> bool {
        matches!(self.kind, ServiceKind::Owned)
    }

    /// Fetches WeatherKit attribution information.
    pub fn attribution(&self) -> Result<WeatherAttribution, WeatherKitError> {
        let ptr = self.fetch_service_handle(
            ffi::service::wk_weather_service_attribution,
            "WeatherService.attribution",
        )?;
        WeatherAttribution::from_owned_ptr(ptr)
    }

    /// Fetches the aggregate WeatherKit weather payload.
    pub fn weather(&self, location: &CLLocation) -> Result<Weather, WeatherKitError> {
        let ptr = self.fetch_location_handle(
            location,
            ffi::service::wk_weather_service_weather,
            "WeatherService.weather(for:)",
        )?;
        parse_json_from_handle(
            ptr,
            ffi::service::wk_weather_release,
            ffi::service::wk_weather_copy_json,
            "weather",
        )
    }

    /// Fetches the WeatherKit current weather payload.
    pub fn current_weather(
        &self,
        location: &CLLocation,
    ) -> Result<CurrentWeather, WeatherKitError> {
        let ptr = self.fetch_location_handle(
            location,
            ffi::service::wk_weather_service_current_weather,
            "WeatherService.weather(for: including: .current)",
        )?;
        CurrentWeather::from_owned_ptr(ptr)
    }

    /// Fetches the WeatherKit hourly forecast.
    pub fn hourly_forecast(
        &self,
        location: &CLLocation,
    ) -> Result<HourlyForecast, WeatherKitError> {
        let ptr = self.fetch_interval_handle(
            location,
            None,
            ffi::service::wk_weather_service_hourly_forecast,
            "WeatherService.weather(for: including: .hourly)",
        )?;
        HourlyForecast::from_owned_ptr(ptr)
    }

    /// Fetches the WeatherKit hourly forecast for the given interval.
    pub fn hourly_forecast_in(
        &self,
        location: &CLLocation,
        interval: DateInterval,
    ) -> Result<HourlyForecast, WeatherKitError> {
        let ptr = self.fetch_interval_handle(
            location,
            Some(&interval),
            ffi::service::wk_weather_service_hourly_forecast,
            "WeatherService.weather(for: including: .hourly(startDate:endDate))",
        )?;
        HourlyForecast::from_owned_ptr(ptr)
    }

    /// Fetches the WeatherKit daily forecast.
    pub fn daily_forecast(&self, location: &CLLocation) -> Result<DailyForecast, WeatherKitError> {
        let ptr = self.fetch_interval_handle(
            location,
            None,
            ffi::service::wk_weather_service_daily_forecast,
            "WeatherService.weather(for: including: .daily)",
        )?;
        DailyForecast::from_owned_ptr(ptr)
    }

    /// Fetches the WeatherKit daily forecast for the given interval.
    pub fn daily_forecast_in(
        &self,
        location: &CLLocation,
        interval: DateInterval,
    ) -> Result<DailyForecast, WeatherKitError> {
        let ptr = self.fetch_interval_handle(
            location,
            Some(&interval),
            ffi::service::wk_weather_service_daily_forecast,
            "WeatherService.weather(for: including: .daily(startDate:endDate))",
        )?;
        DailyForecast::from_owned_ptr(ptr)
    }

    /// Fetches the optional WeatherKit minute forecast.
    pub fn minute_forecast(
        &self,
        location: &CLLocation,
    ) -> Result<Option<MinuteForecastCollection>, WeatherKitError> {
        let ptr = self.fetch_location_handle(
            location,
            ffi::service::wk_weather_service_minute_forecast,
            "WeatherService.weather(for: including: .minute)",
        )?;
        MinuteForecastCollection::option_from_owned_ptr(ptr)
    }

    /// Fetches WeatherKit weather alerts.
    pub fn weather_alerts(
        &self,
        location: &CLLocation,
    ) -> Result<Vec<WeatherAlert>, WeatherKitError> {
        let ptr = self.fetch_location_handle(
            location,
            ffi::service::wk_weather_service_weather_alerts,
            "WeatherService.weather(for: including: .alerts)",
        )?;
        alerts_from_owned_ptr(ptr)
    }

    /// Fetches WeatherKit data availability for the location.
    pub fn availability(
        &self,
        location: &CLLocation,
    ) -> Result<WeatherAvailability, WeatherKitError> {
        let ptr = self.fetch_location_handle(
            location,
            ffi::service::wk_weather_service_availability,
            "WeatherService.weather(for: including: .availability)",
        )?;
        WeatherAvailability::from_owned_ptr(ptr)
    }

    /// Fetches one WeatherKit query result.
    pub fn weather_including(
        &self,
        location: &CLLocation,
        query: WeatherQuery,
    ) -> Result<WeatherQueryResult, WeatherKitError> {
        query.fetch(*self, location)
    }

    /// Fetches 2 WeatherKit query results in order.
    pub fn weather_including2(
        &self,
        location: &CLLocation,
        query1: WeatherQuery,
        query2: WeatherQuery,
    ) -> Result<(WeatherQueryResult, WeatherQueryResult), WeatherKitError> {
        Ok((
            query1.fetch(*self, location)?,
            query2.fetch(*self, location)?,
        ))
    }

    /// Fetches 3 WeatherKit query results in order.
    pub fn weather_including3(
        &self,
        location: &CLLocation,
        query1: WeatherQuery,
        query2: WeatherQuery,
        query3: WeatherQuery,
    ) -> Result<(WeatherQueryResult, WeatherQueryResult, WeatherQueryResult), WeatherKitError> {
        Ok((
            query1.fetch(*self, location)?,
            query2.fetch(*self, location)?,
            query3.fetch(*self, location)?,
        ))
    }

    /// Fetches 4 WeatherKit query results in order.
    pub fn weather_including4(
        &self,
        location: &CLLocation,
        query1: WeatherQuery,
        query2: WeatherQuery,
        query3: WeatherQuery,
        query4: WeatherQuery,
    ) -> Result<
        (
            WeatherQueryResult,
            WeatherQueryResult,
            WeatherQueryResult,
            WeatherQueryResult,
        ),
        WeatherKitError,
    > {
        Ok((
            query1.fetch(*self, location)?,
            query2.fetch(*self, location)?,
            query3.fetch(*self, location)?,
            query4.fetch(*self, location)?,
        ))
    }

    /// Fetches 5 WeatherKit query results in order.
    pub fn weather_including5(
        &self,
        location: &CLLocation,
        query1: WeatherQuery,
        query2: WeatherQuery,
        query3: WeatherQuery,
        query4: WeatherQuery,
        query5: WeatherQuery,
    ) -> Result<
        (
            WeatherQueryResult,
            WeatherQueryResult,
            WeatherQueryResult,
            WeatherQueryResult,
            WeatherQueryResult,
        ),
        WeatherKitError,
    > {
        Ok((
            query1.fetch(*self, location)?,
            query2.fetch(*self, location)?,
            query3.fetch(*self, location)?,
            query4.fetch(*self, location)?,
            query5.fetch(*self, location)?,
        ))
    }

    /// Fetches 6 WeatherKit query results in order.
    #[allow(clippy::too_many_arguments)]
    pub fn weather_including6(
        &self,
        location: &CLLocation,
        query1: WeatherQuery,
        query2: WeatherQuery,
        query3: WeatherQuery,
        query4: WeatherQuery,
        query5: WeatherQuery,
        query6: WeatherQuery,
    ) -> Result<
        (
            WeatherQueryResult,
            WeatherQueryResult,
            WeatherQueryResult,
            WeatherQueryResult,
            WeatherQueryResult,
            WeatherQueryResult,
        ),
        WeatherKitError,
    > {
        Ok((
            query1.fetch(*self, location)?,
            query2.fetch(*self, location)?,
            query3.fetch(*self, location)?,
            query4.fetch(*self, location)?,
            query5.fetch(*self, location)?,
            query6.fetch(*self, location)?,
        ))
    }

    /// Fetches an arbitrary list of WeatherKit query results in order.
    pub fn weather_including_many<I>(
        &self,
        location: &CLLocation,
        queries: I,
    ) -> Result<Vec<WeatherQueryResult>, WeatherKitError>
    where
        I: IntoIterator<Item = WeatherQuery>,
    {
        queries
            .into_iter()
            .map(|query| query.fetch(*self, location))
            .collect()
    }

    /// Fetches the optional WeatherKit weather changes payload.
    pub fn weather_changes(
        &self,
        location: &CLLocation,
    ) -> Result<Option<WeatherChanges>, WeatherKitError> {
        let ptr = self.fetch_location_handle(
            location,
            ffi::changes::wk_weather_service_weather_changes,
            "WeatherService.weather(for: including: .changes)",
        )?;
        WeatherChanges::option_from_owned_ptr(ptr)
    }

    /// Fetches the optional WeatherKit historical comparisons payload.
    pub fn historical_comparisons(
        &self,
        location: &CLLocation,
    ) -> Result<Option<HistoricalComparisons>, WeatherKitError> {
        let ptr = self.fetch_location_handle(
            location,
            ffi::changes::wk_weather_service_historical_comparisons,
            "WeatherService.weather(for: including: .historicalComparisons)",
        )?;
        HistoricalComparisons::option_from_owned_ptr(ptr)
    }

    /// Fetches WeatherKit daily statistics for the requested query.
    pub fn daily_statistics(
        &self,
        location: &CLLocation,
        query: DailyWeatherStatisticsQuery,
    ) -> Result<DailyWeatherStatisticsResult, WeatherKitError> {
        self.daily_statistics_with_scope(location, query, QueryScope::None)
    }

    /// Fetches WeatherKit daily statistics for the given interval.
    pub fn daily_statistics_in(
        &self,
        location: &CLLocation,
        interval: DateInterval,
        query: DailyWeatherStatisticsQuery,
    ) -> Result<DailyWeatherStatisticsResult, WeatherKitError> {
        self.daily_statistics_with_scope(location, query, QueryScope::Interval(&interval))
    }

    /// Fetches WeatherKit daily statistics for the given day range.
    pub fn daily_statistics_between_days(
        &self,
        location: &CLLocation,
        start_day: i64,
        end_day: i64,
        query: DailyWeatherStatisticsQuery,
    ) -> Result<DailyWeatherStatisticsResult, WeatherKitError> {
        self.daily_statistics_with_scope(
            location,
            query,
            QueryScope::Index {
                start: start_day,
                end: end_day,
            },
        )
    }

    /// Fetches the WeatherKit daily summary for the requested query.
    pub fn daily_summary(
        &self,
        location: &CLLocation,
        query: DailyWeatherSummaryQuery,
    ) -> Result<DailyWeatherSummaryResult, WeatherKitError> {
        self.daily_summary_with_scope(location, query, QueryScope::None)
    }

    /// Fetches the WeatherKit daily summary for the given interval.
    pub fn daily_summary_in(
        &self,
        location: &CLLocation,
        interval: DateInterval,
        query: DailyWeatherSummaryQuery,
    ) -> Result<DailyWeatherSummaryResult, WeatherKitError> {
        self.daily_summary_with_scope(location, query, QueryScope::Interval(&interval))
    }

    /// Fetches WeatherKit hourly statistics for the requested query.
    pub fn hourly_statistics(
        &self,
        location: &CLLocation,
        query: HourlyWeatherStatisticsQuery,
    ) -> Result<HourlyWeatherStatistics<HourTemperatureStatistics>, WeatherKitError> {
        self.hourly_statistics_with_scope(location, query, QueryScope::None)
    }

    /// Fetches WeatherKit hourly statistics for the given interval.
    pub fn hourly_statistics_in(
        &self,
        location: &CLLocation,
        interval: DateInterval,
        query: HourlyWeatherStatisticsQuery,
    ) -> Result<HourlyWeatherStatistics<HourTemperatureStatistics>, WeatherKitError> {
        self.hourly_statistics_with_scope(location, query, QueryScope::Interval(&interval))
    }

    /// Fetches WeatherKit hourly statistics for the given hour range.
    pub fn hourly_statistics_between_hours(
        &self,
        location: &CLLocation,
        start_hour: i64,
        end_hour: i64,
        query: HourlyWeatherStatisticsQuery,
    ) -> Result<HourlyWeatherStatistics<HourTemperatureStatistics>, WeatherKitError> {
        self.hourly_statistics_with_scope(
            location,
            query,
            QueryScope::Index {
                start: start_hour,
                end: end_hour,
            },
        )
    }

    /// Fetches WeatherKit monthly statistics for the requested query.
    pub fn monthly_statistics(
        &self,
        location: &CLLocation,
        query: MonthlyWeatherStatisticsQuery,
    ) -> Result<MonthlyWeatherStatisticsResult, WeatherKitError> {
        self.monthly_statistics_with_scope(location, query, QueryScope::None)
    }

    /// Fetches WeatherKit monthly statistics for the given interval.
    pub fn monthly_statistics_in(
        &self,
        location: &CLLocation,
        interval: DateInterval,
        query: MonthlyWeatherStatisticsQuery,
    ) -> Result<MonthlyWeatherStatisticsResult, WeatherKitError> {
        self.monthly_statistics_with_scope(location, query, QueryScope::Interval(&interval))
    }

    /// Fetches WeatherKit monthly statistics for the given month range.
    pub fn monthly_statistics_between_months(
        &self,
        location: &CLLocation,
        start_month: i64,
        end_month: i64,
        query: MonthlyWeatherStatisticsQuery,
    ) -> Result<MonthlyWeatherStatisticsResult, WeatherKitError> {
        self.monthly_statistics_with_scope(
            location,
            query,
            QueryScope::Index {
                start: start_month,
                end: end_month,
            },
        )
    }

    /// Returns today's WeatherKit sun events from the daily forecast.
    pub fn sun_events(&self, location: &CLLocation) -> Result<SunEvents, WeatherKitError> {
        let forecast = self.daily_forecast(location)?;
        forecast
            .forecast
            .first()
            .map(|day| day.sun.clone())
            .ok_or_else(|| WeatherKitError::bridge(-1, "daily forecast returned no days"))
    }

    /// Returns today's WeatherKit moon events from the daily forecast.
    pub fn moon_events(&self, location: &CLLocation) -> Result<MoonEvents, WeatherKitError> {
        let forecast = self.daily_forecast(location)?;
        forecast
            .forecast
            .first()
            .map(|day| day.moon.clone())
            .ok_or_else(|| WeatherKitError::bridge(-1, "daily forecast returned no days"))
    }

    /// Returns the current WeatherKit pressure reading.
    pub fn pressure(&self, location: &CLLocation) -> Result<Pressure, WeatherKitError> {
        Ok(self.current_weather(location)?.pressure_reading())
    }

    fn daily_statistics_with_scope(
        &self,
        location: &CLLocation,
        query: DailyWeatherStatisticsQuery,
        scope: QueryScope<'_>,
    ) -> Result<DailyWeatherStatisticsResult, WeatherKitError> {
        let ptr = self.fetch_scoped_query_handle(
            location,
            query.query_kind(),
            scope,
            ffi::statistics::wk_weather_service_daily_statistics,
            "WeatherService.dailyStatistics",
        )?;
        match query {
            DailyWeatherStatisticsQuery::Temperature => {
                Ok(DailyWeatherStatisticsResult::Temperature(
                    DailyWeatherStatistics::<DayTemperatureStatistics>::from_owned_ptr(ptr)?,
                ))
            }
            DailyWeatherStatisticsQuery::Precipitation => {
                Ok(DailyWeatherStatisticsResult::Precipitation(
                    DailyWeatherStatistics::<DayPrecipitationStatistics>::from_owned_ptr(ptr)?,
                ))
            }
        }
    }

    fn daily_summary_with_scope(
        &self,
        location: &CLLocation,
        query: DailyWeatherSummaryQuery,
        scope: QueryScope<'_>,
    ) -> Result<DailyWeatherSummaryResult, WeatherKitError> {
        let ptr = self.fetch_scoped_query_handle(
            location,
            query.query_kind(),
            scope,
            ffi::statistics::wk_weather_service_daily_summary,
            "WeatherService.dailySummary",
        )?;
        match query {
            DailyWeatherSummaryQuery::Temperature => Ok(DailyWeatherSummaryResult::Temperature(
                DailyWeatherSummary::<DayTemperatureSummary>::from_owned_ptr(ptr)?,
            )),
            DailyWeatherSummaryQuery::Precipitation => {
                Ok(DailyWeatherSummaryResult::Precipitation(
                    DailyWeatherSummary::<DayPrecipitationSummary>::from_owned_ptr(ptr)?,
                ))
            }
        }
    }

    fn hourly_statistics_with_scope(
        &self,
        location: &CLLocation,
        query: HourlyWeatherStatisticsQuery,
        scope: QueryScope<'_>,
    ) -> Result<HourlyWeatherStatistics<HourTemperatureStatistics>, WeatherKitError> {
        let ptr = self.fetch_scoped_query_handle(
            location,
            query.query_kind(),
            scope,
            ffi::statistics::wk_weather_service_hourly_statistics,
            "WeatherService.hourlyStatistics",
        )?;
        HourlyWeatherStatistics::<HourTemperatureStatistics>::from_owned_ptr(ptr)
    }

    fn monthly_statistics_with_scope(
        &self,
        location: &CLLocation,
        query: MonthlyWeatherStatisticsQuery,
        scope: QueryScope<'_>,
    ) -> Result<MonthlyWeatherStatisticsResult, WeatherKitError> {
        let ptr = self.fetch_scoped_query_handle(
            location,
            query.query_kind(),
            scope,
            ffi::statistics::wk_weather_service_monthly_statistics,
            "WeatherService.monthlyStatistics",
        )?;
        match query {
            MonthlyWeatherStatisticsQuery::Temperature => {
                Ok(MonthlyWeatherStatisticsResult::Temperature(
                    MonthlyWeatherStatistics::<MonthTemperatureStatistics>::from_owned_ptr(ptr)?,
                ))
            }
            MonthlyWeatherStatisticsQuery::Precipitation => {
                Ok(MonthlyWeatherStatisticsResult::Precipitation(
                    MonthlyWeatherStatistics::<MonthPrecipitationStatistics>::from_owned_ptr(ptr)?,
                ))
            }
        }
    }

    fn fetch_scoped_query_handle(
        &self,
        location: &CLLocation,
        query_kind: i32,
        scope: QueryScope<'_>,
        call: ScopedQueryFetchFn,
        context: &str,
    ) -> Result<*mut c_void, WeatherKitError> {
        location.validate()?;
        let service = ServiceHandle::acquire(self.kind)?;
        let mut out_handle = core::ptr::null_mut();
        let mut out_error = core::ptr::null_mut();
        let (scope_kind, start_seconds, end_seconds, start_index, end_index) = match scope {
            QueryScope::None => (0, 0.0, 0.0, 0, 0),
            QueryScope::Interval(interval) => {
                (1, interval.start_seconds()?, interval.end_seconds()?, 0, 0)
            }
            QueryScope::Index { start, end } => {
                validate_index_range(start, end, context)?;
                (2, 0.0, 0.0, start, end)
            }
        };
        let status = unsafe {
            // SAFETY: service.as_ptr() is a valid retained handle from
            // ServiceHandle::acquire; out_handle and out_error are valid
            // stack-allocated output pointers.
            call(
                service.as_ptr(),
                location.latitude,
                location.longitude,
                query_kind,
                scope_kind,
                start_seconds,
                end_seconds,
                start_index,
                end_index,
                &mut out_handle,
                &mut out_error,
            )
        };
        if status != ffi::status::OK {
            // SAFETY: error_from_status takes ownership of out_error (C string
            // allocated by the Swift bridge) and frees it via wk_string_free.
            return Err(unsafe { error_from_status(status, out_error) });
        }
        if out_handle.is_null() {
            return Err(WeatherKitError::bridge(
                -1,
                format!("missing handle for {context}"),
            ));
        }
        Ok(out_handle)
    }

    fn fetch_service_handle(
        &self,
        call: ServiceFetchFn,
        context: &str,
    ) -> Result<*mut c_void, WeatherKitError> {
        let service = ServiceHandle::acquire(self.kind)?;
        let mut out_handle = core::ptr::null_mut();
        let mut out_error = core::ptr::null_mut();
        // SAFETY: service.as_ptr() is a valid retained handle; out_handle and
        // out_error are valid stack-allocated output pointers.
        let status = unsafe { call(service.as_ptr(), &mut out_handle, &mut out_error) };
        if status != ffi::status::OK {
            // SAFETY: error_from_status takes ownership of the C string and frees it.
            return Err(unsafe { error_from_status(status, out_error) });
        }
        if out_handle.is_null() {
            return Err(WeatherKitError::bridge(
                -1,
                format!("missing handle for {context}"),
            ));
        }
        Ok(out_handle)
    }

    fn fetch_location_handle(
        &self,
        location: &CLLocation,
        call: LocationFetchFn,
        context: &str,
    ) -> Result<*mut c_void, WeatherKitError> {
        location.validate()?;
        let service = ServiceHandle::acquire(self.kind)?;
        let mut out_handle = core::ptr::null_mut();
        let mut out_error = core::ptr::null_mut();
        // SAFETY: service.as_ptr() is a valid retained handle; out_handle and
        // out_error are valid stack-allocated output pointers.
        let status = unsafe {
            call(
                service.as_ptr(),
                location.latitude,
                location.longitude,
                &mut out_handle,
                &mut out_error,
            )
        };
        if status != ffi::status::OK {
            // SAFETY: error_from_status takes ownership of the C string and frees it.
            return Err(unsafe { error_from_status(status, out_error) });
        }
        if out_handle.is_null() {
            return Err(WeatherKitError::bridge(
                -1,
                format!("missing handle for {context}"),
            ));
        }
        Ok(out_handle)
    }

    fn fetch_interval_handle(
        &self,
        location: &CLLocation,
        interval: Option<&DateInterval>,
        call: IntervalFetchFn,
        context: &str,
    ) -> Result<*mut c_void, WeatherKitError> {
        location.validate()?;
        let service = ServiceHandle::acquire(self.kind)?;
        let mut out_handle = core::ptr::null_mut();
        let mut out_error = core::ptr::null_mut();
        let (has_range, start_seconds, end_seconds) = if let Some(interval) = interval {
            (1, interval.start_seconds()?, interval.end_seconds()?)
        } else {
            (0, 0.0, 0.0)
        };
        // SAFETY: service.as_ptr() is a valid retained handle; out_handle and
        // out_error are valid stack-allocated output pointers.
        let status = unsafe {
            call(
                service.as_ptr(),
                location.latitude,
                location.longitude,
                has_range,
                start_seconds,
                end_seconds,
                &mut out_handle,
                &mut out_error,
            )
        };
        if status != ffi::status::OK {
            // SAFETY: error_from_status takes ownership of the C string and frees it.
            return Err(unsafe { error_from_status(status, out_error) });
        }
        if out_handle.is_null() {
            return Err(WeatherKitError::bridge(
                -1,
                format!("missing handle for {context}"),
            ));
        }
        Ok(out_handle)
    }
}

fn validate_index_range(start: i64, end: i64, context: &str) -> Result<(), WeatherKitError> {
    if start > end {
        return Err(WeatherKitError::bridge(
            -1,
            format!("{context} start index must not be after end index"),
        ));
    }
    Ok(())
}

fn unix_seconds(time: SystemTime) -> Result<f64, WeatherKitError> {
    let duration = time.duration_since(UNIX_EPOCH).map_err(|error| {
        WeatherKitError::bridge(-1, format!("time {time:?} is before UNIX_EPOCH: {error}"))
    })?;
    Ok(duration.as_secs_f64())
}