taskchampion-lib 0.6.1

Personal task-tracking
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
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
use crate::traits::*;
use crate::types::*;
use crate::util::err_to_ruststring;
use crate::TCKV;
use std::convert::TryFrom;
use std::ops::Deref;
use std::ptr::NonNull;
use std::str::FromStr;
use taskchampion::{utc_timestamp, Annotation, Tag, Task, TaskMut, Uuid};

#[ffizz_header::item]
#[ffizz(order = 1000)]
/// ***** TCTask *****
///
/// A task, as publicly exposed by this library.
///
/// A task begins in "immutable" mode.  It must be converted to "mutable" mode
/// to make any changes, and doing so requires exclusive access to the replica
/// until the task is freed or converted back to immutable mode.
///
/// An immutable task carries no reference to the replica that created it, and can be used until it
/// is freed or converted to a TaskMut.  A mutable task carries a reference to the replica and
/// must be freed or made immutable before the replica is freed.
///
/// All `tc_task_..` functions taking a task as an argument require that it not be NULL.
///
/// When a `tc_task_..` function that returns a TCResult returns TC_RESULT_ERROR, then
/// `tc_task_error` will return the error message.
///
/// # Safety
///
/// A task is an owned object, and must be freed with tc_task_free (or, if part of a list,
/// with tc_task_list_free).
///
/// Any function taking a `*TCTask` requires:
///  - the pointer must not be NUL;
///  - the pointer must be one previously returned from a tc_… function;
///  - the memory referenced by the pointer must never be modified by C code; and
///  - except for `tc_{task,task_list}_free`, ownership of a `*TCTask` remains with the caller.
///
/// Once passed to tc_task_free, a `*TCTask` becomes  invalid and must not be used again.
///
/// TCTasks are not threadsafe.
///
/// ```c
/// typedef struct TCTask TCTask;
/// ```
pub struct TCTask {
    /// The wrapped Task or TaskMut
    inner: Inner,

    /// The error from the most recent operation, if any
    error: Option<RustString<'static>>,
}

enum Inner {
    /// A regular, immutable task
    Immutable(Task),

    /// A mutable task, together with the replica to which it holds an exclusive
    /// reference.
    Mutable(TaskMut<'static>, *mut TCReplica),

    /// A transitional state for a TCTask as it goes from mutable to immutable and back.  A task
    /// can only be in this state outside of [`to_mut`] and [`to_immut`] if a panic occurs during
    /// one of those methods.
    Invalid,
}

impl PassByPointer for TCTask {}

impl TCTask {
    /// Make an immutable TCTask into a mutable TCTask.  Does nothing if the task
    /// is already mutable.
    ///
    /// # Safety
    ///
    /// The tcreplica pointer must not be NULL, and the replica it points to must not
    /// be freed before TCTask.to_immut completes.
    unsafe fn to_mut(&mut self, tcreplica: *mut TCReplica) {
        self.inner = match std::mem::replace(&mut self.inner, Inner::Invalid) {
            Inner::Immutable(task) => {
                // SAFETY:
                //  - tcreplica is not null (promised by caller)
                //  - tcreplica outlives the pointer in this variant (promised by caller)
                let tcreplica_ref: &mut TCReplica =
                    unsafe { TCReplica::from_ptr_arg_ref_mut(tcreplica) };
                let rep_ref = tcreplica_ref.borrow_mut();
                Inner::Mutable(task.into_mut(rep_ref), tcreplica)
            }
            Inner::Mutable(task, tcreplica) => Inner::Mutable(task, tcreplica),
            Inner::Invalid => unreachable!(),
        }
    }

    /// Make an mutable TCTask into a immutable TCTask.  Does nothing if the task
    /// is already immutable.
    #[allow(clippy::wrong_self_convention)] // to_immut_mut is not better!
    fn to_immut(&mut self) {
        self.inner = match std::mem::replace(&mut self.inner, Inner::Invalid) {
            Inner::Immutable(task) => Inner::Immutable(task),
            Inner::Mutable(task, tcreplica) => {
                // SAFETY:
                //  - tcreplica is not null (promised by caller of to_mut, which created this
                //    variant)
                //  - tcreplica is still alive (promised by caller of to_mut)
                let tcreplica_ref: &mut TCReplica =
                    unsafe { TCReplica::from_ptr_arg_ref_mut(tcreplica) };
                tcreplica_ref.release_borrow();
                Inner::Immutable(task.into_immut())
            }
            Inner::Invalid => unreachable!(),
        }
    }
}

impl From<Task> for TCTask {
    fn from(task: Task) -> TCTask {
        TCTask {
            inner: Inner::Immutable(task),
            error: None,
        }
    }
}

/// Utility function to get a shared reference to the underlying Task.  All Task getters
/// are error-free, so this does not handle errors.
fn wrap<T, F>(task: *mut TCTask, f: F) -> T
where
    F: FnOnce(&Task) -> T,
{
    // SAFETY:
    //  - task is not null (promised by caller)
    //  - task outlives this function (promised by caller)
    let tctask: &mut TCTask = unsafe { TCTask::from_ptr_arg_ref_mut(task) };
    let task: &Task = match &tctask.inner {
        Inner::Immutable(t) => t,
        Inner::Mutable(t, _) => t.deref(),
        Inner::Invalid => unreachable!(),
    };
    tctask.error = None;
    f(task)
}

/// Utility function to get a mutable reference to the underlying Task.  The
/// TCTask must be mutable.  The inner function may use `?` syntax to return an
/// error, which will be represented with the `err_value` returned to C.
fn wrap_mut<T, F>(task: *mut TCTask, f: F, err_value: T) -> T
where
    F: FnOnce(&mut TaskMut) -> anyhow::Result<T>,
{
    // SAFETY:
    //  - task is not null (promised by caller)
    //  - task outlives this function (promised by caller)
    let tctask: &mut TCTask = unsafe { TCTask::from_ptr_arg_ref_mut(task) };
    let task: &mut TaskMut = match tctask.inner {
        Inner::Immutable(_) => panic!("Task is immutable"),
        Inner::Mutable(ref mut t, _) => t,
        Inner::Invalid => unreachable!(),
    };
    tctask.error = None;
    match f(task) {
        Ok(rv) => rv,
        Err(e) => {
            tctask.error = Some(err_to_ruststring(e));
            err_value
        }
    }
}

impl TryFrom<RustString<'static>> for Tag {
    type Error = anyhow::Error;

    fn try_from(mut rstring: RustString) -> Result<Tag, anyhow::Error> {
        let tagstr = rstring.as_str()?;
        Tag::from_str(tagstr)
    }
}

#[ffizz_header::item]
#[ffizz(order = 1010)]
/// ***** TCTaskList *****
///
/// TCTaskList represents a list of tasks.
///
/// The content of this struct must be treated as read-only: no fields or anything they reference
/// should be modified directly by C code.
///
/// When an item is taken from this list, its pointer in `items` is set to NULL.
///
/// ```c
/// typedef struct TCTaskList {
///   // number of tasks in items
///   size_t len;
///   // reserved
///   size_t _u1;
///   // Array of pointers representing each task. These remain owned by the TCTaskList instance and
///   // will be freed by tc_task_list_free.  This pointer is never NULL for a valid TCTaskList.
///   // Pointers in the array may be NULL after `tc_task_list_take`.
///   struct TCTask **items;
/// } TCTaskList;
/// ```
#[repr(C)]
pub struct TCTaskList {
    /// number of tasks in items
    len: libc::size_t,

    /// total size of items (internal use only)
    capacity: libc::size_t,

    /// Array of pointers representing each task. These remain owned by the TCTaskList instance and
    /// will be freed by tc_task_list_free.  This pointer is never NULL for a valid TCTaskList.
    /// Pointers in the array may be NULL after `tc_task_list_take`.
    items: *mut Option<NonNull<TCTask>>,
}

impl CList for TCTaskList {
    type Element = Option<NonNull<TCTask>>;

    unsafe fn from_raw_parts(items: *mut Self::Element, len: usize, cap: usize) -> Self {
        TCTaskList {
            len,
            capacity: cap,
            items,
        }
    }

    fn slice(&mut self) -> &mut [Self::Element] {
        // SAFETY:
        //  - because we have &mut self, we have read/write access to items[0..len]
        //  - all items are properly initialized Element's
        //  - return value lifetime is equal to &mmut self's, so access is exclusive
        //  - items and len came from Vec, so total size is < isize::MAX
        unsafe { std::slice::from_raw_parts_mut(self.items, self.len) }
    }

    fn into_raw_parts(self) -> (*mut Self::Element, usize, usize) {
        (self.items, self.len, self.capacity)
    }
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Get a task's UUID.
///
/// ```c
/// EXTERN_C struct TCUuid tc_task_get_uuid(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_get_uuid(task: *mut TCTask) -> TCUuid {
    wrap(task, |task| {
        // SAFETY:
        // - value is not allocated and need not be freed
        unsafe { TCUuid::return_val(task.get_uuid()) }
    })
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Get a task's status.
///
/// ```c
/// EXTERN_C enum TCStatus tc_task_get_status(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_get_status(task: *mut TCTask) -> TCStatus {
    wrap(task, |task| task.get_status().into())
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Get the underlying key/value pairs for this task.  The returned TCKVList is
/// a "snapshot" of the task and will not be updated if the task is subsequently
/// modified.  It is the caller's responsibility to free the TCKVList.
///
/// ```c
/// EXTERN_C struct TCKVList tc_task_get_taskmap(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_get_taskmap(task: *mut TCTask) -> TCKVList {
    wrap(task, |task| {
        let vec: Vec<TCKV> = task
            .get_taskmap()
            .iter()
            .map(|(k, v)| {
                let key = RustString::from(k.as_ref());
                let value = RustString::from(v.as_ref());
                TCKV::as_ctype((key, value))
            })
            .collect();
        // SAFETY:
        //  - caller will free this list
        unsafe { TCKVList::return_val(vec) }
    })
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Get a task's description.
///
/// ```c
/// EXTERN_C struct TCString tc_task_get_description(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_get_description(task: *mut TCTask) -> TCString {
    wrap(task, |task| {
        let descr = task.get_description();
        // SAFETY:
        //  - caller promises to free this string
        unsafe { TCString::return_val(descr.into()) }
    })
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Get a task property's value, or NULL if the task has no such property, (including if the
/// property name is not valid utf-8).
///
/// ```c
/// EXTERN_C struct TCString tc_task_get_value(struct TCTask *task, struct TCString property);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_get_value(task: *mut TCTask, property: TCString) -> TCString {
    // SAFETY:
    //  - property is valid (promised by caller)
    //  - caller will not use property after this call (convention)
    let mut property = unsafe { TCString::val_from_arg(property) };
    wrap(task, |task| {
        if let Ok(property) = property.as_str() {
            let value = task.get_value(property);
            if let Some(value) = value {
                // SAFETY:
                //  - caller promises to free this string
                return unsafe { TCString::return_val(value.into()) };
            }
        }
        TCString::default() // null value
    })
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Get the entry timestamp for a task (when it was created), or 0 if not set.
///
/// ```c
/// EXTERN_C time_t tc_task_get_entry(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_get_entry(task: *mut TCTask) -> libc::time_t {
    wrap(task, |task| libc::time_t::as_ctype(task.get_entry()))
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Get the wait timestamp for a task, or 0 if not set.
///
/// ```c
/// EXTERN_C time_t tc_task_get_wait(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_get_wait(task: *mut TCTask) -> libc::time_t {
    wrap(task, |task| libc::time_t::as_ctype(task.get_wait()))
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Get the modified timestamp for a task, or 0 if not set.
///
/// ```c
/// EXTERN_C time_t tc_task_get_modified(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_get_modified(task: *mut TCTask) -> libc::time_t {
    wrap(task, |task| libc::time_t::as_ctype(task.get_modified()))
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Check if a task is waiting.
///
/// ```c
/// EXTERN_C bool tc_task_is_waiting(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_is_waiting(task: *mut TCTask) -> bool {
    wrap(task, |task| task.is_waiting())
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Check if a task is active (started and not stopped).
///
/// ```c
/// EXTERN_C bool tc_task_is_active(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_is_active(task: *mut TCTask) -> bool {
    wrap(task, |task| task.is_active())
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Check if a task is blocked (depends on at least one other task).
///
/// ```c
/// EXTERN_C bool tc_task_is_blocked(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_is_blocked(task: *mut TCTask) -> bool {
    wrap(task, |task| task.is_blocked())
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Check if a task is blocking (at least one other task depends on it).
///
/// ```c
/// EXTERN_C bool tc_task_is_blocking(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_is_blocking(task: *mut TCTask) -> bool {
    wrap(task, |task| task.is_blocking())
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Check if a task has the given tag.  If the tag is invalid, this function will return false, as
/// that (invalid) tag is not present. No error will be reported via `tc_task_error`.
///
/// ```c
/// EXTERN_C bool tc_task_has_tag(struct TCTask *task, struct TCString tag);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_has_tag(task: *mut TCTask, tag: TCString) -> bool {
    // SAFETY:
    //  - tag is valid (promised by caller)
    //  - caller will not use tag after this call (convention)
    let tcstring = unsafe { TCString::val_from_arg(tag) };
    wrap(task, |task| {
        if let Ok(tag) = Tag::try_from(tcstring) {
            task.has_tag(&tag)
        } else {
            false
        }
    })
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Get the tags for the task.
///
/// The caller must free the returned TCStringList instance.  The TCStringList instance does not
/// reference the task and the two may be freed in any order.
///
/// ```c
/// EXTERN_C struct TCStringList tc_task_get_tags(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_get_tags(task: *mut TCTask) -> TCStringList {
    wrap(task, |task| {
        let vec: Vec<TCString> = task
            .get_tags()
            .map(|t| {
                // SAFETY:
                //  - this TCString will be freed via tc_string_list_free.
                unsafe { TCString::return_val(t.as_ref().into()) }
            })
            .collect();
        // SAFETY:
        //  - caller will free the list
        unsafe { TCStringList::return_val(vec) }
    })
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Get the annotations for the task.
///
/// The caller must free the returned TCAnnotationList instance.  The TCStringList instance does not
/// reference the task and the two may be freed in any order.
///
/// ```c
/// EXTERN_C struct TCAnnotationList tc_task_get_annotations(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_get_annotations(task: *mut TCTask) -> TCAnnotationList {
    wrap(task, |task| {
        let vec: Vec<TCAnnotation> = task
            .get_annotations()
            .map(|a| {
                let description = RustString::from(a.description);
                TCAnnotation::as_ctype((a.entry, description))
            })
            .collect();
        // SAFETY:
        //  - caller will free the list
        unsafe { TCAnnotationList::return_val(vec) }
    })
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Get the named UDA from the task.
///
/// Returns a TCString with NULL ptr field if the UDA does not exist.
///
/// ```c
/// EXTERN_C struct TCString tc_task_get_uda(struct TCTask *task, struct TCString ns, struct TCString key);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_get_uda(
    task: *mut TCTask,
    ns: TCString,
    key: TCString,
) -> TCString {
    wrap(task, |task| {
        // SAFETY:
        //  - ns is valid (promised by caller)
        //  - caller will not use ns after this call (convention)
        if let Ok(ns) = unsafe { TCString::val_from_arg(ns) }.as_str() {
            // SAFETY: same
            if let Ok(key) = unsafe { TCString::val_from_arg(key) }.as_str() {
                if let Some(value) = task.get_uda(ns, key) {
                    // SAFETY:
                    // - caller will free this string (caller promises)
                    return unsafe { TCString::return_val(value.into()) };
                }
            }
        }
        TCString::default()
    })
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Get the named legacy UDA from the task.
///
/// Returns NULL if the UDA does not exist.
///
/// ```c
/// EXTERN_C struct TCString tc_task_get_legacy_uda(struct TCTask *task, struct TCString key);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_get_legacy_uda(task: *mut TCTask, key: TCString) -> TCString {
    wrap(task, |task| {
        // SAFETY:
        //  - key is valid (promised by caller)
        //  - caller will not use key after this call (convention)
        if let Ok(key) = unsafe { TCString::val_from_arg(key) }.as_str() {
            if let Some(value) = task.get_legacy_uda(key) {
                // SAFETY:
                // - caller will free this string (caller promises)
                return unsafe { TCString::return_val(value.into()) };
            }
        }
        TCString::default()
    })
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Get all UDAs for this task.
///
/// Legacy UDAs are represented with an empty string in the ns field.
///
/// ```c
/// EXTERN_C struct TCUdaList tc_task_get_udas(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_get_udas(task: *mut TCTask) -> TCUdaList {
    wrap(task, |task| {
        let vec: Vec<TCUda> = task
            .get_udas()
            .map(|((ns, key), value)| {
                // SAFETY:
                //  - will be freed by tc_uda_list_free
                unsafe {
                    TCUda::return_val(Uda {
                        ns: Some(ns.into()),
                        key: key.into(),
                        value: value.into(),
                    })
                }
            })
            .collect();
        // SAFETY:
        //  - caller will free this list
        unsafe { TCUdaList::return_val(vec) }
    })
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Get all UDAs for this task.
///
/// All TCUdas in this list have a NULL ns field.  The entire UDA key is
/// included in the key field.  The caller must free the returned list.
///
/// ```c
/// EXTERN_C struct TCUdaList tc_task_get_legacy_udas(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_get_legacy_udas(task: *mut TCTask) -> TCUdaList {
    wrap(task, |task| {
        let vec: Vec<TCUda> = task
            .get_legacy_udas()
            .map(|(key, value)| {
                // SAFETY:
                //  - will be freed by tc_uda_list_free
                unsafe {
                    TCUda::return_val(Uda {
                        ns: None,
                        key: key.into(),
                        value: value.into(),
                    })
                }
            })
            .collect();
        // SAFETY:
        //  - caller will free this list
        unsafe { TCUdaList::return_val(vec) }
    })
}

#[ffizz_header::item]
#[ffizz(order = 1001)]
/// Get all dependencies for a task.
///
/// ```c
/// EXTERN_C struct TCUuidList tc_task_get_dependencies(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_get_dependencies(task: *mut TCTask) -> TCUuidList {
    wrap(task, |task| {
        let vec: Vec<TCUuid> = task
            .get_dependencies()
            .map(|u| {
                // SAFETY:
                //  - value is not allocated
                unsafe { TCUuid::return_val(u) }
            })
            .collect();
        // SAFETY:
        //  - caller will free this list
        unsafe { TCUuidList::return_val(vec) }
    })
}

#[ffizz_header::item]
#[ffizz(order = 1002)]
/// Convert an immutable task into a mutable task.
///
/// The task must not be NULL. It is modified in-place, and becomes mutable.
///
/// The replica must not be NULL. After this function returns, the replica _cannot be used at all_
/// until this task is made immutable again.  This implies that it is not allowed for more than one
/// task associated with a replica to be mutable at any time.
///
/// Typically mutation of tasks is bracketed with `tc_task_to_mut` and `tc_task_to_immut`:
///
/// ```text
///     tc_task_to_mut(task, rep);
///     success = tc_task_done(task);
///     tc_task_to_immut(task, rep);
///     if (!success) { ... }
/// ```
///
/// ```c
/// EXTERN_C void tc_task_to_mut(struct TCTask *task, struct TCReplica *tcreplica);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_to_mut(task: *mut TCTask, tcreplica: *mut TCReplica) {
    // SAFETY:
    //  - task is not null (promised by caller)
    //  - task outlives 'a (promised by caller)
    let tctask: &mut TCTask = unsafe { TCTask::from_ptr_arg_ref_mut(task) };
    // SAFETY:
    //  - tcreplica is not NULL (promised by caller)
    //  - tcreplica lives until later call to to_immut via tc_task_to_immut (promised by caller,
    //    who cannot call tc_replica_free during this time)
    unsafe { tctask.to_mut(tcreplica) };
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Set a mutable task's status.
///
/// ```c
/// EXTERN_C TCResult tc_task_set_status(struct TCTask *task, enum TCStatus status);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_set_status(task: *mut TCTask, status: TCStatus) -> TCResult {
    wrap_mut(
        task,
        |task| {
            task.set_status(status.into())?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Set a mutable task's property value by name.  If value.ptr is NULL, the property is removed.
///
/// ```c
/// EXTERN_C TCResult tc_task_set_value(struct TCTask *task, struct TCString property, struct TCString value);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_set_value(
    task: *mut TCTask,
    property: TCString,
    value: TCString,
) -> TCResult {
    // SAFETY:
    //  - property is valid (promised by caller)
    //  - caller will not use property after this call (convention)
    let mut property = unsafe { TCString::val_from_arg(property) };
    let value = if value.is_null() {
        None
    } else {
        // SAFETY:
        //  - value is valid (promised by caller, after NULL check)
        //  - caller will not use value after this call (convention)
        Some(unsafe { TCString::val_from_arg(value) })
    };
    wrap_mut(
        task,
        |task| {
            let value_str = if let Some(mut v) = value {
                Some(v.as_str()?.to_string())
            } else {
                None
            };
            task.set_value(property.as_str()?.to_string(), value_str)?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Set a mutable task's description.
///
/// ```c
/// EXTERN_C TCResult tc_task_set_description(struct TCTask *task, struct TCString description);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_set_description(
    task: *mut TCTask,
    description: TCString,
) -> TCResult {
    // SAFETY:
    //  - description is valid (promised by caller)
    //  - caller will not use description after this call (convention)
    let mut description = unsafe { TCString::val_from_arg(description) };
    wrap_mut(
        task,
        |task| {
            task.set_description(description.as_str()?.to_string())?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Set a mutable task's entry (creation time).  Pass entry=0 to unset
/// the entry field.
///
/// ```c
/// EXTERN_C TCResult tc_task_set_entry(struct TCTask *task, time_t entry);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_set_entry(task: *mut TCTask, entry: libc::time_t) -> TCResult {
    wrap_mut(
        task,
        |task| {
            // SAFETY: any time_t value is a valid timestamp
            task.set_entry(unsafe { entry.from_ctype() })?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Set a mutable task's wait timestamp.  Pass wait=0 to unset the wait field.
///
/// ```c
/// EXTERN_C TCResult tc_task_set_wait(struct TCTask *task, time_t wait);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_set_wait(task: *mut TCTask, wait: libc::time_t) -> TCResult {
    wrap_mut(
        task,
        |task| {
            // SAFETY: any time_t value is a valid timestamp
            task.set_wait(unsafe { wait.from_ctype() })?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Set a mutable task's modified timestamp.  The value cannot be zero.
///
/// ```c
/// EXTERN_C TCResult tc_task_set_modified(struct TCTask *task, time_t modified);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_set_modified(
    task: *mut TCTask,
    modified: libc::time_t,
) -> TCResult {
    wrap_mut(
        task,
        |task| {
            task.set_modified(
                // SAFETY: any time_t value is a valid timestamp
                unsafe { modified.from_ctype() }
                    .ok_or_else(|| anyhow::anyhow!("modified cannot be zero"))?,
            )?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Start a task.
///
/// ```c
/// EXTERN_C TCResult tc_task_start(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_start(task: *mut TCTask) -> TCResult {
    wrap_mut(
        task,
        |task| {
            task.start()?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Stop a task.
///
/// ```c
/// EXTERN_C TCResult tc_task_stop(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_stop(task: *mut TCTask) -> TCResult {
    wrap_mut(
        task,
        |task| {
            task.stop()?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Mark a task as done.
///
/// ```c
/// EXTERN_C TCResult tc_task_done(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_done(task: *mut TCTask) -> TCResult {
    wrap_mut(
        task,
        |task| {
            task.done()?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Mark a task as deleted.
///
/// ```c
/// EXTERN_C TCResult tc_task_delete(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_delete(task: *mut TCTask) -> TCResult {
    wrap_mut(
        task,
        |task| {
            task.delete()?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Add a tag to a mutable task.
///
/// ```c
/// EXTERN_C TCResult tc_task_add_tag(struct TCTask *task, struct TCString tag);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_add_tag(task: *mut TCTask, tag: TCString) -> TCResult {
    // SAFETY:
    //  - tag is valid (promised by caller)
    //  - caller will not use tag after this call (convention)
    let tcstring = unsafe { TCString::val_from_arg(tag) };
    wrap_mut(
        task,
        |task| {
            let tag = Tag::try_from(tcstring)?;
            task.add_tag(&tag)?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Remove a tag from a mutable task.
///
/// ```c
/// EXTERN_C TCResult tc_task_remove_tag(struct TCTask *task, struct TCString tag);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_remove_tag(task: *mut TCTask, tag: TCString) -> TCResult {
    // SAFETY:
    //  - tag is valid (promised by caller)
    //  - caller will not use tag after this call (convention)
    let tcstring = unsafe { TCString::val_from_arg(tag) };
    wrap_mut(
        task,
        |task| {
            let tag = Tag::try_from(tcstring)?;
            task.remove_tag(&tag)?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Add an annotation to a mutable task.  This call takes ownership of the
/// passed annotation, which must not be used after the call returns.
///
/// ```c
/// EXTERN_C TCResult tc_task_add_annotation(struct TCTask *task, struct TCAnnotation *annotation);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_add_annotation(
    task: *mut TCTask,
    annotation: *mut TCAnnotation,
) -> TCResult {
    // SAFETY:
    //  - annotation is not NULL (promised by caller)
    //  - annotation is return from a tc_string_.. so is valid
    //  - caller will not use annotation after this call
    let (entry, description) =
        unsafe { TCAnnotation::take_val_from_arg(annotation, TCAnnotation::default()) };
    wrap_mut(
        task,
        |task| {
            let description = description.into_string()?;
            task.add_annotation(Annotation { entry, description })?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Remove an annotation from a mutable task.
///
/// ```c
/// EXTERN_C TCResult tc_task_remove_annotation(struct TCTask *task, int64_t entry);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_remove_annotation(task: *mut TCTask, entry: i64) -> TCResult {
    wrap_mut(
        task,
        |task| {
            task.remove_annotation(utc_timestamp(entry))?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Set a UDA on a mutable task.
///
/// ```c
/// EXTERN_C TCResult tc_task_set_uda(struct TCTask *task,
///                          struct TCString ns,
///                          struct TCString key,
///                          struct TCString value);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_set_uda(
    task: *mut TCTask,
    ns: TCString,
    key: TCString,
    value: TCString,
) -> TCResult {
    // safety:
    //  - ns is valid (promised by caller)
    //  - caller will not use ns after this call (convention)
    let mut ns = unsafe { TCString::val_from_arg(ns) };
    // SAFETY: same
    let mut key = unsafe { TCString::val_from_arg(key) };
    // SAFETY: same
    let mut value = unsafe { TCString::val_from_arg(value) };
    wrap_mut(
        task,
        |task| {
            task.set_uda(ns.as_str()?, key.as_str()?, value.as_str()?.to_string())?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Remove a UDA fraom a mutable task.
///
/// ```c
/// EXTERN_C TCResult tc_task_remove_uda(struct TCTask *task, struct TCString ns, struct TCString key);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_remove_uda(
    task: *mut TCTask,
    ns: TCString,
    key: TCString,
) -> TCResult {
    // safety:
    //  - ns is valid (promised by caller)
    //  - caller will not use ns after this call (convention)
    let mut ns = unsafe { TCString::val_from_arg(ns) };
    // SAFETY: same
    let mut key = unsafe { TCString::val_from_arg(key) };
    wrap_mut(
        task,
        |task| {
            task.remove_uda(ns.as_str()?, key.as_str()?)?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Set a legacy UDA on a mutable task.
///
/// ```c
/// EXTERN_C TCResult tc_task_set_legacy_uda(struct TCTask *task, struct TCString key, struct TCString value);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_set_legacy_uda(
    task: *mut TCTask,
    key: TCString,
    value: TCString,
) -> TCResult {
    // safety:
    //  - key is valid (promised by caller)
    //  - caller will not use key after this call (convention)
    let mut key = unsafe { TCString::val_from_arg(key) };
    // SAFETY: same
    let mut value = unsafe { TCString::val_from_arg(value) };
    wrap_mut(
        task,
        |task| {
            task.set_legacy_uda(key.as_str()?.to_string(), value.as_str()?.to_string())?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Remove a UDA fraom a mutable task.
///
/// ```c
/// EXTERN_C TCResult tc_task_remove_legacy_uda(struct TCTask *task, struct TCString key);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_remove_legacy_uda(task: *mut TCTask, key: TCString) -> TCResult {
    // safety:
    //  - key is valid (promised by caller)
    //  - caller will not use key after this call (convention)
    let mut key = unsafe { TCString::val_from_arg(key) };
    wrap_mut(
        task,
        |task| {
            task.remove_legacy_uda(key.as_str()?.to_string())?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Add a dependency.
///
/// ```c
/// EXTERN_C TCResult tc_task_add_dependency(struct TCTask *task, struct TCUuid dep);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_add_dependency(task: *mut TCTask, dep: TCUuid) -> TCResult {
    // SAFETY:
    //  - tcuuid is a valid TCUuid (all byte patterns are valid)
    let dep: Uuid = unsafe { TCUuid::val_from_arg(dep) };
    wrap_mut(
        task,
        |task| {
            task.add_dependency(dep)?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1003)]
/// Remove a dependency.
///
/// ```c
/// EXTERN_C TCResult tc_task_remove_dependency(struct TCTask *task, struct TCUuid dep);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_remove_dependency(task: *mut TCTask, dep: TCUuid) -> TCResult {
    // SAFETY:
    //  - tcuuid is a valid TCUuid (all byte patterns are valid)
    let dep: Uuid = unsafe { TCUuid::val_from_arg(dep) };
    wrap_mut(
        task,
        |task| {
            task.remove_dependency(dep)?;
            Ok(TCResult::Ok)
        },
        TCResult::Error,
    )
}

#[ffizz_header::item]
#[ffizz(order = 1004)]
/// Convert a mutable task into an immutable task.
///
/// The task must not be NULL.  It is modified in-place, and becomes immutable.
///
/// The replica passed to `tc_task_to_mut` may be used freely after this call.
///
/// ```c
/// EXTERN_C void tc_task_to_immut(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_to_immut(task: *mut TCTask) {
    // SAFETY:
    //  - task is not null (promised by caller)
    //  - task outlives 'a (promised by caller)
    let tctask: &mut TCTask = unsafe { TCTask::from_ptr_arg_ref_mut(task) };
    tctask.to_immut();
}

#[ffizz_header::item]
#[ffizz(order = 1005)]
/// Get the latest error for a task, or a string NULL ptr field if the last operation succeeded.
/// Subsequent calls to this function will return NULL.  The task pointer must not be NULL.  The
/// caller must free the returned string.
///
/// ```c
/// EXTERN_C struct TCString tc_task_error(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_error(task: *mut TCTask) -> TCString {
    // SAFETY:
    //  - task is not null (promised by caller)
    //  - task outlives 'a (promised by caller)
    let task: &mut TCTask = unsafe { TCTask::from_ptr_arg_ref_mut(task) };
    if let Some(rstring) = task.error.take() {
        // SAFETY:
        //  - caller promises to free this value
        unsafe { TCString::return_val(rstring) }
    } else {
        TCString::default()
    }
}

#[ffizz_header::item]
#[ffizz(order = 1006)]
/// Free a task.  The given task must not be NULL.  The task must not be used after this function
/// returns, and must not be freed more than once.
///
/// If the task is currently mutable, it will first be made immutable.
///
/// ```c
/// EXTERN_C void tc_task_free(struct TCTask *task);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_free(task: *mut TCTask) {
    // SAFETY:
    //  - task is not NULL (promised by caller)
    //  - caller will not use the TCTask after this (promised by caller)
    let mut tctask = unsafe { TCTask::take_from_ptr_arg(task) };

    // convert to immut if it was mutable
    tctask.to_immut();

    drop(tctask);
}

#[ffizz_header::item]
#[ffizz(order = 1011)]
/// Take an item from a TCTaskList.  After this call, the indexed item is no longer associated
/// with the list and becomes the caller's responsibility, just as if it had been returned from
/// `tc_replica_get_task`.
///
/// The corresponding element in the `items` array will be set to NULL.  If that field is already
/// NULL (that is, if the item has already been taken), this function will return NULL.  If the
/// index is out of bounds, this function will also return NULL.
///
/// The passed TCTaskList remains owned by the caller.
///
/// ```c
/// EXTERN_C struct TCTask *tc_task_list_take(struct TCTaskList *tasks, size_t index);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_list_take(tasks: *mut TCTaskList, index: usize) -> *mut TCTask {
    // SAFETY:
    //  - tasks is not NULL and points to a valid TCTaskList (caller is not allowed to
    //    modify the list directly, and tc_task_list_take leaves the list valid)
    let p = unsafe { take_optional_pointer_list_item(tasks, index) };
    if let Some(p) = p {
        p.as_ptr()
    } else {
        std::ptr::null_mut()
    }
}

#[ffizz_header::item]
#[ffizz(order = 1011)]
/// Free a TCTaskList instance.  The instance, and all TCTaskList it contains, must not be used after
/// this call.
///
/// When this call returns, the `items` pointer will be NULL, signalling an invalid TCTaskList.
///
/// ```c
/// EXTERN_C void tc_task_list_free(struct TCTaskList *tasks);
/// ```
#[no_mangle]
pub unsafe extern "C" fn tc_task_list_free(tasks: *mut TCTaskList) {
    // SAFETY:
    //  - tasks is not NULL and points to a valid TCTaskList (caller is not allowed to
    //    modify the list directly, and tc_task_list_take leaves the list valid)
    //  - caller promises not to use the value after return
    unsafe { drop_optional_pointer_list(tasks) };
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn empty_list_has_non_null_pointer() {
        let tasks = unsafe { TCTaskList::return_val(Vec::new()) };
        assert!(!tasks.items.is_null());
        assert_eq!(tasks.len, 0);
        assert_eq!(tasks.capacity, 0);
    }

    #[test]
    fn free_sets_null_pointer() {
        let mut tasks = unsafe { TCTaskList::return_val(Vec::new()) };
        // SAFETY: testing expected behavior
        unsafe { tc_task_list_free(&mut tasks) };
        assert!(tasks.items.is_null());
        assert_eq!(tasks.len, 0);
        assert_eq!(tasks.capacity, 0);
    }
}