photokit 0.4.5

Safe Rust bindings for Apple's Photos framework — photo library access 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
use core::ffi::c_void;
use std::ptr::{self, NonNull};

use base64::Engine;
use serde::{Deserialize, Serialize};

use crate::asset::PHAsset;
use crate::error::{NSErrorInfo, PhotoKitError};
use crate::ffi;
use crate::live_photo::PHLivePhotoResult;
use crate::private::{cstring_from_str, json_cstring, parse_json_ptr};

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Wraps the target size passed to `PHImageManager` image requests.
pub struct PHImageSize {
    /// Corresponds to `PHImageSize.width`.
    pub width: f64,
    /// Corresponds to `PHImageSize.height`.
    pub height: f64,
}

#[allow(non_upper_case_globals)]
/// Matches `PHImageManagerMaximumSize`.
pub const PHImageManagerMaximumSize: PHImageSize = PHImageSize {
    width: -1.0,
    height: -1.0,
};
#[allow(non_upper_case_globals)]
/// Matches `PHImageResultRequestIDKey`.
pub const PHImageResultRequestIDKey: &str = "PHImageResultRequestIDKey";
#[allow(non_upper_case_globals)]
/// Matches `PHImageErrorKey`.
pub const PHImageErrorKey: &str = "PHImageErrorKey";

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
/// Wraps `PHImageContentMode`.
pub enum PHImageContentMode {
    #[default]
    /// Case of `PHImageContentMode`.
    Default,
    /// Case of `PHImageContentMode`.
    AspectFit,
    /// Case of `PHImageContentMode`.
    AspectFill,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Wraps `PHImageRequestOptionsVersion`.
pub enum PHImageRequestOptionsVersion {
    /// Case of `PHImageRequestOptionsVersion`.
    Current,
    /// Case of `PHImageRequestOptionsVersion`.
    Unadjusted,
    /// Case of `PHImageRequestOptionsVersion`.
    Original,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Wraps `PHImageRequestOptionsDeliveryMode`.
pub enum PHImageRequestOptionsDeliveryMode {
    /// Case of `PHImageRequestOptionsDeliveryMode`.
    Opportunistic,
    /// Case of `PHImageRequestOptionsDeliveryMode`.
    HighQualityFormat,
    /// Case of `PHImageRequestOptionsDeliveryMode`.
    FastFormat,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Wraps `PHImageRequestOptionsResizeMode`.
pub enum PHImageRequestOptionsResizeMode {
    /// Case of `PHImageRequestOptionsResizeMode`.
    None,
    /// Case of `PHImageRequestOptionsResizeMode`.
    Fast,
    /// Case of `PHImageRequestOptionsResizeMode`.
    Exact,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
/// Wraps `PHVideoRequestOptionsVersion`.
pub struct PHVideoRequestOptionsVersion(
    /// Raw value for `PHVideoRequestOptionsVersion`.
    pub i64,
);

impl PHVideoRequestOptionsVersion {
    /// Constant on `PHVideoRequestOptionsVersion`.
    pub const CURRENT: Self = Self(0);
    /// Constant on `PHVideoRequestOptionsVersion`.
    pub const ORIGINAL: Self = Self(1);
}

impl Default for PHVideoRequestOptionsVersion {
    fn default() -> Self {
        Self::CURRENT
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
/// Wraps `PHVideoRequestOptionsDeliveryMode`.
pub struct PHVideoRequestOptionsDeliveryMode(
    /// Raw value for `PHVideoRequestOptionsDeliveryMode`.
    pub i64,
);

impl PHVideoRequestOptionsDeliveryMode {
    /// Constant on `PHVideoRequestOptionsDeliveryMode`.
    pub const AUTOMATIC: Self = Self(0);
    /// Constant on `PHVideoRequestOptionsDeliveryMode`.
    pub const HIGH_QUALITY_FORMAT: Self = Self(1);
    /// Constant on `PHVideoRequestOptionsDeliveryMode`.
    pub const MEDIUM_QUALITY_FORMAT: Self = Self(2);
    /// Constant on `PHVideoRequestOptionsDeliveryMode`.
    pub const FAST_FORMAT: Self = Self(3);
}

impl Default for PHVideoRequestOptionsDeliveryMode {
    fn default() -> Self {
        Self::AUTOMATIC
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
/// Wraps `PHVideoRequestOptions`.
pub struct PHVideoRequestOptions {
    #[serde(default)]
    /// Corresponds to `PHVideoRequestOptions.networkAccessAllowed`.
    pub network_access_allowed: bool,
    #[serde(default)]
    /// Corresponds to `PHVideoRequestOptions.version`.
    pub version: PHVideoRequestOptionsVersion,
    #[serde(default)]
    /// Corresponds to `PHVideoRequestOptions.deliveryMode`.
    pub delivery_mode: PHVideoRequestOptionsDeliveryMode,
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Request builder for `PHImageManager.requestImage`.
pub struct PHImageRequest {
    /// Corresponds to `PHImageRequest.targetWidth`.
    pub target_width: f64,
    /// Corresponds to `PHImageRequest.targetHeight`.
    pub target_height: f64,
    /// Corresponds to `PHImageRequest.contentMode`.
    pub content_mode: PHImageContentMode,
    /// Corresponds to `PHImageRequest.version`.
    pub version: Option<PHImageRequestOptionsVersion>,
    /// Corresponds to `PHImageRequest.deliveryMode`.
    pub delivery_mode: Option<PHImageRequestOptionsDeliveryMode>,
    /// Corresponds to `PHImageRequest.resizeMode`.
    pub resize_mode: Option<PHImageRequestOptionsResizeMode>,
    #[serde(default)]
    /// Corresponds to `PHImageRequest.networkAccessAllowed`.
    pub network_access_allowed: bool,
    #[serde(default)]
    /// Corresponds to `PHImageRequest.synchronous`.
    pub synchronous: bool,
    #[serde(default)]
    /// Corresponds to `PHImageRequest.allowSecondaryDegradedImage`.
    pub allow_secondary_degraded_image: bool,
}

impl PHImageRequest {
    /// Creates a helper value for the related Photos framework API.
    pub fn new(target_width: f64, target_height: f64, content_mode: PHImageContentMode) -> Self {
        Self {
            target_width,
            target_height,
            content_mode,
            version: None,
            delivery_mode: None,
            resize_mode: None,
            network_access_allowed: false,
            synchronous: false,
            allow_secondary_degraded_image: false,
        }
    }

    /// Wraps a Photos framework operation on `PHImageRequest`.
    pub fn maximum(content_mode: PHImageContentMode) -> Self {
        Self::new(
            PHImageManagerMaximumSize.width,
            PHImageManagerMaximumSize.height,
            content_mode,
        )
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Serialized result delivered by `PHImageManager.requestImage`.
pub struct PHImageResult {
    /// Corresponds to `PHImageResult.tiffDataBase64`.
    pub tiff_data_base64: String,
    /// Corresponds to `PHImageResult.width`.
    pub width: f64,
    /// Corresponds to `PHImageResult.height`.
    pub height: f64,
    /// Corresponds to `PHImageResult.cancelled`.
    pub cancelled: bool,
    /// Corresponds to `PHImageResult.degraded`.
    pub degraded: bool,
    #[serde(default, alias = "requestID")]
    /// Corresponds to `PHImageResult.requestId`.
    pub request_id: Option<i32>,
    #[serde(default)]
    /// Corresponds to `PHImageResult.error`.
    pub error: Option<NSErrorInfo>,
}

impl PHImageResult {
    /// Wraps a Photos framework operation on `PHImageResult`.
    pub fn tiff_data(&self) -> Vec<u8> {
        base64::engine::general_purpose::STANDARD
            .decode(self.tiff_data_base64.as_bytes())
            .unwrap_or_default()
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Serialized result delivered by `PHImageManager.requestImageDataAndOrientation`.
pub struct PHImageDataResult {
    /// Corresponds to `PHImageDataResult.dataBase64`.
    pub data_base64: String,
    /// Corresponds to `PHImageDataResult.uniformTypeIdentifier`.
    pub uniform_type_identifier: Option<String>,
    #[serde(default)]
    /// Corresponds to `PHImageDataResult.contentTypeIdentifier`.
    pub content_type_identifier: Option<String>,
    /// Corresponds to `PHImageDataResult.orientation`.
    pub orientation: i32,
    /// Corresponds to `PHImageDataResult.cancelled`.
    pub cancelled: bool,
    #[serde(default)]
    /// Corresponds to `PHImageDataResult.degraded`.
    pub degraded: bool,
    #[serde(default)]
    /// Corresponds to `PHImageDataResult.isInCloud`.
    pub is_in_cloud: bool,
    #[serde(default, alias = "requestID")]
    /// Corresponds to `PHImageDataResult.requestId`.
    pub request_id: Option<i32>,
    #[serde(default)]
    /// Corresponds to `PHImageDataResult.error`.
    pub error: Option<NSErrorInfo>,
}

impl PHImageDataResult {
    /// Decodes the binary data carried by `PHImageDataResult`.
    pub fn data(&self) -> Vec<u8> {
        base64::engine::general_purpose::STANDARD
            .decode(self.data_base64.as_bytes())
            .unwrap_or_default()
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Serialized result delivered by `PHImageManager` video request APIs.
pub struct PHVideoResult {
    /// Corresponds to `PHVideoResult.resultType`.
    pub result_type: String,
    #[serde(default, alias = "requestID")]
    /// Corresponds to `PHVideoResult.requestId`.
    pub request_id: Option<i32>,
    #[serde(default)]
    /// Corresponds to `PHVideoResult.cancelled`.
    pub cancelled: bool,
    #[serde(default)]
    /// Corresponds to `PHVideoResult.isInCloud`.
    pub is_in_cloud: bool,
    #[serde(default)]
    /// Corresponds to `PHVideoResult.error`.
    pub error: Option<NSErrorInfo>,
    #[serde(default)]
    /// Corresponds to `PHVideoResult.assetUrl`.
    pub asset_url: Option<String>,
    #[serde(default)]
    /// Corresponds to `PHVideoResult.durationSeconds`.
    pub duration_seconds: Option<f64>,
    #[serde(default)]
    /// Corresponds to `PHVideoResult.hasPlayerItem`.
    pub has_player_item: bool,
    #[serde(default)]
    /// Corresponds to `PHVideoResult.hasExportSession`.
    pub has_export_session: bool,
    #[serde(default)]
    /// Corresponds to `PHVideoResult.hasAvAsset`.
    pub has_av_asset: bool,
    #[serde(default)]
    /// Corresponds to `PHVideoResult.hasAudioMix`.
    pub has_audio_mix: bool,
    #[serde(default)]
    /// Corresponds to `PHVideoResult.exportPreset`.
    pub export_preset: Option<String>,
    #[serde(default)]
    /// Corresponds to `PHVideoResult.supportedFileTypes`.
    pub supported_file_types: Vec<String>,
}

#[derive(Debug)]
/// Wraps `PHImageManager`.
pub struct PHImageManager {
    raw: NonNull<c_void>,
}

impl PHImageManager {
    /// Returns the shared Photos framework `PHImageManager` instance.
    pub fn shared() -> Result<Self, PhotoKitError> {
        let raw = NonNull::new(unsafe { ffi::ph_image_manager_default() }).ok_or_else(|| {
            PhotoKitError::OperationFailed("failed to create PHImageManager".to_owned())
        })?;
        Ok(Self { raw })
    }

    /// Wraps a Photos framework request operation on `PHImageManager`.
    pub fn request_image(
        &self,
        asset: &PHAsset,
        request: PHImageRequest,
    ) -> Result<PHImageRequestHandle, PhotoKitError> {
        let asset_identifier = cstring_from_str(&asset.local_identifier, "asset local identifier")?;
        let request_json = json_cstring(&request, "PHImageRequest")?;
        let mut error = ptr::null_mut();
        let raw = unsafe {
            ffi::ph_image_manager_request_image(
                self.raw.as_ptr(),
                asset_identifier.as_ptr(),
                request_json.as_ptr(),
                &mut error,
            )
        };
        NonNull::new(raw)
            .map(|raw| PHImageRequestHandle { raw })
            .ok_or_else(|| unsafe { PhotoKitError::from_error_ptr(error, "requestImage failed") })
    }

    /// Wraps a Photos framework request operation on `PHImageManager`.
    pub fn request_image_data(
        &self,
        asset: &PHAsset,
        request: &PHImageRequest,
    ) -> Result<PHImageDataRequestHandle, PhotoKitError> {
        let asset_identifier = cstring_from_str(&asset.local_identifier, "asset local identifier")?;
        let request_json = json_cstring(request, "PHImageRequest")?;
        let mut error = ptr::null_mut();
        let raw = unsafe {
            ffi::ph_image_manager_request_image_data(
                self.raw.as_ptr(),
                asset_identifier.as_ptr(),
                request_json.as_ptr(),
                &mut error,
            )
        };
        NonNull::new(raw)
            .map(|raw| PHImageDataRequestHandle { raw })
            .ok_or_else(|| unsafe {
                PhotoKitError::from_error_ptr(error, "requestImageData failed")
            })
    }

    /// Wraps a Photos framework request operation on `PHImageManager`.
    pub fn request_live_photo(
        &self,
        asset: &PHAsset,
        request: PHImageRequest,
    ) -> Result<PHLivePhotoRequestHandle, PhotoKitError> {
        let asset_identifier = cstring_from_str(&asset.local_identifier, "asset local identifier")?;
        let request_json = json_cstring(&request, "PHImageRequest")?;
        let mut error = ptr::null_mut();
        let raw = unsafe {
            ffi::ph_image_manager_request_live_photo(
                self.raw.as_ptr(),
                asset_identifier.as_ptr(),
                request_json.as_ptr(),
                &mut error,
            )
        };
        NonNull::new(raw)
            .map(|raw| PHLivePhotoRequestHandle { raw })
            .ok_or_else(|| unsafe {
                PhotoKitError::from_error_ptr(error, "requestLivePhoto failed")
            })
    }

    /// Wraps a Photos framework request operation on `PHImageManager`.
    pub fn request_player_item_for_video(
        &self,
        asset: &PHAsset,
        options: &PHVideoRequestOptions,
        timeout_ms: u64,
    ) -> Result<PHVideoResult, PhotoKitError> {
        self.request_video_json(
            asset,
            options,
            timeout_ms,
            None,
            ffi::ph_image_manager_request_player_item_for_video_json,
        )
    }

    /// Wraps a Photos framework request operation on `PHImageManager`.
    pub fn request_export_session_for_video(
        &self,
        asset: &PHAsset,
        options: &PHVideoRequestOptions,
        export_preset: &str,
        timeout_ms: u64,
    ) -> Result<PHVideoResult, PhotoKitError> {
        let export_preset = cstring_from_str(export_preset, "video export preset")?;
        let asset_identifier = cstring_from_str(&asset.local_identifier, "asset local identifier")?;
        let options_json = json_cstring(options, "PHVideoRequestOptions")?;
        let mut error = ptr::null_mut();
        let payload = unsafe {
            ffi::ph_image_manager_request_export_session_for_video_json(
                self.raw.as_ptr(),
                asset_identifier.as_ptr(),
                options_json.as_ptr(),
                export_preset.as_ptr(),
                timeout_ms,
                &mut error,
            )
        };
        if payload.is_null() {
            Err(unsafe {
                PhotoKitError::from_error_ptr(error, "requestExportSessionForVideo failed")
            })
        } else {
            unsafe { parse_json_ptr(payload, "PHVideoResult") }
        }
    }

    /// Wraps a Photos framework request operation on `PHImageManager`.
    pub fn request_av_asset_for_video(
        &self,
        asset: &PHAsset,
        options: &PHVideoRequestOptions,
        timeout_ms: u64,
    ) -> Result<PHVideoResult, PhotoKitError> {
        self.request_video_json(
            asset,
            options,
            timeout_ms,
            None,
            ffi::ph_image_manager_request_av_asset_for_video_json,
        )
    }

    fn request_video_json(
        &self,
        asset: &PHAsset,
        options: &PHVideoRequestOptions,
        timeout_ms: u64,
        export_preset: Option<&str>,
        request_fn: unsafe extern "C" fn(
            *mut c_void,
            *const core::ffi::c_char,
            *const core::ffi::c_char,
            u64,
            *mut *mut core::ffi::c_char,
        ) -> *mut core::ffi::c_char,
    ) -> Result<PHVideoResult, PhotoKitError> {
        debug_assert!(export_preset.is_none());
        let asset_identifier = cstring_from_str(&asset.local_identifier, "asset local identifier")?;
        let options_json = json_cstring(options, "PHVideoRequestOptions")?;
        let mut error = ptr::null_mut();
        let payload = unsafe {
            request_fn(
                self.raw.as_ptr(),
                asset_identifier.as_ptr(),
                options_json.as_ptr(),
                timeout_ms,
                &mut error,
            )
        };
        if payload.is_null() {
            Err(unsafe { PhotoKitError::from_error_ptr(error, "video request failed") })
        } else {
            unsafe { parse_json_ptr(payload, "PHVideoResult") }
        }
    }
}

impl Drop for PHImageManager {
    fn drop(&mut self) {
        unsafe { ffi::ph_image_manager_release(self.raw.as_ptr()) };
    }
}

#[derive(Debug)]
/// Wraps `PHCachingImageManager`.
pub struct PHCachingImageManager {
    raw: NonNull<c_void>,
}

impl PHCachingImageManager {
    /// Creates a helper value for the related Photos framework API.
    pub fn new() -> Result<Self, PhotoKitError> {
        let raw =
            NonNull::new(unsafe { ffi::ph_caching_image_manager_new() }).ok_or_else(|| {
                PhotoKitError::OperationFailed("failed to create PHCachingImageManager".to_owned())
            })?;
        Ok(Self { raw })
    }

    /// Wraps a Photos framework operation on `PHCachingImageManager`.
    pub fn start_caching_images(
        &self,
        assets: &[PHAsset],
        request: &PHImageRequest,
    ) -> Result<(), PhotoKitError> {
        let identifiers: Vec<String> = assets
            .iter()
            .map(|asset| asset.local_identifier.clone())
            .collect();
        let identifiers_json = json_cstring(&identifiers, "asset identifiers")?;
        let request_json = json_cstring(request, "PHImageRequest")?;
        let mut error = ptr::null_mut();
        let status = unsafe {
            ffi::ph_caching_image_manager_start_caching(
                self.raw.as_ptr(),
                identifiers_json.as_ptr(),
                request_json.as_ptr(),
                &mut error,
            )
        };
        if status == ffi::status::OK && error.is_null() {
            Ok(())
        } else {
            Err(unsafe { PhotoKitError::from_error_ptr(error, "startCachingImages failed") })
        }
    }

    /// Wraps a Photos framework operation on `PHCachingImageManager`.
    pub fn stop_caching_images(
        &self,
        assets: &[PHAsset],
        request: &PHImageRequest,
    ) -> Result<(), PhotoKitError> {
        let identifiers: Vec<String> = assets
            .iter()
            .map(|asset| asset.local_identifier.clone())
            .collect();
        let identifiers_json = json_cstring(&identifiers, "asset identifiers")?;
        let request_json = json_cstring(request, "PHImageRequest")?;
        let mut error = ptr::null_mut();
        let status = unsafe {
            ffi::ph_caching_image_manager_stop_caching(
                self.raw.as_ptr(),
                identifiers_json.as_ptr(),
                request_json.as_ptr(),
                &mut error,
            )
        };
        if status == ffi::status::OK && error.is_null() {
            Ok(())
        } else {
            Err(unsafe { PhotoKitError::from_error_ptr(error, "stopCachingImages failed") })
        }
    }

    /// Wraps a Photos framework operation on `PHCachingImageManager`.
    pub fn stop_caching_images_for_all_assets(&self) {
        unsafe { ffi::ph_caching_image_manager_stop_caching_all(self.raw.as_ptr()) };
    }
}

impl Drop for PHCachingImageManager {
    fn drop(&mut self) {
        unsafe { ffi::ph_image_manager_release(self.raw.as_ptr()) };
    }
}

#[derive(Debug)]
/// Cancellable request handle returned by `PHImageManager.requestImage`.
pub struct PHImageRequestHandle {
    pub(crate) raw: NonNull<c_void>,
}

impl PHImageRequestHandle {
    /// Waits for the Photos framework operation represented by `PHImageRequestHandle` to finish.
    pub fn wait(&self, timeout_ms: u64) -> Result<PHImageResult, PhotoKitError> {
        wait_for_request(self.raw, timeout_ms, "PHImageResult")
    }

    /// Cancels the Photos framework operation represented by `PHImageRequestHandle`.
    pub fn cancel(&self) {
        unsafe { ffi::ph_image_request_cancel(self.raw.as_ptr()) };
    }
}

impl Drop for PHImageRequestHandle {
    fn drop(&mut self) {
        unsafe { ffi::ph_image_request_release(self.raw.as_ptr()) };
    }
}

#[derive(Debug)]
/// Cancellable request handle returned by `PHImageManager.requestImageData`.
pub struct PHImageDataRequestHandle {
    raw: NonNull<c_void>,
}

impl PHImageDataRequestHandle {
    /// Waits for the Photos framework operation represented by `PHImageDataRequestHandle` to finish.
    pub fn wait(&self, timeout_ms: u64) -> Result<PHImageDataResult, PhotoKitError> {
        wait_for_request(self.raw, timeout_ms, "PHImageDataResult")
    }

    /// Cancels the Photos framework operation represented by `PHImageDataRequestHandle`.
    pub fn cancel(&self) {
        unsafe { ffi::ph_image_request_cancel(self.raw.as_ptr()) };
    }
}

impl Drop for PHImageDataRequestHandle {
    fn drop(&mut self) {
        unsafe { ffi::ph_image_request_release(self.raw.as_ptr()) };
    }
}

#[derive(Debug)]
/// Cancellable request handle returned by `PHImageManager.requestLivePhoto`.
pub struct PHLivePhotoRequestHandle {
    pub(crate) raw: NonNull<c_void>,
}

impl PHLivePhotoRequestHandle {
    /// Waits for the Photos framework operation represented by `PHLivePhotoRequestHandle` to finish.
    pub fn wait(&self, timeout_ms: u64) -> Result<PHLivePhotoResult, PhotoKitError> {
        wait_for_request(self.raw, timeout_ms, "PHLivePhotoResult")
    }

    /// Cancels the Photos framework operation represented by `PHLivePhotoRequestHandle`.
    pub fn cancel(&self) {
        unsafe { ffi::ph_image_request_cancel(self.raw.as_ptr()) };
    }
}

impl Drop for PHLivePhotoRequestHandle {
    fn drop(&mut self) {
        unsafe { ffi::ph_image_request_release(self.raw.as_ptr()) };
    }
}

fn wait_for_request<T: serde::de::DeserializeOwned>(
    raw: NonNull<c_void>,
    timeout_ms: u64,
    context: &str,
) -> Result<T, PhotoKitError> {
    let mut error = ptr::null_mut();
    let payload = unsafe { ffi::ph_image_request_wait_json(raw.as_ptr(), timeout_ms, &mut error) };
    if payload.is_null() {
        Err(unsafe { PhotoKitError::from_error_ptr(error, "request wait failed") })
    } else {
        unsafe { parse_json_ptr(payload, context) }
    }
}