cloudkit 0.3.5

Safe Rust bindings for Apple's CloudKit framework — iCloud databases and sync 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
use core::ffi::{c_char, c_void};
use std::collections::BTreeMap;
use std::ffi::{CStr, CString};

use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};

use crate::error::{CloudKitError, ErrorPayload};
use crate::ffi;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKRecordZoneIDPayload {
    pub zone_name: String,
    pub owner_name: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKRecordIDPayload {
    pub record_name: String,
    #[serde(rename = "zoneID")]
    pub zone_id: CKRecordZoneIDPayload,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKReferencePayload {
    #[serde(rename = "recordID")]
    pub record_id: CKRecordIDPayload,
    pub action: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKAssetPayload {
    pub file_url: String,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) enum RecordValueKind {
    String,
    Int,
    Double,
    Bool,
    Bytes,
    Date,
    Asset,
    Reference,
    Array,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKRecordValuePayload {
    pub kind: RecordValueKind,
    pub string_value: Option<String>,
    pub int_value: Option<i64>,
    pub double_value: Option<f64>,
    pub bool_value: Option<bool>,
    pub bytes_value: Option<Vec<u8>>,
    pub date_value: Option<String>,
    pub asset_value: Option<CKAssetPayload>,
    pub reference_value: Option<CKReferencePayload>,
    pub array_value: Option<Vec<CKRecordValuePayload>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKRecordPayload {
    pub record_type: String,
    #[serde(rename = "recordID")]
    pub record_id: CKRecordIDPayload,
    pub fields: BTreeMap<String, CKRecordValuePayload>,
    pub encoded_system_fields: Vec<u8>,
    pub record_change_tag: Option<String>,
    #[serde(rename = "creatorUserRecordID")]
    pub creator_user_record_id: Option<CKRecordIDPayload>,
    pub creation_date: Option<String>,
    #[serde(rename = "lastModifiedUserRecordID")]
    pub last_modified_user_record_id: Option<CKRecordIDPayload>,
    pub modification_date: Option<String>,
    pub parent: Option<CKReferencePayload>,
    pub share: Option<CKReferencePayload>,
    pub changed_keys: Vec<String>,
    pub all_tokens: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKRecordZonePayload {
    #[serde(rename = "zoneID")]
    pub zone_id: CKRecordZoneIDPayload,
    pub capabilities: u64,
    pub share: Option<CKReferencePayload>,
    pub encryption_scope: Option<i32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct SortDescriptorPayload {
    pub key: String,
    pub ascending: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKQueryPayload {
    pub record_type: String,
    pub predicate_format: String,
    pub sort_descriptors: Vec<SortDescriptorPayload>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKNotificationInfoPayload {
    pub alert_body: Option<String>,
    pub alert_localization_key: Option<String>,
    pub alert_localization_args: Option<Vec<String>>,
    pub title: Option<String>,
    pub title_localization_key: Option<String>,
    pub title_localization_args: Option<Vec<String>>,
    pub subtitle: Option<String>,
    pub subtitle_localization_key: Option<String>,
    pub subtitle_localization_args: Option<Vec<String>>,
    pub alert_action_localization_key: Option<String>,
    pub alert_launch_image: Option<String>,
    pub sound_name: Option<String>,
    pub desired_keys: Option<Vec<String>>,
    pub should_badge: bool,
    pub should_send_content_available: bool,
    pub should_send_mutable_content: bool,
    pub category: Option<String>,
    #[serde(rename = "collapseIDKey")]
    pub collapse_id_key: Option<String>,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) enum CKSubscriptionPayloadKind {
    Query,
    RecordZone,
    Database,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKSubscriptionPayload {
    pub kind: CKSubscriptionPayloadKind,
    #[serde(rename = "subscriptionID")]
    pub subscription_id: String,
    #[serde(rename = "subscriptionType")]
    pub subscription_type: i32,
    #[serde(rename = "notificationInfo")]
    pub notification_info: Option<CKNotificationInfoPayload>,
    pub record_type: Option<String>,
    pub predicate_format: Option<String>,
    #[serde(rename = "zoneID")]
    pub zone_id: Option<CKRecordZoneIDPayload>,
    #[serde(rename = "querySubscriptionOptions")]
    pub query_subscription_options: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKPersonNameComponentsPayload {
    pub name_prefix: Option<String>,
    pub given_name: Option<String>,
    pub middle_name: Option<String>,
    pub family_name: Option<String>,
    pub name_suffix: Option<String>,
    pub nickname: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKUserIdentityLookupInfoPayload {
    pub email_address: Option<String>,
    pub phone_number: Option<String>,
    #[serde(rename = "userRecordID")]
    pub user_record_id: Option<CKRecordIDPayload>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKUserIdentityPayload {
    pub archived_data: Vec<u8>,
    #[serde(rename = "userRecordID")]
    pub user_record_id: Option<CKRecordIDPayload>,
    pub lookup_info: Option<CKUserIdentityLookupInfoPayload>,
    pub name_components: Option<CKPersonNameComponentsPayload>,
    pub hasi_cloud_account: bool,
    pub contact_identifiers: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKShareParticipantPayload {
    pub archived_data: Vec<u8>,
    pub user_identity: CKUserIdentityPayload,
    pub role: Option<i32>,
    pub permission: i32,
    pub acceptance_status: i32,
    #[serde(rename = "participantID")]
    pub participant_id: String,
    #[serde(rename = "isApprovedRequester")]
    pub is_approved_requester: Option<bool>,
    #[serde(rename = "dateAddedToShare")]
    pub date_added_to_share: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKSharePayload {
    #[serde(rename = "shareRecord")]
    pub share_record: CKRecordPayload,
    #[serde(rename = "rootRecord")]
    pub root_record: Option<CKRecordPayload>,
    #[serde(rename = "zoneID")]
    pub zone_id: CKRecordZoneIDPayload,
    pub public_permission: i32,
    pub url: Option<String>,
    pub participants: Vec<CKShareParticipantPayload>,
    pub owner: Option<CKShareParticipantPayload>,
    #[serde(rename = "currentUserParticipant")]
    pub current_user_participant: Option<CKShareParticipantPayload>,
    pub title: Option<String>,
    #[serde(rename = "thumbnailImageData")]
    pub thumbnail_image_data: Option<Vec<u8>>,
    #[serde(rename = "shareType")]
    pub share_type: Option<String>,
    #[serde(rename = "allowsAccessRequests")]
    pub allows_access_requests: Option<bool>,
    #[serde(rename = "isZoneWide")]
    pub is_zone_wide: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKServerChangeTokenPayload {
    pub archived_data: Vec<u8>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKQueryCursorPayload {
    pub archived_data: Vec<u8>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKFetchedQueryResultsPayload {
    pub records: Vec<CKRecordPayload>,
    pub matches: Vec<CKQueryMatchResultPayload>,
    pub cursor: Option<CKQueryCursorPayload>,
    #[serde(rename = "operationError")]
    pub operation_error: Option<ErrorPayload>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKModifyRecordsOperationPayload {
    pub records_to_save: Vec<CKRecordPayload>,
    #[serde(rename = "recordIDsToDelete")]
    pub record_ids_to_delete: Vec<CKRecordIDPayload>,
    pub save_policy: i32,
    pub atomic: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKRecordSaveResultPayload {
    #[serde(rename = "recordID")]
    pub record_id: CKRecordIDPayload,
    pub record: Option<CKRecordPayload>,
    pub error: Option<ErrorPayload>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKRecordDeleteResultPayload {
    #[serde(rename = "recordID")]
    pub record_id: CKRecordIDPayload,
    pub error: Option<ErrorPayload>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKModifyRecordsResultPayload {
    pub saved_records: Vec<CKRecordPayload>,
    #[serde(rename = "deletedRecordIDs")]
    pub deleted_record_ids: Vec<CKRecordIDPayload>,
    #[serde(rename = "saveResults")]
    pub save_results: Vec<CKRecordSaveResultPayload>,
    #[serde(rename = "deleteResults")]
    pub delete_results: Vec<CKRecordDeleteResultPayload>,
    #[serde(rename = "operationError")]
    pub operation_error: Option<ErrorPayload>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKQueryOperationPayload {
    pub query: CKQueryPayload,
    #[serde(rename = "zoneID")]
    pub zone_id: Option<CKRecordZoneIDPayload>,
    pub desired_keys: Option<Vec<String>>,
    pub results_limit: Option<usize>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKQueryMatchResultPayload {
    #[serde(rename = "recordID")]
    pub record_id: CKRecordIDPayload,
    pub record: Option<CKRecordPayload>,
    pub error: Option<ErrorPayload>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKQueryOperationResultPayload {
    pub records: Vec<CKRecordPayload>,
    pub matches: Vec<CKQueryMatchResultPayload>,
    #[serde(rename = "cursorReturned")]
    pub cursor_returned: bool,
    #[serde(rename = "operationError")]
    pub operation_error: Option<ErrorPayload>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKFetchRecordsOperationPayload {
    #[serde(rename = "recordIDs")]
    pub record_ids: Vec<CKRecordIDPayload>,
    pub desired_keys: Option<Vec<String>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKRecordResultPayload {
    #[serde(rename = "recordID")]
    pub record_id: CKRecordIDPayload,
    pub record: Option<CKRecordPayload>,
    pub error: Option<ErrorPayload>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKFetchRecordsResultPayload {
    pub records: Vec<CKRecordPayload>,
    pub results: Vec<CKRecordResultPayload>,
    #[serde(rename = "operationError")]
    pub operation_error: Option<ErrorPayload>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKFetchDatabaseChangesOperationPayload {
    #[serde(rename = "previousServerChangeToken")]
    pub previous_server_change_token: Option<CKServerChangeTokenPayload>,
    pub results_limit: Option<usize>,
    #[serde(rename = "fetchAllChanges")]
    pub fetch_all_changes: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKFetchDatabaseChangesResultPayload {
    #[serde(rename = "changedZoneIDs")]
    pub changed_zone_ids: Vec<CKRecordZoneIDPayload>,
    #[serde(rename = "deletedZoneIDs")]
    pub deleted_zone_ids: Vec<CKRecordZoneIDPayload>,
    #[serde(rename = "purgedZoneIDs")]
    pub purged_zone_ids: Vec<CKRecordZoneIDPayload>,
    #[serde(rename = "encryptedDataResetZoneIDs")]
    pub encrypted_data_reset_zone_ids: Vec<CKRecordZoneIDPayload>,
    #[serde(rename = "updatedServerChangeTokens")]
    pub updated_server_change_tokens: Vec<CKServerChangeTokenPayload>,
    #[serde(rename = "serverChangeToken")]
    pub server_change_token: Option<CKServerChangeTokenPayload>,
    #[serde(rename = "moreComing")]
    pub more_coming: bool,
    #[serde(rename = "operationError")]
    pub operation_error: Option<ErrorPayload>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKFetchRecordZoneChangesConfigurationPayload {
    #[serde(rename = "previousServerChangeToken")]
    pub previous_server_change_token: Option<CKServerChangeTokenPayload>,
    pub results_limit: Option<usize>,
    pub desired_keys: Option<Vec<String>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKFetchRecordZoneChangesConfigurationEntryPayload {
    #[serde(rename = "zoneID")]
    pub zone_id: CKRecordZoneIDPayload,
    pub configuration: CKFetchRecordZoneChangesConfigurationPayload,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKDeletedRecordPayload {
    #[serde(rename = "recordID")]
    pub record_id: CKRecordIDPayload,
    pub record_type: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKFetchRecordZoneResultPayload {
    #[serde(rename = "zoneID")]
    pub zone_id: CKRecordZoneIDPayload,
    pub changed_records: Vec<CKRecordPayload>,
    pub deleted_records: Vec<CKDeletedRecordPayload>,
    #[serde(rename = "updatedServerChangeTokens")]
    pub updated_server_change_tokens: Vec<CKServerChangeTokenPayload>,
    #[serde(rename = "serverChangeToken")]
    pub server_change_token: Option<CKServerChangeTokenPayload>,
    #[serde(rename = "clientChangeTokenData")]
    pub client_change_token_data: Option<Vec<u8>>,
    #[serde(rename = "moreComing")]
    pub more_coming: bool,
    #[serde(rename = "zoneError")]
    pub zone_error: Option<ErrorPayload>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKFetchRecordZoneChangesOperationPayload {
    pub zones: Vec<CKFetchRecordZoneChangesConfigurationEntryPayload>,
    #[serde(rename = "fetchAllChanges")]
    pub fetch_all_changes: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKFetchRecordZoneChangesResultPayload {
    pub zones: Vec<CKFetchRecordZoneResultPayload>,
    #[serde(rename = "operationError")]
    pub operation_error: Option<ErrorPayload>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct CKShareMetadataPayload {
    pub archived_data: Vec<u8>,
    pub container_identifier: String,
    pub share: CKSharePayload,
    #[serde(rename = "hierarchicalRootRecordID")]
    pub hierarchical_root_record_id: Option<CKRecordIDPayload>,
    pub participant_role: Option<i32>,
    pub participant_status: i32,
    pub participant_permission: i32,
    pub owner_identity: CKUserIdentityPayload,
    pub root_record: Option<CKRecordPayload>,
    pub participant_type: Option<i32>,
    #[serde(rename = "rootRecordID")]
    pub root_record_id: Option<CKRecordIDPayload>,
}

pub(crate) fn cstring_from_str(value: &str, context: &str) -> Result<CString, CloudKitError> {
    CString::new(value).map_err(|error| {
        CloudKitError::bridge(
            -1,
            format!("{context} contains an interior NUL byte: {error}"),
        )
    })
}

pub(crate) fn optional_cstring_from_str(
    value: Option<&str>,
    context: &str,
) -> Result<Option<CString>, CloudKitError> {
    value
        .map(|value| cstring_from_str(value, context))
        .transpose()
}

pub(crate) fn json_cstring<T: Serialize>(
    value: &T,
    context: &str,
) -> Result<CString, CloudKitError> {
    let json = serde_json::to_string(value).map_err(|error| {
        CloudKitError::bridge(-1, format!("failed to encode {context} as JSON: {error}"))
    })?;
    cstring_from_str(&json, context)
}

pub(crate) fn opt_cstring_ptr(value: &Option<CString>) -> *const c_char {
    value
        .as_ref()
        .map_or(core::ptr::null(), |value| value.as_c_str().as_ptr())
}

/// # Safety
/// `ptr` must be either null or a valid pointer to a bridge-allocated null-terminated C string; this function frees the string via `ck_string_free`.
pub(crate) unsafe fn take_string(ptr: *mut c_char) -> Option<String> {
    if ptr.is_null() {
        return None;
    }
    let string = CStr::from_ptr(ptr).to_string_lossy().into_owned();
    ffi::ck_string_free(ptr);
    Some(string)
}

pub(crate) fn parse_json_str<T: DeserializeOwned>(
    json: &str,
    context: &str,
) -> Result<T, CloudKitError> {
    serde_json::from_str(json).map_err(|error| {
        CloudKitError::bridge(
            -1,
            format!("failed to parse {context} JSON: {error}; payload={json}"),
        )
    })
}

/// # Safety
/// `ptr` must be either null or a valid pointer to a bridge-allocated null-terminated C string; this function frees the string via `ck_string_free`.
pub(crate) unsafe fn parse_json_ptr<T: DeserializeOwned>(
    ptr: *mut c_char,
    context: &str,
) -> Result<T, CloudKitError> {
    let json = take_string(ptr)
        .ok_or_else(|| CloudKitError::bridge(-1, format!("missing JSON payload for {context}")))?;
    parse_json_str(&json, context)
}

/// # Safety
/// `ptr` must be either null or a valid pointer to a bridge-allocated null-terminated C string; this function frees the string via `ck_string_free`.
pub(crate) unsafe fn parse_error_ptr(ptr: *mut c_char) -> CloudKitError {
    if ptr.is_null() {
        return CloudKitError::bridge(-2, "CloudKit bridge returned an error without payload");
    }
    let json = CStr::from_ptr(ptr).to_string_lossy().into_owned();
    ffi::ck_string_free(ptr);
    parse_error_json_str(&json)
}

/// # Safety
/// `ptr` must be null or point to a valid null-terminated C string for the duration of this call; this function borrows the string and does not free it.
pub(crate) unsafe fn parse_borrowed_error_ptr(ptr: *const c_char) -> CloudKitError {
    if ptr.is_null() {
        return CloudKitError::bridge(-2, "CloudKit bridge returned an error without payload");
    }
    let json = CStr::from_ptr(ptr).to_string_lossy().into_owned();
    parse_error_json_str(&json)
}

pub(crate) fn parse_error_json_str(json: &str) -> CloudKitError {
    match serde_json::from_str::<ErrorPayload>(json) {
        Ok(payload) => CloudKitError::from_payload(payload),
        Err(error) => CloudKitError::bridge(
            -1,
            format!("failed to parse CloudKit error payload: {error}; payload={json}"),
        ),
    }
}

/// # Safety
/// `err_msg` must be either null or a valid pointer to a bridge-allocated null-terminated C string; when non-null it is consumed via `parse_error_ptr`.
pub(crate) unsafe fn error_from_status(status: i32, err_msg: *mut c_char) -> CloudKitError {
    if !err_msg.is_null() {
        return parse_error_ptr(err_msg);
    }
    let message = match status {
        ffi::status::INVALID_ARGUMENT => "invalid argument",
        ffi::status::TIMED_OUT => "timed out waiting for CloudKit",
        _ => "CloudKit bridge failure",
    };
    CloudKitError::bridge(i64::from(status), message)
}

pub(crate) fn box_closure<F>(closure: F) -> *mut c_void
where
    F: Send + 'static,
{
    Box::into_raw(Box::new(closure)).cast::<c_void>()
}