photokit 0.4.0

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
//! Async API for `photokit` (Photos.framework)
//!
//! This module wraps Photos.framework completion-handler APIs as executor-agnostic
//! [`std::future::Future`] types. Enable with the `async` Cargo feature.
//!
//! ## Available Types
//!
//! | Type | Description |
//! |------|-------------|
//! | [`AsyncPHPhotoLibrary`](crate::async_api::AsyncPHPhotoLibrary) | Async authorization + `performChanges` entrypoints |
//! | [`AsyncPHAssetChangeRequest`](crate::async_api::AsyncPHAssetChangeRequest) | Async asset change requests |
//! | [`AsyncPHAssetCollectionChangeRequest`](crate::async_api::AsyncPHAssetCollectionChangeRequest) | Async asset-collection change requests |
//! | [`AsyncPHCollectionListChangeRequest`](crate::async_api::AsyncPHCollectionListChangeRequest) | Async collection-list change requests |
//! | [`AsyncPHImageManager`](crate::async_api::AsyncPHImageManager) | Async image + image-data requests |
//! | [`AsyncPHLivePhotoEditingContext`](crate::async_api::AsyncPHLivePhotoEditingContext) | Async live photo save + prepare |
//!
//! ## Notes
//! - `PHCachingImageManager::start/stop_caching_images` are synchronous; use the
//!   sync API from [`crate::image_manager`] directly.
//! - Multi-fire delegate callbacks (change observers, availability observers) are
//!   stream patterns and deferred to Tier 2.
//!
//! ## Example
//! ```no_run
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use photokit::async_api::AsyncPHPhotoLibrary;
//! use photokit::PHAccessLevel;
//!
//! pollster::block_on(async {
//!     let status = AsyncPHPhotoLibrary::request_authorization(PHAccessLevel::ReadWrite).await?;
//!     println!("Status: {status:?}");
//!     Ok::<_, Box<dyn std::error::Error>>(())
//! })
//! # }
//! ```

use std::ffi::{c_void, CStr};
use std::future::Future;
use std::pin::Pin;
use std::ptr::NonNull;
use std::task::{Context, Poll};

use doom_fish_utils::completion::{error_from_cstr, AsyncCompletion, AsyncCompletionFuture};
use serde::Deserialize;

use crate::asset::PHAsset;
use crate::asset_change_request::PHAssetChangeRequest;
use crate::asset_collection_change_request::PHAssetCollectionChangeRequest;
use crate::collection_list_change_request::PHCollectionListChangeRequest;
use crate::content_editing_output::PHContentEditingOutput;
use crate::error::{PHAuthorizationStatus, PhotoKitError};
use crate::image_manager::{PHImageDataResult, PHImageRequest, PHImageResult};
use crate::live_photo::PHLivePhotoResult;
use crate::live_photo_editing_context::{PHLivePhotoEditingContext, PHLivePhotoEditingSaveResult};
use crate::photo_library::PHAccessLevel;
use crate::private::{cstring_from_str, json_cstring};

macro_rules! define_json_callback {
    ($name:ident, $t:ty) => {
        unsafe extern "C" fn $name(
            result: *const core::ffi::c_char,
            error: *const core::ffi::c_char,
            ctx: *mut c_void,
        ) {
            if !error.is_null() {
                let message = error_from_cstr(error);
                AsyncCompletion::<$t>::complete_err(ctx, message);
            } else if !result.is_null() {
                let json = CStr::from_ptr(result).to_string_lossy();
                match serde_json::from_str::<$t>(json.as_ref()) {
                    Ok(value) => AsyncCompletion::complete_ok(ctx, value),
                    Err(err) => AsyncCompletion::<$t>::complete_err(ctx, err.to_string()),
                }
            } else {
                AsyncCompletion::<$t>::complete_err(
                    ctx,
                    "no result or error from async callback".to_owned(),
                );
            }
        }
    };
}

#[derive(Debug, Deserialize)]
struct AuthStatusPayload {
    status: i32,
}

define_json_callback!(auth_status_callback, AuthStatusPayload);

/// Future for [`AsyncPHPhotoLibrary::request_authorization`].
pub struct RequestAuthorizationFuture {
    inner: AsyncCompletionFuture<AuthStatusPayload>,
}

impl core::fmt::Debug for RequestAuthorizationFuture {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("RequestAuthorizationFuture")
            .finish_non_exhaustive()
    }
}

impl Future for RequestAuthorizationFuture {
    type Output = Result<PHAuthorizationStatus, PhotoKitError>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        Pin::new(&mut self.inner).poll(cx).map(|result| {
            result
                .map(|payload| PHAuthorizationStatus::from_raw(payload.status))
                .map_err(PhotoKitError::OperationFailed)
        })
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ChangeResultPayload {
    placeholder_local_identifier: Option<String>,
}

define_json_callback!(change_result_callback, ChangeResultPayload);

/// Future for `performChanges`-backed async mutation requests.
pub struct PerformChangesFuture {
    inner: AsyncCompletionFuture<ChangeResultPayload>,
}

impl core::fmt::Debug for PerformChangesFuture {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("PerformChangesFuture")
            .finish_non_exhaustive()
    }
}

impl Future for PerformChangesFuture {
    type Output = Result<Option<String>, PhotoKitError>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        Pin::new(&mut self.inner).poll(cx).map(|result| {
            result
                .map(|payload| payload.placeholder_local_identifier)
                .map_err(PhotoKitError::OperationFailed)
        })
    }
}

define_json_callback!(image_result_callback, PHImageResult);
define_json_callback!(image_data_result_callback, PHImageDataResult);

/// Future for [`AsyncPHImageManager::request_image`].
pub struct RequestImageFuture {
    inner: AsyncCompletionFuture<PHImageResult>,
}

impl core::fmt::Debug for RequestImageFuture {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("RequestImageFuture").finish_non_exhaustive()
    }
}

impl Future for RequestImageFuture {
    type Output = Result<PHImageResult, PhotoKitError>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        Pin::new(&mut self.inner)
            .poll(cx)
            .map(|result| result.map_err(PhotoKitError::OperationFailed))
    }
}

/// Future for [`AsyncPHImageManager::request_image_data`].
pub struct RequestImageDataFuture {
    inner: AsyncCompletionFuture<PHImageDataResult>,
}

impl core::fmt::Debug for RequestImageDataFuture {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("RequestImageDataFuture")
            .finish_non_exhaustive()
    }
}

impl Future for RequestImageDataFuture {
    type Output = Result<PHImageDataResult, PhotoKitError>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        Pin::new(&mut self.inner)
            .poll(cx)
            .map(|result| result.map_err(PhotoKitError::OperationFailed))
    }
}

define_json_callback!(live_photo_save_callback, PHLivePhotoEditingSaveResult);
define_json_callback!(live_photo_prepare_callback, PHLivePhotoResult);

/// Future for [`AsyncPHLivePhotoEditingContext::save_live_photo`].
pub struct SaveLivePhotoFuture {
    inner: AsyncCompletionFuture<PHLivePhotoEditingSaveResult>,
}

impl core::fmt::Debug for SaveLivePhotoFuture {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("SaveLivePhotoFuture")
            .finish_non_exhaustive()
    }
}

impl Future for SaveLivePhotoFuture {
    type Output = Result<PHLivePhotoEditingSaveResult, PhotoKitError>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        Pin::new(&mut self.inner)
            .poll(cx)
            .map(|result| result.map_err(PhotoKitError::OperationFailed))
    }
}

/// Future for [`AsyncPHLivePhotoEditingContext::prepare_live_photo`].
pub struct PrepareLivePhotoFuture {
    inner: AsyncCompletionFuture<PHLivePhotoResult>,
}

impl core::fmt::Debug for PrepareLivePhotoFuture {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("PrepareLivePhotoFuture")
            .finish_non_exhaustive()
    }
}

impl Future for PrepareLivePhotoFuture {
    type Output = Result<PHLivePhotoResult, PhotoKitError>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        Pin::new(&mut self.inner)
            .poll(cx)
            .map(|result| result.map_err(PhotoKitError::OperationFailed))
    }
}

/// Async wrapper for `PHPhotoLibrary` authorization and `performChanges` operations.
#[derive(Debug, Clone, Copy)]
pub struct AsyncPHPhotoLibrary;

impl AsyncPHPhotoLibrary {
    /// Asynchronously request photo library authorization for the given access level.
    pub fn request_authorization(access_level: PHAccessLevel) -> RequestAuthorizationFuture {
        let (future, ctx) = AsyncCompletion::create();
        unsafe {
            crate::ffi::ph_photo_library_request_authorization_async(
                access_level.as_raw(),
                auth_status_callback,
                ctx,
            );
        }
        RequestAuthorizationFuture { inner: future }
    }

    /// Asynchronously perform asset changes via `PHPhotoLibrary.performChanges`.
    pub fn perform_asset_change(
        request: &PHAssetChangeRequest,
    ) -> Result<PerformChangesFuture, PhotoKitError> {
        let payload_json = json_cstring(request, "PHAssetChangeRequest")?;
        let (future, ctx) = AsyncCompletion::create();
        unsafe {
            crate::ffi::ph_asset_change_request_perform_async(
                payload_json.as_ptr(),
                change_result_callback,
                ctx,
            );
        }
        Ok(PerformChangesFuture { inner: future })
    }

    /// Asynchronously perform asset-collection changes via `PHPhotoLibrary.performChanges`.
    pub fn perform_collection_change(
        request: &PHAssetCollectionChangeRequest,
    ) -> Result<PerformChangesFuture, PhotoKitError> {
        let payload_json = json_cstring(request, "PHAssetCollectionChangeRequest")?;
        let (future, ctx) = AsyncCompletion::create();
        unsafe {
            crate::ffi::ph_asset_collection_change_request_perform_async(
                payload_json.as_ptr(),
                change_result_callback,
                ctx,
            );
        }
        Ok(PerformChangesFuture { inner: future })
    }

    /// Asynchronously perform collection-list changes via `PHPhotoLibrary.performChanges`.
    pub fn perform_collection_list_change(
        request: &PHCollectionListChangeRequest,
    ) -> Result<PerformChangesFuture, PhotoKitError> {
        let payload_json = json_cstring(request, "PHCollectionListChangeRequest")?;
        let (future, ctx) = AsyncCompletion::create();
        unsafe {
            crate::ffi::ph_collection_list_change_request_perform_async(
                payload_json.as_ptr(),
                change_result_callback,
                ctx,
            );
        }
        Ok(PerformChangesFuture { inner: future })
    }
}

/// Async wrapper for [`PHAssetChangeRequest`].
#[derive(Debug, Clone, Copy)]
pub struct AsyncPHAssetChangeRequest;

impl AsyncPHAssetChangeRequest {
    /// Asynchronously perform an asset change request.
    pub fn perform(request: &PHAssetChangeRequest) -> Result<PerformChangesFuture, PhotoKitError> {
        AsyncPHPhotoLibrary::perform_asset_change(request)
    }
}

/// Async wrapper for [`PHAssetCollectionChangeRequest`].
#[derive(Debug, Clone, Copy)]
pub struct AsyncPHAssetCollectionChangeRequest;

impl AsyncPHAssetCollectionChangeRequest {
    /// Asynchronously perform an asset-collection change request.
    pub fn perform(
        request: &PHAssetCollectionChangeRequest,
    ) -> Result<PerformChangesFuture, PhotoKitError> {
        AsyncPHPhotoLibrary::perform_collection_change(request)
    }
}

/// Async wrapper for [`PHCollectionListChangeRequest`].
#[derive(Debug, Clone, Copy)]
pub struct AsyncPHCollectionListChangeRequest;

impl AsyncPHCollectionListChangeRequest {
    /// Asynchronously perform a collection-list change request.
    pub fn perform(
        request: &PHCollectionListChangeRequest,
    ) -> Result<PerformChangesFuture, PhotoKitError> {
        AsyncPHPhotoLibrary::perform_collection_list_change(request)
    }
}

/// Async wrapper for `PHImageManager` image + image-data requests.
#[derive(Debug)]
pub struct AsyncPHImageManager {
    raw: NonNull<c_void>,
}

impl AsyncPHImageManager {
    /// Create an async wrapper around the shared `PHImageManager`.
    pub fn shared() -> Result<Self, PhotoKitError> {
        let raw =
            NonNull::new(unsafe { crate::ffi::ph_image_manager_default() }).ok_or_else(|| {
                PhotoKitError::OperationFailed("failed to get PHImageManager".to_owned())
            })?;
        Ok(Self { raw })
    }

    /// Asynchronously request an image for `asset`.
    pub fn request_image(
        &self,
        asset: &PHAsset,
        request: PHImageRequest,
    ) -> Result<RequestImageFuture, PhotoKitError> {
        let asset_id = cstring_from_str(&asset.local_identifier, "asset local identifier")?;
        let request_json = json_cstring(&request, "PHImageRequest")?;
        let (future, ctx) = AsyncCompletion::create();
        unsafe {
            crate::ffi::ph_image_manager_request_image_async(
                self.raw.as_ptr(),
                asset_id.as_ptr(),
                request_json.as_ptr(),
                image_result_callback,
                ctx,
            );
        }
        Ok(RequestImageFuture { inner: future })
    }

    /// Asynchronously request raw image data for `asset`.
    pub fn request_image_data(
        &self,
        asset: &PHAsset,
        request: &PHImageRequest,
    ) -> Result<RequestImageDataFuture, PhotoKitError> {
        let asset_id = cstring_from_str(&asset.local_identifier, "asset local identifier")?;
        let request_json = json_cstring(request, "PHImageRequest")?;
        let (future, ctx) = AsyncCompletion::create();
        unsafe {
            crate::ffi::ph_image_manager_request_image_data_async(
                self.raw.as_ptr(),
                asset_id.as_ptr(),
                request_json.as_ptr(),
                image_data_result_callback,
                ctx,
            );
        }
        Ok(RequestImageDataFuture { inner: future })
    }
}

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

/// Async wrapper for `PHLivePhotoEditingContext` completion-handler APIs.
#[derive(Debug, Clone, Copy)]
pub struct AsyncPHLivePhotoEditingContext;

impl AsyncPHLivePhotoEditingContext {
    /// Asynchronously save a live photo to `output`.
    pub fn save_live_photo(
        context: &PHLivePhotoEditingContext,
        output: &PHContentEditingOutput,
    ) -> SaveLivePhotoFuture {
        let (future, ctx) = AsyncCompletion::create();
        unsafe {
            crate::ffi::ph_live_photo_editing_context_save_async(
                context.as_raw(),
                output.as_raw(),
                live_photo_save_callback,
                ctx,
            );
        }
        SaveLivePhotoFuture { inner: future }
    }

    /// Asynchronously prepare a live photo for playback at the given target size.
    pub fn prepare_live_photo(
        context: &PHLivePhotoEditingContext,
        target_width: f64,
        target_height: f64,
    ) -> PrepareLivePhotoFuture {
        let (future, ctx) = AsyncCompletion::create();
        unsafe {
            crate::ffi::ph_live_photo_editing_context_prepare_async(
                context.as_raw(),
                target_width,
                target_height,
                live_photo_prepare_callback,
                ctx,
            );
        }
        PrepareLivePhotoFuture { inner: future }
    }
}