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
#![allow(clippy::unsafe_derive_deserialize)]

use std::ops::Deref;

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

use crate::asset::PHCoordinate;
use crate::cloud_identifier::PHCloudIdentifier;
use crate::ffi;
use crate::geometry::{PHColor, PHRect};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Wraps `PHProjectInfo.CreationSource`.
pub enum PHProjectCreationSource {
    /// Case of `PHProjectCreationSource`.
    Undefined,
    /// Case of `PHProjectCreationSource`.
    UserSelection,
    /// Case of `PHProjectCreationSource`.
    Album,
    /// Case of `PHProjectCreationSource`.
    Memory,
    /// Case of `PHProjectCreationSource`.
    Moment,
    /// Case of `PHProjectCreationSource`.
    Project,
    /// Case of `PHProjectCreationSource`.
    ProjectBook,
    /// Case of `PHProjectCreationSource`.
    ProjectCalendar,
    /// Case of `PHProjectCreationSource`.
    ProjectCard,
    /// Case of `PHProjectCreationSource`.
    ProjectPrintOrder,
    /// Case of `PHProjectCreationSource`.
    ProjectSlideshow,
    /// Case of `PHProjectCreationSource`.
    ProjectExtension,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Wraps `PHProjectSection.SectionType`.
pub enum PHProjectSectionType {
    /// Case of `PHProjectSectionType`.
    Undefined,
    /// Case of `PHProjectSectionType`.
    Cover,
    /// Case of `PHProjectSectionType`.
    Content,
    /// Case of `PHProjectSectionType`.
    Auxiliary,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Wraps `PHProjectTextElement.ElementType`.
pub enum PHProjectTextElementType {
    /// Case of `PHProjectTextElementType`.
    Body,
    /// Case of `PHProjectTextElementType`.
    Title,
    /// Case of `PHProjectTextElementType`.
    Subtitle,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Wraps `PHProjectElement`.
pub struct PHProjectElement {
    /// Corresponds to `PHProjectElement.weight`.
    pub weight: f64,
    /// Corresponds to `PHProjectElement.placement`.
    pub placement: Option<PHRect>,
}

impl PHProjectElement {
    /// Returns whether `PHProjectElement` is available on the current SDK.
    pub fn is_available() -> bool {
        unsafe { ffi::ph_project_element_is_available() == ffi::status::OK }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Wraps `PHProjectRegionOfInterest`.
pub struct PHProjectRegionOfInterest {
    /// Corresponds to `PHProjectRegionOfInterest.rect`.
    pub rect: PHRect,
    /// Corresponds to `PHProjectRegionOfInterest.weight`.
    pub weight: f64,
    /// Corresponds to `PHProjectRegionOfInterest.quality`.
    pub quality: f64,
    /// Corresponds to `PHProjectRegionOfInterest.identifier`.
    pub identifier: String,
}

impl PHProjectRegionOfInterest {
    /// Returns whether `PHProjectRegionOfInterest` is available on the current SDK.
    pub fn is_available() -> bool {
        unsafe { ffi::ph_project_region_of_interest_is_available() == ffi::status::OK }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Wraps map-annotation details exposed through `PHProjectMapElement`.
pub struct PHProjectMapAnnotation {
    /// Corresponds to `MKAnnotation.coordinate`.
    pub coordinate: PHCoordinate,
    /// Corresponds to `MKAnnotation.title`.
    pub title: Option<String>,
    /// Corresponds to `MKAnnotation.subtitle`.
    pub subtitle: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Wraps `PHProjectAssetElement`.
pub struct PHProjectAssetElement {
    #[serde(flatten)]
    /// Corresponds to the base `PHProjectElement` state.
    pub element: PHProjectElement,
    /// Corresponds to `PHProjectAssetElement.cloudAssetIdentifier`.
    pub cloud_asset_identifier: PHCloudIdentifier,
    /// Corresponds to `PHProjectAssetElement.annotation`.
    pub annotation: String,
    /// Corresponds to `PHProjectAssetElement.cropRect`.
    pub crop_rect: PHRect,
    #[serde(default)]
    /// Corresponds to `PHProjectAssetElement.regionsOfInterest`.
    pub regions_of_interest: Vec<PHProjectRegionOfInterest>,
    /// Corresponds to `PHProjectAssetElement.horizontallyFlipped`.
    pub horizontally_flipped: bool,
    /// Corresponds to `PHProjectAssetElement.verticallyFlipped`.
    pub vertically_flipped: bool,
}

impl PHProjectAssetElement {
    /// Returns whether `PHProjectAssetElement` is available on the current SDK.
    pub fn is_available() -> bool {
        unsafe { ffi::ph_project_asset_element_is_available() == ffi::status::OK }
    }
}

impl Deref for PHProjectAssetElement {
    type Target = PHProjectElement;

    fn deref(&self) -> &Self::Target {
        &self.element
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Wraps `PHProjectTextElement`.
pub struct PHProjectTextElement {
    #[serde(flatten)]
    /// Corresponds to the base `PHProjectElement` state.
    pub element: PHProjectElement,
    /// Corresponds to `PHProjectTextElement.text`.
    pub text: String,
    /// Corresponds to `PHProjectTextElement.attributedText` serialized as RTF data.
    pub attributed_text_rtf_base64: Option<String>,
    /// Corresponds to `PHProjectTextElement.textElementType`.
    pub text_element_type: PHProjectTextElementType,
}

impl PHProjectTextElement {
    /// Returns whether `PHProjectTextElement` is available on the current SDK.
    pub fn is_available() -> bool {
        unsafe { ffi::ph_project_text_element_is_available() == ffi::status::OK }
    }

    /// Decodes the serialized RTF payload, if any.
    pub fn attributed_text_rtf_data(&self) -> Option<Vec<u8>> {
        self.attributed_text_rtf_base64.as_ref().and_then(|value| {
            base64::engine::general_purpose::STANDARD
                .decode(value.as_bytes())
                .ok()
        })
    }
}

impl Deref for PHProjectTextElement {
    type Target = PHProjectElement;

    fn deref(&self) -> &Self::Target {
        &self.element
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Wraps `PHProjectJournalEntryElement`.
pub struct PHProjectJournalEntryElement {
    #[serde(flatten)]
    /// Corresponds to the base `PHProjectElement` state.
    pub element: PHProjectElement,
    /// Corresponds to `PHProjectJournalEntryElement.date`.
    pub date: String,
    /// Corresponds to `PHProjectJournalEntryElement.assetElement`.
    pub asset_element: Option<PHProjectAssetElement>,
    /// Corresponds to `PHProjectJournalEntryElement.textElement`.
    pub text_element: Option<PHProjectTextElement>,
}

impl PHProjectJournalEntryElement {
    /// Returns whether `PHProjectJournalEntryElement` is available on the current SDK.
    pub fn is_available() -> bool {
        unsafe { ffi::ph_project_journal_entry_element_is_available() == ffi::status::OK }
    }
}

impl Deref for PHProjectJournalEntryElement {
    type Target = PHProjectElement;

    fn deref(&self) -> &Self::Target {
        &self.element
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Wraps `PHProjectMapElement`.
pub struct PHProjectMapElement {
    #[serde(flatten)]
    /// Corresponds to the base `PHProjectElement` state.
    pub element: PHProjectElement,
    /// Corresponds to `PHProjectMapElement.mapType`.
    pub map_type: u64,
    /// Corresponds to `PHProjectMapElement.centerCoordinate`.
    pub center_coordinate: PHCoordinate,
    /// Corresponds to `PHProjectMapElement.heading`.
    pub heading: f64,
    /// Corresponds to `PHProjectMapElement.pitch`.
    pub pitch: f64,
    /// Corresponds to `PHProjectMapElement.altitude`.
    pub altitude: f64,
    #[serde(default)]
    /// Corresponds to `PHProjectMapElement.annotations`.
    pub annotations: Vec<PHProjectMapAnnotation>,
}

impl PHProjectMapElement {
    /// Returns whether `PHProjectMapElement` is available on the current SDK.
    pub fn is_available() -> bool {
        unsafe { ffi::ph_project_map_element_is_available() == ffi::status::OK }
    }
}

impl Deref for PHProjectMapElement {
    type Target = PHProjectElement;

    fn deref(&self) -> &Self::Target {
        &self.element
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", content = "payload", rename_all = "camelCase")]
/// Wraps the heterogenous `PHProjectElement` array exposed by `PHProjectSectionContent`.
pub enum PHProjectSectionElement {
    /// Case wrapping `PHProjectAssetElement`.
    Asset(PHProjectAssetElement),
    /// Case wrapping `PHProjectTextElement`.
    Text(PHProjectTextElement),
    /// Case wrapping `PHProjectJournalEntryElement`.
    JournalEntry(PHProjectJournalEntryElement),
    /// Case wrapping `PHProjectMapElement`.
    Map(PHProjectMapElement),
}

impl PHProjectSectionElement {
    /// Returns the base `PHProjectElement` shared by the variant.
    pub fn element(&self) -> &PHProjectElement {
        match self {
            Self::Asset(value) => &value.element,
            Self::Text(value) => &value.element,
            Self::JournalEntry(value) => &value.element,
            Self::Map(value) => &value.element,
        }
    }

    /// Returns the wrapped `PHProjectAssetElement`, if this is an asset entry.
    pub fn as_asset_element(&self) -> Option<&PHProjectAssetElement> {
        match self {
            Self::Asset(value) => Some(value),
            _ => None,
        }
    }

    /// Returns the wrapped `PHProjectTextElement`, if this is a text entry.
    pub fn as_text_element(&self) -> Option<&PHProjectTextElement> {
        match self {
            Self::Text(value) => Some(value),
            _ => None,
        }
    }

    /// Returns the wrapped `PHProjectJournalEntryElement`, if this is a journal entry.
    pub fn as_journal_entry_element(&self) -> Option<&PHProjectJournalEntryElement> {
        match self {
            Self::JournalEntry(value) => Some(value),
            _ => None,
        }
    }

    /// Returns the wrapped `PHProjectMapElement`, if this is a map entry.
    pub fn as_map_element(&self) -> Option<&PHProjectMapElement> {
        match self {
            Self::Map(value) => Some(value),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Wraps `PHProjectSectionContent`.
pub struct PHProjectSectionContent {
    #[serde(default)]
    /// Corresponds to `PHProjectSectionContent.elements`.
    pub elements: Vec<PHProjectSectionElement>,
    /// Corresponds to `PHProjectSectionContent.numberOfColumns`.
    pub number_of_columns: i64,
    /// Corresponds to `PHProjectSectionContent.aspectRatio`.
    pub aspect_ratio: f64,
    #[serde(default)]
    /// Corresponds to `PHProjectSectionContent.cloudAssetIdentifiers`.
    pub cloud_asset_identifiers: Vec<PHCloudIdentifier>,
    /// Corresponds to `PHProjectSectionContent.backgroundColor`.
    pub background_color: Option<PHColor>,
}

impl PHProjectSectionContent {
    /// Returns whether `PHProjectSectionContent` is available on the current SDK.
    pub fn is_available() -> bool {
        unsafe { ffi::ph_project_section_content_is_available() == ffi::status::OK }
    }

    /// Returns all asset elements contained in the section content.
    pub fn asset_elements(&self) -> Vec<&PHProjectAssetElement> {
        self.elements
            .iter()
            .filter_map(PHProjectSectionElement::as_asset_element)
            .collect()
    }

    /// Returns all text elements contained in the section content.
    pub fn text_elements(&self) -> Vec<&PHProjectTextElement> {
        self.elements
            .iter()
            .filter_map(PHProjectSectionElement::as_text_element)
            .collect()
    }

    /// Returns all journal-entry elements contained in the section content.
    pub fn journal_entry_elements(&self) -> Vec<&PHProjectJournalEntryElement> {
        self.elements
            .iter()
            .filter_map(PHProjectSectionElement::as_journal_entry_element)
            .collect()
    }

    /// Returns all map elements contained in the section content.
    pub fn map_elements(&self) -> Vec<&PHProjectMapElement> {
        self.elements
            .iter()
            .filter_map(PHProjectSectionElement::as_map_element)
            .collect()
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Wraps `PHProjectSection`.
pub struct PHProjectSection {
    #[serde(default)]
    /// Corresponds to `PHProjectSection.sectionContents`.
    pub section_contents: Vec<PHProjectSectionContent>,
    /// Corresponds to `PHProjectSection.sectionType`.
    pub section_type: PHProjectSectionType,
    /// Corresponds to `PHProjectSection.title`.
    pub title: String,
}

impl PHProjectSection {
    /// Returns whether `PHProjectSection` is available on the current SDK.
    pub fn is_available() -> bool {
        unsafe { ffi::ph_project_section_is_available() == ffi::status::OK }
    }

    /// Returns the least-curated section content suggested by PhotosUI.
    pub fn preferred_section_content(&self) -> Option<&PHProjectSectionContent> {
        self.section_contents.first()
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Wraps `PHProjectInfo`.
pub struct PHProjectInfo {
    /// Corresponds to `PHProjectInfo.creationSource`.
    pub creation_source: PHProjectCreationSource,
    /// Corresponds to `PHProjectInfo.projectType`.
    pub project_type: String,
    #[serde(default)]
    /// Corresponds to `PHProjectInfo.sections`.
    pub sections: Vec<PHProjectSection>,
    /// Corresponds to `PHProjectInfo.brandingEnabled`.
    pub branding_enabled: bool,
    /// Corresponds to `PHProjectInfo.pageNumbersEnabled`.
    pub page_numbers_enabled: bool,
    /// Corresponds to `PHProjectInfo.productIdentifier`.
    pub product_identifier: Option<String>,
    /// Corresponds to `PHProjectInfo.themeIdentifier`.
    pub theme_identifier: Option<String>,
}

impl PHProjectInfo {
    /// Returns whether `PHProjectInfo` is available on the current SDK.
    pub fn is_available() -> bool {
        unsafe { ffi::ph_project_info_is_available() == ffi::status::OK }
    }

    /// Returns all cloud asset identifiers referenced anywhere in the project info.
    pub fn all_cloud_asset_identifiers(&self) -> Vec<PHCloudIdentifier> {
        self.sections
            .iter()
            .flat_map(|section| {
                section
                    .section_contents
                    .iter()
                    .flat_map(|content| content.cloud_asset_identifiers.clone())
            })
            .collect()
    }
}