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
//! Manually Generated DittoFFI Bindings
//!
//! These must be updated when dittoffi changes

use std::{
    fmt,
    os::raw::{c_double, c_int, c_uint, c_ulonglong, c_void},
};

use ::safer_ffi::prelude::*;

#[repr(C)]
pub struct Ditto {
    _private: [u8; 0],
}

macro_rules! opaque_wrap {(
    $pub:vis
    type $TypeName:ident ~ repr_c::Box<$Pointee:ty>;
    $(
        fn drop $($rest:tt)*
    )?
) => (
    #[repr(transparent)]
    $pub
    struct $TypeName {
        ptr: ::core::ptr::NonNull<$Pointee>,
    }

    impl ::core::ops::DerefMut for $TypeName {
        fn deref_mut (self: &'_ mut Self) -> &'_ mut $Pointee
        {
            impl ::core::ops::Deref for $TypeName {
                type Target = $Pointee;

                fn deref (self: &'_ Self) -> &'_ $Pointee
                {
                    unsafe {
                        self.ptr.as_ref()
                    }
                }
            }

            unsafe {
                self.ptr.as_mut()
            }
        }
    }

    unsafe impl ::core::marker::Send for $TypeName {}
    unsafe impl ::core::marker::Sync for $TypeName {}

    $(
        impl Drop for $TypeName {
            fn drop $($rest)*
        }
    )?
)}

opaque_wrap! {
    pub type BoxedDitto ~ repr_c::Box<Ditto>;
    fn drop(&mut self) {
        unsafe {
            ditto_drop(self);
            // At this point, we *could* ditto_free, but things could go south
            // especially as ditto_drop basically does all the actual clean up work
        }
    }
}

impl fmt::Debug for BoxedDitto {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Ditto").finish()
    }
}

opaque_wrap! {
    pub type BoxedAuthClient ~ repr_c::Box<AuthClient>;
    fn drop(&mut self) {
        unsafe {ditto_auth_client_free(&mut **self);}
    }
}

#[repr(C)]
pub struct UninitializedDitto {
    _private: [u8; 0],
}

opaque_wrap! {
    pub type BoxedUninitializedDitto ~ repr_c::Box<UninitializedDitto>;
}

#[repr(C)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Platform {
    Windows,
    Mac,
    Ios,
    Android,
    Linux,
    Unknown,
}

#[repr(C)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(dead_code)]
pub enum Language {
    Swift,
    ObjectiveC,
    CPlusPlus,
    CSharp,
    Javascript,
    Unknown,
    Rust,
}

#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum CLogLevel {
    // Starts at 1 to match the Rust log levels which have an `Off` case with
    // value 0
    Error = 1,
    Warning,
    Info,
    Debug,
    Verbose,
}

#[repr(C)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WebSocketMode {
    Enabled,
    Disabled,
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct StaticTcpClientPlatformHandle {
    _private: [u8; 0],
}

opaque_wrap! {
    pub type BoxedStaticTcpClient ~ repr_c::Box<StaticTcpClientPlatformHandle>;
    fn drop(&mut self) {
        unsafe {static_tcp_client_free_handle(&mut **self);}
    }
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct WebsocketClientPlatformHandle {
    _private: [u8; 0],
}

opaque_wrap! {
    pub type BoxedWebsocketClient ~ repr_c::Box<WebsocketClientPlatformHandle>;
    fn drop(&mut self) {
        unsafe {websocket_client_free_handle(&mut **self);}
    }
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct BleClientPlatformHandle {
    _private: [u8; 0],
}

opaque_wrap! {
    pub type BoxedBleClient ~ repr_c::Box<BleClientPlatformHandle>;
    fn drop(&mut self) {
        unsafe {ble_client_free_handle(&mut **self);}
    }
}

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct BleServerPlatformHandle {
    _private: [u8; 0],
}

opaque_wrap! {
    pub type BoxedBleServer ~ repr_c::Box<BleServerPlatformHandle>;
    fn drop(&mut self) {
        unsafe {ble_server_free_handle(&mut **self);}
    }
}

#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq)]
#[allow(dead_code)]
pub enum LicenseVerificationResult {
    LicenseOk = 0,
    VerificationFailed = -1,
    LicenseExpired = -2,
    UnsupportedFutureVersion = -3,
}

#[repr(C)]
pub struct CWriteTransaction {
    _private: [u8; 0],
}

opaque_wrap! {
    pub type BoxedWriteTransaction ~ repr_c::Box<CWriteTransaction>;
    fn drop(&mut self) {
        unsafe {
            ditto_write_transaction_free(&mut **self);
        }
    }
}

#[repr(C)]
pub struct CReadTransaction {
    _private: [u8; 0],
}

opaque_wrap! {
    pub type BoxedReadTransaction ~ repr_c::Box<CReadTransaction>;
    fn drop(&mut self) {

        let ptr: *mut CReadTransaction  = &mut **self;
        unsafe {ditto_read_transaction_free(ptr)}
    }
}

// This type is NOT opaque
#[repr(C)]
pub struct COrderByParam<'__> {
    pub query_c_str: char_p::Ref<'__>,
    pub direction: QuerySortDirection,
}

#[repr(C)]
#[derive(Copy, Clone, Debug)]
#[allow(dead_code)]
pub enum QuerySortDirection {
    Ascending = 1,
    Descending,
}

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct Document {
    _private: [u8; 0],
}

opaque_wrap! {
    pub type BoxedDocument ~ repr_c::Box<Document>;
    fn drop(&mut self) {
        unsafe {
            ditto_document_free(self);
        }
    }
}

impl fmt::Debug for BoxedDocument {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let c_str = unsafe { ditto_document_id(self) };
        // TODO: FIXME
        f.debug_struct("Document").field("id", &c_str).finish()
    }
}

#[repr(C)]
#[derive(Copy, Clone, Debug)]
#[allow(dead_code)]
pub enum LiveQueryAvailability {
    Always,
    WhenSignalled,
}

#[repr(C)]
#[allow(nonstandard_style)]
pub struct c_cb_params {
    /// Must be freed with `ditto_free_documents`.
    pub documents: repr_c::Vec<BoxedDocument>,
    pub is_initial: bool,
    /// Must be freed with `ditto_free_documents`.
    pub old_documents: Option<repr_c::Vec<BoxedDocument>>,
    /// Must be freed using `ditto_free_indices`.
    pub insertions: Option<c_slice::Box<usize>>,
    /// Must be freed using `ditto_free_indices`.
    pub deletions: Option<c_slice::Box<usize>>,
    /// Must be freed using `ditto_free_indices`.
    pub updates: Option<c_slice::Box<usize>>,
    /// Must be freed using `ditto_free_indices`.
    pub moves: Option<c_slice::Box<usize>>,
}

#[repr(C)]
#[derive(Copy, Clone, Debug)]
#[allow(dead_code)]
pub enum AttachmentFileOperation {
    Copy = 1,
    Move,
}

#[repr(C)]
pub struct Attachment {
    pub id: c_slice::Box<u8>,
    pub len: u64,
    pub handle: BoxedAttachmentHandle,
}

#[repr(C)]
pub struct AttachmentHandle {
    _private: [u8; 0],
}

#[repr(C)]
pub struct AuthClient {
    _private: [u8; 0],
}

opaque_wrap! {
    pub type BoxedAttachmentHandle ~ repr_c::Box<AttachmentHandle>;
    fn drop(&mut self) {
        unsafe {
            ditto_free_attachment_handle(self);
        }
    }
}

impl fmt::Debug for BoxedAttachmentHandle {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("AttachmentHandle").finish()
    }
}

#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub enum StringPrimitiveFormat {
    WithQuotes,
    WithoutQuotes,
}

#[allow(improper_ctypes)]
#[link(name = "dittoffi")]
extern "C" {
    pub fn ditto_init_sdk_version(
        platform: Platform,
        language: Language,
        sdk_semver: char_p::Ref<'_>,
    );

    pub fn uninitialized_ditto_make(working_dir: char_p::Ref<'_>) -> BoxedUninitializedDitto;
    pub fn ditto_make(
        uninit_ditto: BoxedUninitializedDitto,
        auth_client: &'_ AuthClient,
    ) -> BoxedDitto;

    pub fn ditto_logger_init();
    pub fn ditto_logger_enabled(enabled: bool);
    pub fn ditto_logger_enabled_get() -> bool;
    pub fn ditto_logger_emoji_headings_enabled(enabled: bool);
    pub fn ditto_logger_emoji_headings_enabled_get() -> bool;
    pub fn ditto_logger_minimum_log_level_get() -> CLogLevel;
    pub fn ditto_logger_minimum_log_level(log_level: CLogLevel);

    pub fn ditto_shutdown(ditto: &'_ Ditto);
    pub fn ditto_drop(ditto: &'_ mut Ditto);
    pub fn ditto_free(ditto: BoxedDitto);
    pub fn ditto_start_tcp_server(ditto: &'_ Ditto, bind: Option<char_p::Ref<'_>>) -> c_int;
    pub fn ditto_stop_tcp_server(ditto: &'_ Ditto);
    pub fn ditto_start_http_server(
        ditto: &'_ Ditto,
        bind: Option<char_p::Ref<'_>>,
        static_path: Option<char_p::Ref<'_>>,
        enable_websocket: WebSocketMode,
        tls_cert_path: Option<char_p::Ref<'_>>,
        tls_key_path: Option<char_p::Ref<'_>>,
    ) -> c_int;
    pub fn ditto_stop_http_server(ditto: &'_ Ditto);
    pub fn ditto_add_static_tcp_client(
        ditto: &Ditto,
        address: char_p::Ref<'_>,
    ) -> BoxedStaticTcpClient;
    pub fn ditto_add_websocket_client(
        ditto: &'_ Ditto,
        address: char_p::Ref<'_>,
    ) -> BoxedWebsocketClient;
    pub fn ditto_add_internal_ble_client_transport(ditto: &'_ Ditto) -> BoxedBleClient;
    pub fn ditto_add_internal_ble_server_transport(ditto: &'_ Ditto) -> BoxedBleServer;
    pub fn ditto_get_sdk_version() -> char_p::Box;
    pub fn verify_license(
        license: char_p::Ref<'_>,
        out_err_msg: Option<Out<'_, Option<char_p::Box>>>,
    ) -> LicenseVerificationResult;

    pub fn ditto_get_collection_names(
        ditto: &'_ Ditto,
        out_names: Out<'_, Option<repr_c::Vec<char_p::Box>>>,
    ) -> c_int;

    pub fn ditto_queries_hash(
        ditto: &Ditto,
        coll_names: c_slice::Ref<'_, char_p::Ref<'_>>,
        queries: c_slice::Ref<'_, char_p::Ref<'_>>,
        c_hash: Out<'_, u64>,
    ) -> c_int;

    pub fn ditto_queries_hash_mnemonic(
        ditto: &'_ Ditto,
        coll_names: c_slice::Ref<'_, char_p::Ref<'_>>,
        queries: c_slice::Ref<'_, char_p::Ref<'_>>,
        out_mnemonic_hash: Out<'_, Option<char_p::Box>>,
    ) -> c_int;

    pub fn ditto_collection(ditto: &'_ Ditto, name: char_p::Ref<'_>) -> c_int;

    pub fn ditto_collection_insert(
        ditto: &'_ Ditto,
        coll_name: char_p::Ref<'_>,
        transaction: &'_ mut CWriteTransaction,
        document: BoxedDocument,
        id: Out<'_, Option<c_slice::Box<u8>>>,
    ) -> c_int;

    pub fn ditto_collection_update(
        ditto: &'_ Ditto,
        coll_name: char_p::Ref<'_>,
        transaction: &'_ mut CWriteTransaction,
        document: BoxedDocument,
    ) -> c_int;

    pub fn ditto_collection_evict(
        ditto: &'_ Ditto,
        coll_name: char_p::Ref<'_>,
        transaction: &'_ mut CWriteTransaction,
        id: c_slice::Ref<'_, u8>,
        out_evicted: Option<Out<'_, bool>>,
    ) -> c_int;

    pub fn ditto_collection_remove(
        ditto: &'_ Ditto,
        coll_name: char_p::Ref<'_>,
        transaction: &'_ mut CWriteTransaction,
        id: c_slice::Ref<'_, u8>,
        out_removed: Option<Out<'_, bool>>,
    ) -> c_int;

    pub fn ditto_collection_get(
        ditto: &'_ Ditto,
        coll_name: char_p::Ref<'_>,
        id: c_slice::Ref<'_, u8>,
        transaction: &'_ mut CReadTransaction,
        document: Out<'_, Option<BoxedDocument>>,
    ) -> c_int;

    pub fn ditto_collection_exec_query_str<'order_ref, 'order: 'order_ref>(
        ditto: &'_ Ditto,
        coll_name: char_p::Ref<'_>,
        transaction: &'_ mut CWriteTransaction,
        query: char_p::Ref<'_>,
        order_by_params: c_slice::Ref<'order_ref, COrderByParam<'order>>,
        limit: c_int,
        offset: c_uint,
        out_documents: Out<'_, Option<repr_c::Vec<BoxedDocument>>>,
    ) -> c_int;

    pub fn ditto_collection_remove_query_str<'order_ref, 'order: 'order_ref>(
        ditto: &'_ Ditto,
        coll_name: char_p::Ref<'_>,
        transaction: &'_ mut CWriteTransaction,
        query: char_p::Ref<'_>,
        order_by_params: c_slice::Ref<'order_ref, COrderByParam<'order>>,
        limit: c_int,
        offset: c_uint,
        out_ids: Out<'_, repr_c::Vec<c_slice::Box<u8>>>,
    ) -> c_int;

    pub fn ditto_collection_evict_query_str<'order_ref, 'order: 'order_ref>(
        ditto: &'_ Ditto,
        coll_name: char_p::Ref<'_>,
        transaction: &'_ mut CWriteTransaction,
        query: char_p::Ref<'_>,
        order_by_params: c_slice::Ref<'order_ref, COrderByParam<'order>>,
        limit: c_int,
        offset: c_uint,
        out_ids: Out<'_, repr_c::Vec<c_slice::Box<u8>>>,
    ) -> c_int;

    pub fn ditto_read_transaction<'txn>(
        ditto: &'_ Ditto,
        txn: Out<'txn, Option<BoxedReadTransaction>>,
    ) -> c_int;

    pub fn ditto_read_transaction_free(transaction: *mut CReadTransaction);

    pub fn ditto_write_transaction(
        ditto: &'_ Ditto,
        txn: Out<'_, Option<BoxedWriteTransaction>>,
    ) -> c_int;

    pub fn ditto_write_transaction_free(txn: *mut CWriteTransaction);

    pub fn ditto_write_transaction_commit(
        ditto: &Ditto,
        transaction: BoxedWriteTransaction,
    ) -> c_int;

    pub fn ditto_write_transaction_rollback(transaction: BoxedWriteTransaction);

    pub fn ditto_document_new_cbor(
        cbor: c_slice::Ref<'_, u8>,
        id: Option<c_slice::Ref<'_, u8>>,
        _site_id: c_ulonglong,
        document: Out<'_, Option<BoxedDocument>>,
    ) -> c_int;

    pub fn ditto_document_new_cbor_with_timestamp(
        cbor: c_slice::Ref<'_, u8>,
        id: Option<c_slice::Ref<'_, u8>>,
        _site_id: c_ulonglong,
        timestamp: c_uint,
        document: Out<'_, Option<BoxedDocument>>,
    ) -> c_int;

    pub fn ditto_document_id(document: &'_ Document) -> c_slice::Box<u8>;

    pub fn ditto_document_id_query_compatible(
        id: c_slice::Ref<'_, u8>,
        string_primitive_format: StringPrimitiveFormat,
    ) -> char_p::Box;

    pub fn ditto_validate_document_id(
        cbor: c_slice::Ref<'_, u8>,
        out_cbor: Out<'_, Option<c_slice::Box<u8>>>,
    ) -> c_uint;

    pub fn ditto_add_subscription(
        ditto: &'_ Ditto,
        collection: char_p::Ref<'_>,
        query: char_p::Ref<'_>,
    );

    pub fn ditto_remove_subscription(
        ditto: &'_ Ditto,
        collection: char_p::Ref<'_>,
        query: char_p::Ref<'_>,
    );

    pub fn ditto_live_query_register_str(
        ditto: &'_ Ditto,
        coll_name: char_p::Ref<'_>,
        query: char_p::Ref<'_>,
        order_by: c_slice::Ref<'_, COrderByParam>,
        limit: c_int,
        offset: c_uint,
        lq_availability: LiveQueryAvailability,
        id: Out<'_, i64>,
        ctx: *mut c_void,
        retain: Option<unsafe extern "C" fn(*mut c_void)>,
        release: Option<unsafe extern "C" fn(*mut c_void)>,
        c_cb: unsafe extern "C" fn(ctx: *mut c_void, params: c_cb_params),
    ) -> c_int;

    pub fn ditto_live_query_start(ditto: &'_ Ditto, id: i64) -> c_int;

    pub fn ditto_live_query_stop(ditto: &'_ Ditto, id: i64);

    pub fn ditto_stop_all_live_queries(ditto: &mut Ditto);

    pub fn ditto_live_query_signal_available_next(ditto: &'_ Ditto, id: i64);

    pub fn ditto_new_attachment_from_file(
        ditto: &'_ Ditto,
        source_path: char_p::Ref<'_>,
        file_operation: AttachmentFileOperation,
        out_attachment: Out<'_, Attachment>,
    ) -> c_uint;

    pub fn ditto_get_complete_attachment_path(
        ditto: &'_ Ditto,
        handle: &'_ AttachmentHandle,
    ) -> char_p::Box;

    pub fn ditto_resolve_attachment(
        ditto: &'_ Ditto,
        id: c_slice::Ref<'_, u8>,
        ctx: *mut c_void,
        retain: Option<unsafe extern "C" fn(*mut c_void)>,
        release: Option<unsafe extern "C" fn(*mut c_void)>,
        out_cancel_token: Out<'_, u64>,
        on_complete_cb: unsafe extern "C" fn(ctx: *mut c_void, BoxedAttachmentHandle),
        on_progress_cb: unsafe extern "C" fn(ctx: *mut c_void, u64, u64),
        on_deleted_cb: unsafe extern "C" fn(ctx: *mut c_void),
    ) -> c_uint;

    pub fn ditto_cancel_resolve_attachment(
        ditto: &'_ Ditto,
        id: c_slice::Ref<'_, u8>,
        cancel_token: u64,
    ) -> c_uint;

    pub fn ditto_document_update(
        document: &'_ mut Document,
        cbor: c_slice::Ref<'_, u8>,
        create_path: bool,
    ) -> c_int;

    pub fn ditto_collection_update_multiple(
        ditto: &'_ Ditto,
        coll_name: char_p::Ref<'_>,
        transaction: &'_ mut CWriteTransaction,
        documents: repr_c::Vec<BoxedDocument>,
    ) -> c_int;

    pub fn ditto_error_message() -> Option<char_p::Box>;

    pub fn ditto_document_cbor(document: &'_ Document) -> c_slice::Box<u8>;

    pub fn ditto_document_get_cbor(
        document: &'_ Document,
        pointer: char_p::Ref<'_>,
    ) -> Option<c_slice::Box<u8>>;

    pub fn ditto_document_set_cbor(
        document: &'_ mut Document,
        pointer: char_p::Ref<'_>,
        cbor: c_slice::Ref<'_, u8>,
        create_path: bool,
    ) -> c_int;

    pub fn ditto_document_set_cbor_with_timestamp(
        document: &'_ mut Document,
        pointer: char_p::Ref<'_>,
        cbor: c_slice::Ref<'_, u8>,
        create_path: bool,
        timestamp: c_uint,
    ) -> c_int;

    pub fn ditto_document_remove(document: &'_ mut Document, pointer: char_p::Ref<'_>) -> c_int;

    pub fn ditto_document_insert_cbor(
        document: &'_ mut Document,
        pointer: char_p::Ref<'_>,
        cbor: c_slice::Ref<'_, u8>,
    ) -> c_int;

    pub fn ditto_document_push_cbor(
        document: &'_ mut Document,
        pointer: char_p::Ref<'_>,
        cbor: c_slice::Ref<'_, u8>,
    ) -> c_int;

    pub fn ditto_document_pop_cbor(
        document: &'_ mut Document,
        pointer: char_p::Ref<'_>,
        out_cbor: Out<'_, c_slice::Box<u8>>,
    ) -> c_int;

    pub fn ditto_document_free(document: &mut Document);

    pub fn ditto_document_replace_with_counter(
        document: &'_ mut Document,
        pointer: char_p::Ref<'_>,
    ) -> c_int;

    pub fn ditto_document_replace_with_counter_with_timestamp(
        document: &'_ mut Document,
        pointer: char_p::Ref<'_>,
        timestamp: c_uint,
    ) -> c_int;

    pub fn ditto_document_increment_counter(
        document: &'_ mut Document,
        pointer: char_p::Ref<'_>,
        amount: c_double,
    ) -> c_int;

    pub fn ditto_free_attachment_handle(handle: &mut AttachmentHandle);

    pub fn static_tcp_client_free_handle(handle: *mut StaticTcpClientPlatformHandle);

    pub fn websocket_client_free_handle(handle: *mut WebsocketClientPlatformHandle);

    pub fn ble_client_free_handle(handle: *mut BleClientPlatformHandle);

    pub fn ble_server_free_handle(handle: *mut BleServerPlatformHandle);

    pub fn ditto_free_documents(documents: Option<repr_c::Vec<BoxedDocument>>);

    pub fn ditto_free_indices(indices: Option<c_slice::Box<usize>>);

    pub fn ditto_c_string_free(s: char_p::Box);

    pub fn ditto_c_bytes_free(bytes: c_slice::Box<u8>);

    pub fn ditto_auth_client_make_with_web(
        working_dir: char_p::Ref<'_>,
        app_id: char_p::Ref<'_>,
        base_url: char_p::Ref<'_>,
        auth_client: Out<'_, Option<BoxedAuthClient>>,
    ) -> c_int;

    pub fn ditto_auth_client_make_for_development(
        working_dir: Option<char_p::Ref<'_>>,
        app_id: char_p::Ref<'_>,
        site_id: c_ulonglong,
        auth_client: Out<'_, Option<BoxedAuthClient>>,
    ) -> c_int;

    pub fn ditto_auth_client_make_with_shared_key(
        working_dir: Option<char_p::Ref<'_>>,
        app_id: char_p::Ref<'_>,
        key_der_b64: char_p::Ref<'_>,
        site_id: c_ulonglong,
        auth_client: Out<'_, Option<BoxedAuthClient>>,
    ) -> c_int;

    pub fn ditto_auth_client_make_with_static_x509(
        config_cbor_b64: char_p::Ref<'_>,
        auth_client: Out<'_, Option<BoxedAuthClient>>,
    ) -> c_int;

    pub fn ditto_auth_client_free(auth_client: *mut AuthClient);

    pub fn ditto_auth_client_get_site_id(auth_client: &'_ AuthClient) -> c_ulonglong;

}

#[cfg(test)]
mod test {
    use ditto_test_support::ditto_test;

    use super::*;

    /// Minimal test to force cargo/rustc to attempt to link to
    /// libdittoffi
    #[ditto_test]
    fn test_linking() {
        unsafe {
            ditto_logger_init();
        }
    }
}