koicore_ffi 0.2.2

FFI bindings for koicore
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
use koicore::{
    Value,
    command::{Command, CompositeValue, Parameter},
};
use std::{
    ffi::{CStr, c_char},
    ptr, slice,
};

use crate::command::param::KoiParamType;

use super::command::KoiCommand;

/// Opaque handle for composite list parameter
///
/// This structure represents a list parameter in a KoiLang command.
/// Lists can contain values of different types (integers, floats, strings).
#[repr(C)]
pub struct KoiCompositeList {
    _data: (),
    _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
}

/// Get composite list parameter from command
///
/// This function retrieves a list parameter from a command at the specified index.
/// The parameter must be of list type, otherwise NULL is returned.
///
/// # Ownership and Lifetime
///
/// The returned pointer is a borrowed reference to data owned by the command.
/// It must NOT be freed with KoiCompositeList_Del. The pointer is only valid
/// as long as the command object exists and is not modified or destroyed.
///
/// # Arguments
///
/// * `command` - Pointer to the command object
/// * `index` - Zero-based index of the parameter to retrieve
///
/// # Returns
///
/// Pointer to the composite list parameter, or NULL if:
/// - command is NULL
/// - index is out of bounds
/// - the parameter at the specified index is not a list
///
/// # Safety
///
/// The `command` pointer must be either NULL or point to a valid KoiCommand object.
/// The returned pointer must NOT be freed with KoiCompositeList_Del as it is owned by the command.
/// The returned pointer becomes invalid if the command is destroyed or modified.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_GetCompositeList(
    command: *mut KoiCommand,
    index: usize,
) -> *mut KoiCompositeList {
    if command.is_null() {
        return ptr::null_mut();
    }

    let command = unsafe { &*(command as *mut Command) };
    let params = command.params();

    if index >= params.len() {
        return ptr::null_mut();
    }

    match &params[index] {
        p @ &Parameter::Composite(_, CompositeValue::List(_)) => {
            // Cast the parameter reference to the opaque list type
            p as *const Parameter as *mut KoiCompositeList
        }
        _ => ptr::null_mut(),
    }
}

/// Get composite list parameter length
///
/// This function returns the number of elements in the list parameter.
///
/// # Arguments
///
/// * `list` - Pointer to the composite list parameter
///
/// # Returns
///
/// Number of elements in the list, or 0 if the list pointer is NULL or invalid.
///
/// # Safety
///
/// The `list` pointer must be either NULL or point to a valid KoiCompositeList object
/// obtained from KoiCommand_GetCompositeList or KoiCompositeList_New.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_GetLength(list: *mut KoiCompositeList) -> usize {
    if list.is_null() {
        return 0;
    }

    let param = unsafe { &*(list as *const Parameter) };
    match param {
        Parameter::Composite(_, CompositeValue::List(values)) => values.len(),
        _ => 0,
    }
}

/// Get value type from composite list by index
///
/// This function determines the type of a value at the specified index in the list.
///
/// # Arguments
///
/// * `list` - Pointer to the composite list parameter
/// * `index` - Zero-based index of the value to query
///
/// # Returns
///
/// The type of the value as a KoiParamType enum value, or KoiParamType::Invalid
/// if the list pointer is NULL, invalid, or the index is out of bounds.
///
/// # Safety
///
/// The `list` pointer must be either NULL or point to a valid KoiCompositeList object.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_GetValueType(
    list: *mut KoiCompositeList,
    index: usize,
) -> i32 {
    if list.is_null() {
        return KoiParamType::Invalid as i32;
    }

    let param = unsafe { &*(list as *const Parameter) };
    match param {
        Parameter::Composite(_, CompositeValue::List(values)) => {
            if index >= values.len() {
                KoiParamType::Invalid as i32
            } else {
                match &values[index] {
                    Value::Int(_) => KoiParamType::BasicInt as i32,
                    Value::Float(_) => KoiParamType::BasicFloat as i32,
                    Value::String(_) => KoiParamType::BasicString as i32,
                    Value::Bool(_) => KoiParamType::BasicBool as i32,
                }
            }
        }
        _ => KoiParamType::Invalid as i32,
    }
}

/// Get integer value from composite list by index
///
/// This function retrieves an integer value from the list at the specified index.
/// The value at the specified index must be of integer type.
///
/// # Arguments
///
/// * `list` - Pointer to the composite list parameter
/// * `index` - Zero-based index of the value to retrieve
/// * `out_value` - Pointer to store the retrieved integer value
///
/// # Returns
///
/// 0 on success, or a non-zero error code on failure:
/// - -1: list pointer is NULL or out_value is NULL
/// - -2: index is out of bounds
/// - -3: value at the specified index is not an integer
/// - -4: list pointer is invalid
///
/// # Safety
///
/// The `list` pointer must be either NULL or point to a valid KoiCompositeList object.
/// The `out_value` pointer must point to a valid i64 variable.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_GetIntValue(
    list: *mut KoiCompositeList,
    index: usize,
    out_value: *mut i64,
) -> i32 {
    if list.is_null() || out_value.is_null() {
        return -1;
    }

    let param = unsafe { &*(list as *const Parameter) };
    match param {
        Parameter::Composite(_, CompositeValue::List(values)) => {
            if index >= values.len() {
                return -2;
            }

            match &values[index] {
                Value::Int(value) => {
                    unsafe { *out_value = *value };
                    0
                }
                _ => -3,
            }
        }
        _ => -4,
    }
}

/// Get float value from composite list by index
///
/// This function retrieves a float value from the list at the specified index.
/// The value at the specified index must be of float type.
///
/// # Arguments
///
/// * `list` - Pointer to the composite list parameter
/// * `index` - Zero-based index of the value to retrieve
/// * `out_value` - Pointer to store the retrieved float value
///
/// # Returns
///
/// 0 on success, or a non-zero error code on failure:
/// - -1: list pointer is NULL or out_value is NULL
/// - -2: index is out of bounds
/// - -3: value at the specified index is not a float
/// - -4: list pointer is invalid
///
/// # Safety
///
/// The `list` pointer must be either NULL or point to a valid KoiCompositeList object.
/// The `out_value` pointer must point to a valid f64 variable.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_GetFloatValue(
    list: *mut KoiCompositeList,
    index: usize,
    out_value: *mut f64,
) -> i32 {
    if list.is_null() || out_value.is_null() {
        return -1;
    }

    let param = unsafe { &*(list as *const Parameter) };
    match param {
        Parameter::Composite(_, CompositeValue::List(values)) => {
            if index >= values.len() {
                return -2;
            }

            match &values[index] {
                Value::Float(value) => {
                    unsafe { *out_value = *value };
                    0
                }
                _ => -3,
            }
        }
        _ => -4,
    }
}

/// Get string value from composite list by index
///
/// This function retrieves a string value from the list at the specified index.
/// The value at the specified index must be of string type.
///
/// # Arguments
///
/// * `list` - Pointer to the composite list parameter
/// * `index` - Zero-based index of the value to retrieve
/// * `buffer` - Buffer to store the retrieved string value
/// * `buffer_size` - Size of the buffer in bytes
///
/// # Returns
///
/// The number of bytes required for the string including the null terminator.
/// If the buffer is NULL or too small, no data is written and the required size is returned.
/// Returns 0 on error or if the value at the specified index is not a string.
///
/// # Safety
///
/// The `list` pointer must be either NULL or point to a valid KoiCompositeList object.
/// If `buffer` is not NULL, it must point to a valid memory region of at least `buffer_size` bytes.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_GetStringValue(
    list: *mut KoiCompositeList,
    index: usize,
    buffer: *mut c_char,
    buffer_size: usize,
) -> usize {
    if list.is_null() || buffer.is_null() {
        return 0;
    }

    let param = unsafe { &*(list as *const Parameter) };
    match param {
        Parameter::Composite(_, CompositeValue::List(values)) => {
            if index >= values.len() {
                return 0;
            }

            let value_str = match &values[index] {
                Value::String(value) => value,
                _ => return 0,
            };

            let value_bytes = value_str.as_bytes();
            let value_len = value_bytes.len();
            let required_size = value_len + 1;

            if buffer_size < required_size {
                return required_size;
            }

            let buffer_slice = unsafe { slice::from_raw_parts_mut(buffer as *mut u8, buffer_size) };
            buffer_slice[..value_len].copy_from_slice(value_bytes);
            buffer_slice[value_len] = 0;

            required_size
        }
        _ => 0,
    }
}

/// Get string value length from composite list by index
///
/// This function returns the length of a string value at the specified index in the list.
/// The value at the specified index must be of string type.
///
/// # Arguments
///
/// * `list` - Pointer to the composite list parameter
/// * `index` - Zero-based index of the value to query
///
/// # Returns
///
/// Required buffer size (including null terminator), or 0 on error or type mismatch.
///
/// # Safety
///
/// The `list` pointer must be either NULL or point to a valid KoiCompositeList object.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_GetStringValueLen(
    list: *mut KoiCompositeList,
    index: usize,
) -> usize {
    if list.is_null() {
        return 0;
    }

    let param = unsafe { &*(list as *const Parameter) };
    match param {
        Parameter::Composite(_, CompositeValue::List(values)) => {
            if index >= values.len() {
                return 0;
            }

            match &values[index] {
                Value::String(value) => value.len() + 1,
                _ => 0,
            }
        }
        _ => 0,
    }
}

/// Create a new empty composite list
///
/// This function creates a new empty composite list parameter that can be
/// added to a command or used independently.
///
/// # Arguments
///
/// * `name` - The name of the composite parameter (null-terminated C string)
///
/// # Returns
///
/// Pointer to the newly created composite list parameter, or NULL on failure.
/// The caller is responsible for freeing the list using KoiCompositeList_Del,
/// unless it is passed to KoiCommand_AddCompositeList.
///
/// # Safety
///
/// The returned pointer must be freed using KoiCompositeList_Del when no longer needed,
/// unless ownership is transferred to a command.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_New(name: *const c_char) -> *mut KoiCompositeList {
    if name.is_null() {
        return ptr::null_mut();
    }

    let name_str = match unsafe { CStr::from_ptr(name) }.to_str() {
        Ok(s) => s.to_string(),
        Err(_) => return ptr::null_mut(),
    };

    let param = Parameter::Composite(name_str, CompositeValue::List(Vec::new()));
    Box::into_raw(Box::new(param)) as *mut KoiCompositeList
}

/// Add composite list to command
///
/// Adds the specified composite list to the command's parameters.
/// This function TAKES OWNERSHIP of the list pointer. The caller must NOT
/// free the list using KoiCompositeList_Del after a successful call.
///
/// # Arguments
///
/// * `command` - Pointer to the command object
/// * `list` - Pointer to the composite list parameter to add
///
/// # Returns
///
/// 0 on success, or a non-zero error code on failure:
/// - -1: command or list pointer is NULL
/// - -3: command pointer is invalid
///
/// # Safety
///
/// The `command` pointer must be a valid KoiCommand.
/// The `list` pointer must be a valid KoiCompositeList created with KoiCompositeList_New.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_AddCompositeList(
    command: *mut KoiCommand,
    list: *mut KoiCompositeList,
) -> i32 {
    if command.is_null() || list.is_null() {
        return -1;
    }

    let command = unsafe { &mut *(command as *mut Command) };
    let list_param = unsafe { Box::from_raw(list as *mut Parameter) };

    command.params.push(*list_param);
    0
}

/// Add integer value to composite list
///
/// This function appends an integer value to the end of the list.
///
/// # Arguments
///
/// * `list` - Pointer to the composite list parameter
/// * `value` - Integer value to add
///
/// # Returns
///
/// 0 on success, or a non-zero error code on failure:
/// - -1: list pointer is NULL
/// - -3: list pointer is invalid
///
/// # Safety
///
/// The `list` pointer must be either NULL or point to a valid KoiCompositeList object
/// obtained from KoiCommand_GetCompositeList or KoiCompositeList_New.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_AddIntValue(
    list: *mut KoiCompositeList,
    value: i64,
) -> i32 {
    if list.is_null() {
        return -1;
    }

    let param = unsafe { &mut *(list as *mut Parameter) };
    match param {
        Parameter::Composite(_, CompositeValue::List(values)) => {
            values.push(Value::Int(value));
            0
        }
        _ => -3,
    }
}

/// Add float value to composite list
///
/// This function appends a float value to the end of the list.
///
/// # Arguments
///
/// * `list` - Pointer to the composite list parameter
/// * `value` - Float value to add
///
/// # Returns
///
/// 0 on success, or a non-zero error code on failure:
/// - -1: list pointer is NULL
/// - -3: list pointer is invalid
///
/// # Safety
///
/// The `list` pointer must be either NULL or point to a valid KoiCompositeList object
/// obtained from KoiCommand_GetCompositeList or KoiCompositeList_New.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_AddFloatValue(
    list: *mut KoiCompositeList,
    value: f64,
) -> i32 {
    if list.is_null() {
        return -1;
    }

    let param = unsafe { &mut *(list as *mut Parameter) };
    match param {
        Parameter::Composite(_, CompositeValue::List(values)) => {
            values.push(Value::Float(value));
            0
        }
        _ => -3,
    }
}

/// Add string value to composite list
///
/// This function appends a string value to the end of the list.
/// The string is copied and managed by the list.
///
/// # Arguments
///
/// * `list` - Pointer to the composite list parameter
/// * `value` - String value to add (null-terminated C string)
///
/// # Returns
///
/// 0 on success, or a non-zero error code on failure:
/// - -1: list pointer is NULL or value is NULL
/// - -2: value contains invalid UTF-8
/// - -3: list pointer is invalid
///
/// # Safety
///
/// The `list` pointer must be either NULL or point to a valid KoiCompositeList object.
/// The `value` pointer must be either NULL or point to a valid null-terminated string.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_AddStringValue(
    list: *mut KoiCompositeList,
    value: *const c_char,
) -> i32 {
    if list.is_null() || value.is_null() {
        return -1;
    }

    let value_str = match unsafe { CStr::from_ptr(value) }.to_str() {
        Ok(s) => s.to_string(),
        Err(_) => return -2,
    };

    let param = unsafe { &mut *(list as *mut Parameter) };
    match param {
        Parameter::Composite(_, CompositeValue::List(values)) => {
            values.push(Value::String(value_str));
            0
        }
        _ => -3,
    }
}

/// Set integer value in composite list by index
///
/// This function replaces the value at the specified index with an integer value.
/// The index must be within the bounds of the list.
///
/// # Arguments
///
/// * `list` - Pointer to the composite list parameter
/// * `index` - Zero-based index of the value to replace
/// * `value` - New integer value
///
/// # Returns
///
/// 0 on success, or a non-zero error code on failure:
/// - -1: list pointer is NULL
/// - -2: index is out of bounds
/// - -3: list pointer is invalid
///
/// # Safety
///
/// The `list` pointer must be either NULL or point to a valid KoiCompositeList object.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_SetIntValue(
    list: *mut KoiCompositeList,
    index: usize,
    value: i64,
) -> i32 {
    if list.is_null() {
        return -1;
    }

    let param = unsafe { &mut *(list as *mut Parameter) };
    match param {
        Parameter::Composite(_, CompositeValue::List(values)) => {
            if index >= values.len() {
                return -2;
            }

            values[index] = Value::Int(value);
            0
        }
        _ => -3,
    }
}

/// Set float value in composite list by index
///
/// This function replaces the value at the specified index with a float value.
/// The index must be within the bounds of the list.
///
/// # Arguments
///
/// * `list` - Pointer to the composite list parameter
/// * `index` - Zero-based index of the value to replace
/// * `value` - New float value
///
/// # Returns
///
/// 0 on success, or a non-zero error code on failure:
/// - -1: list pointer is NULL
/// - -2: index is out of bounds
/// - -3: list pointer is invalid
///
/// # Safety
///
/// The `list` pointer must be either NULL or point to a valid KoiCompositeList object.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_SetFloatValue(
    list: *mut KoiCompositeList,
    index: usize,
    value: f64,
) -> i32 {
    if list.is_null() {
        return -1;
    }

    let param = unsafe { &mut *(list as *mut Parameter) };
    match param {
        Parameter::Composite(_, CompositeValue::List(values)) => {
            if index >= values.len() {
                return -2;
            }

            values[index] = Value::Float(value);
            0
        }
        _ => -3,
    }
}

/// Set string value in composite list by index
///
/// This function replaces the value at the specified index with a string value.
/// The index must be within the bounds of the list.
/// The string is copied and managed by the list.
///
/// # Arguments
///
/// * `list` - Pointer to the composite list parameter
/// * `index` - Zero-based index of the value to replace
/// * `value` - New string value (null-terminated C string)
///
/// # Returns
///
/// 0 on success, or a non-zero error code on failure:
/// - -1: list pointer is NULL or value is NULL
/// - -2: value contains invalid UTF-8
/// - -3: list pointer is invalid
///
/// # Safety
///
/// The `list` pointer must be either NULL or point to a valid KoiCompositeList object.
/// The `value` pointer must be either NULL or point to a valid null-terminated string.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_SetStringValue(
    list: *mut KoiCompositeList,
    index: usize,
    value: *const c_char,
) -> i32 {
    if list.is_null() || value.is_null() {
        return -1;
    }

    let value_str = match unsafe { CStr::from_ptr(value) }.to_str() {
        Ok(s) => s.to_string(),
        Err(_) => return -2,
    };

    let param = unsafe { &mut *(list as *mut Parameter) };
    match param {
        Parameter::Composite(_, CompositeValue::List(values)) => {
            if index >= values.len() {
                return -2;
            }

            values[index] = Value::String(value_str);
            0
        }
        _ => -3,
    }
}

/// Remove value from composite list by index
///
/// This function removes the value at the specified index from the list.
/// The index must be within the bounds of the list.
///
/// # Arguments
///
/// * `list` - Pointer to the composite list parameter
/// * `index` - Zero-based index of the value to remove
///
/// # Returns
///
/// 0 on success, or a non-zero error code on failure:
/// - -1: list pointer is NULL
/// - -2: index is out of bounds
/// - -3: list pointer is invalid
///
/// # Safety
///
/// The `list` pointer must be either NULL or point to a valid KoiCompositeList object.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_RemoveValue(
    list: *mut KoiCompositeList,
    index: usize,
) -> i32 {
    if list.is_null() {
        return -1;
    }

    let param = unsafe { &mut *(list as *mut Parameter) };
    match param {
        Parameter::Composite(_, CompositeValue::List(values)) => {
            if index >= values.len() {
                return -2;
            }

            values.remove(index);
            0
        }
        _ => -3,
    }
}

/// Clear all values from composite list
///
/// This function removes all values from the list, making it empty.
///
/// # Arguments
///
/// * `list` - Pointer to the composite list parameter
///
/// # Returns
///
/// 0 on success, or a non-zero error code on failure:
/// - -1: list pointer is NULL
/// - -3: list pointer is invalid
///
/// # Safety
///
/// The `list` pointer must be either NULL or point to a valid KoiCompositeList object.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_Clear(list: *mut KoiCompositeList) -> i32 {
    if list.is_null() {
        return -1;
    }

    let param = unsafe { &mut *(list as *mut Parameter) };
    match param {
        Parameter::Composite(_, CompositeValue::List(values)) => {
            values.clear();
            0
        }
        _ => -3,
    }
}

/// Free composite list parameter
///
/// This function frees the memory used by a composite list parameter.
/// After calling this function, the list pointer becomes invalid and must not be used.
///
/// # Arguments
///
/// * `list` - Pointer to the composite list parameter to delete
///
/// # Safety
///
/// The `list` pointer must be either NULL or point to a valid KoiCompositeList object
/// obtained from KoiCommand_GetCompositeList or KoiCompositeList_New.
/// After this function returns, the pointer is invalid and must not be used.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_Del(list: *mut KoiCompositeList) {
    if list.is_null() {
        return;
    }

    unsafe { drop(Box::from_raw(list as *mut Parameter)) };
}

/// Get boolean value from composite list by index
///
/// # Arguments
/// * `list` - Pointer to the composite list parameter
/// * `index` - Zero-based index of the value to retrieve
/// * `out_value` - Pointer to store boolean value (1 for true, 0 for false)
///
/// # Returns
/// 0 on success, non-zero on error
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_GetBoolValue(
    list: *mut KoiCompositeList,
    index: usize,
    out_value: *mut i32,
) -> i32 {
    if list.is_null() || out_value.is_null() {
        return -1;
    }

    let param = unsafe { &*(list as *const Parameter) };
    match param {
        Parameter::Composite(_, CompositeValue::List(values)) => {
            if index >= values.len() {
                return -2;
            }

            match &values[index] {
                Value::Bool(value) => {
                    unsafe { *out_value = if *value { 1 } else { 0 } };
                    0
                }
                _ => -3,
            }
        }
        _ => -4,
    }
}

/// Add boolean value to composite list
///
/// # Arguments
/// * `list` - Pointer to the composite list parameter
/// * `value` - Boolean value to add (non-zero for true, 0 for false)
///
/// # Returns
/// 0 on success, non-zero on error
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_AddBoolValue(
    list: *mut KoiCompositeList,
    value: i32,
) -> i32 {
    if list.is_null() {
        return -1;
    }

    let param = unsafe { &mut *(list as *mut Parameter) };
    match param {
        Parameter::Composite(_, CompositeValue::List(values)) => {
            values.push(Value::Bool(value != 0));
            0
        }
        _ => -3,
    }
}

/// Set boolean value in composite list by index
///
/// # Arguments
/// * `list` - Pointer to the composite list parameter
/// * `index` - Zero-based index of the value to replace
/// * `value` - New boolean value (non-zero for true, 0 for false)
///
/// # Returns
/// 0 on success, non-zero on error
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCompositeList_SetBoolValue(
    list: *mut KoiCompositeList,
    index: usize,
    value: i32,
) -> i32 {
    if list.is_null() {
        return -1;
    }

    let param = unsafe { &mut *(list as *mut Parameter) };
    match param {
        Parameter::Composite(_, CompositeValue::List(values)) => {
            if index >= values.len() {
                return -2;
            }

            values[index] = Value::Bool(value != 0);
            0
        }
        _ => -3,
    }
}