koicore_ffi 0.2.3

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

use super::command::KoiCommand;

/// Unified parameter type enumeration
///
/// This enumeration represents all possible parameter types in KoiLang commands.
/// It includes both basic types (int, float, string) and composite types (single, list, dict).
#[repr(C)]
pub enum KoiParamType {
    /// 64-bit signed integer value
    BasicInt = 0,
    /// 64-bit floating point value
    BasicFloat = 1,
    /// UTF-8 string value (merged Literal and String into single String type)
    BasicString = 2,
    /// Single composite value
    CompositeSingle = 3,
    /// List composite value
    CompositeList = 4,
    /// Dictionary composite value
    CompositeDict = 5,
    /// Invalid or unknown type
    Invalid = -1,
    /// Boolean value
    BasicBool = 6,
}

/// Get number of parameters in command
///
/// # Arguments
/// * `command` - Command object pointer
///
/// # Returns
/// Number of parameters, or 0 if command is null
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_GetParamCount(command: *mut KoiCommand) -> usize {
    if command.is_null() {
        return 0;
    }

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

/// Get parameter type (unified enum for both basic and composite types)
///
/// # Arguments
/// * `command` - Command object pointer
/// * `index` - Parameter index
///
/// # Returns
/// Parameter type, or KoiParamType::Invalid on error
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_GetParamType(command: *mut KoiCommand, index: usize) -> i32 {
    if command.is_null() {
        return KoiParamType::Invalid as i32;
    }

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

    if index >= params.len() {
        return KoiParamType::Invalid as i32;
    }

    match &params[index] {
        Parameter::Basic(value) => match value {
            Value::Int(_) => KoiParamType::BasicInt as i32,
            Value::Float(_) => KoiParamType::BasicFloat as i32,
            Value::String(_) => KoiParamType::BasicString as i32,
            Value::Bool(_) => KoiParamType::BasicBool as i32,
        },
        Parameter::Composite(_, composite) => match composite {
            CompositeValue::Single(_) => KoiParamType::CompositeSingle as i32,
            CompositeValue::List(_) => KoiParamType::CompositeList as i32,
            CompositeValue::Dict(_) => KoiParamType::CompositeDict as i32,
        },
    }
}

/// Get integer value from basic parameter
///
/// # Arguments
/// * `command` - Command object pointer
/// * `index` - Parameter index
/// * `out_value` - Pointer to store integer value
///
/// # Returns
/// 0 on success, non-zero on error or type mismatch
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_GetIntParam(
    command: *mut KoiCommand,
    index: usize,
    out_value: *mut i64,
) -> i32 {
    if command.is_null() || out_value.is_null() {
        return -1;
    }

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

    if index >= params.len() {
        return -2;
    }

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

/// Get float value from basic parameter
///
/// # Arguments
/// * `command` - Command object pointer
/// * `index` - Parameter index
/// * `out_value` - Pointer to store float value
///
/// # Returns
/// 0 on success, non-zero on error or type mismatch
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_GetFloatParam(
    command: *mut KoiCommand,
    index: usize,
    out_value: *mut f64,
) -> i32 {
    if command.is_null() || out_value.is_null() {
        return -1;
    }

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

    if index >= params.len() {
        return -2;
    }

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

/// Get string value from basic parameter into provided buffer
///
/// # Arguments
/// * `command` - Command object pointer
/// * `index` - Parameter index
/// * `buffer` - Buffer for string output
/// * `buffer_size` - Buffer size
///
/// # Returns
/// Actual string length (excluding null terminator), or required buffer size if insufficient
/// Returns 0 on error or type mismatch
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_GetStringParam(
    command: *mut KoiCommand,
    index: usize,
    buffer: *mut c_char,
    buffer_size: usize,
) -> usize {
    if command.is_null() {
        return 0;
    }

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

    if index >= params.len() {
        return 0;
    }

    let value_str = match &params[index] {
        Parameter::Basic(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.is_null() || 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
}

/// Get string parameter length
///
/// # Arguments
/// * `command` - Command object pointer
/// * `index` - Parameter index
///
/// # Returns
/// Required buffer size (including null terminator), or 0 on error
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_GetStringParamLen(
    command: *mut KoiCommand,
    index: usize,
) -> usize {
    if command.is_null() {
        return 0;
    }

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

    if index >= params.len() {
        return 0;
    }

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

/// Get composite parameter name into provided buffer
///
/// # Arguments
/// * `command` - Command object pointer
/// * `index` - Parameter index
/// * `buffer` - Buffer for name output
/// * `buffer_size` - Buffer size
///
/// # Returns
/// Actual name length (excluding null terminator), or required buffer size if insufficient
/// Returns 0 on error or if parameter is not composite
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_GetCompositeParamName(
    command: *mut KoiCommand,
    index: usize,
    buffer: *mut c_char,
    buffer_size: usize,
) -> usize {
    if command.is_null() {
        return 0;
    }

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

    if index >= params.len() {
        return 0;
    }

    let name = match &params[index] {
        Parameter::Composite(name, _) => name,
        _ => return 0,
    };

    let name_bytes = name.as_bytes();
    let name_len = name_bytes.len();
    let required_size = name_len + 1;

    if buffer.is_null() || buffer_size < required_size {
        return required_size;
    }

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

    required_size
}

/// Get composite parameter name length
///
/// # Arguments
/// * `command` - Command object pointer
/// * `index` - Parameter index
///
/// # Returns
/// Required buffer size (including null terminator), or 0 on error
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_GetCompositeParamNameLen(
    command: *mut KoiCommand,
    index: usize,
) -> usize {
    if command.is_null() {
        return 0;
    }

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

    if index >= params.len() {
        return 0;
    }

    match &params[index] {
        Parameter::Composite(name, _) => name.len() + 1,
        _ => 0,
    }
}

/// Check if command is a text command (@text)
///
/// # Arguments
/// * `command` - Command object pointer
///
/// # Returns
/// 1 if text command, 0 otherwise or on error
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_IsTextCommand(command: *mut KoiCommand) -> i32 {
    if command.is_null() {
        return 0;
    }

    let command = unsafe { &*(command as *mut Command) };
    (command.name() == "@text") as i32
}

/// Check if command is an annotation command (@annotation)
///
/// # Arguments
/// * `command` - Command object pointer
///
/// # Returns
/// 1 if annotation command, 0 otherwise or on error
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_IsAnnotationCommand(command: *mut KoiCommand) -> i32 {
    if command.is_null() {
        return 0;
    }

    let command = unsafe { &*(command as *mut Command) };
    (command.name() == "@annotation") as i32
}

/// Check if command is a number command (@number)
///
/// # Arguments
/// * `command` - Command object pointer
///
/// # Returns
/// 1 if number command, 0 otherwise or on error
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_IsNumberCommand(command: *mut KoiCommand) -> i32 {
    if command.is_null() {
        return 0;
    }

    let command = unsafe { &*(command as *mut Command) };
    (command.name() == "@number") as i32
}

/// Add a new integer parameter to command
///
/// # Arguments
/// * `command` - Command object pointer
/// * `value` - Integer value
///
/// # Returns
/// 0 on success, non-zero on error
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_AddIntParameter(command: *mut KoiCommand, value: i64) -> i32 {
    if command.is_null() {
        return -1;
    }

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

/// Add a new float parameter to command
///
/// # Arguments
/// * `command` - Command object pointer
/// * `value` - Float value
///
/// # Returns
/// 0 on success, non-zero on error
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_AddFloatParameter(command: *mut KoiCommand, value: f64) -> i32 {
    if command.is_null() {
        return -1;
    }

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

/// Add a new string parameter to command
///
/// # Arguments
/// * `command` - Command object pointer
/// * `value` - String value (null-terminated C string)
///
/// # Returns
/// 0 on success, non-zero on error
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_AddStringParameter(
    command: *mut KoiCommand,
    value: *const c_char,
) -> i32 {
    if command.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 command = unsafe { &mut *(command as *mut Command) };
    command.params.push(Value::String(value_str).into());
    0
}

/// Remove parameter from command by index
///
/// # Arguments
/// * `command` - Command object pointer
/// * `index` - Parameter index to remove
///
/// # Returns
/// 0 on success, non-zero on error
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_RemoveParameter(command: *mut KoiCommand, index: usize) -> i32 {
    if command.is_null() {
        return -1;
    }

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

/// Clear all parameters from command
///
/// # Arguments
/// * `command` - Command object pointer
///
/// # Returns
/// 0 on success, non-zero on error
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_ClearParameters(command: *mut KoiCommand) -> i32 {
    if command.is_null() {
        return -1;
    }

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

/// Modify integer parameter value
///
/// # Arguments
/// * `command` - Command object pointer
/// * `index` - Parameter index
/// * `value` - New integer value
///
/// # Returns
/// 0 on success, non-zero on error or type mismatch
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_SetIntParameter(
    command: *mut KoiCommand,
    index: usize,
    value: i64,
) -> i32 {
    if command.is_null() {
        return -1;
    }

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

    if index >= params.len() {
        return -2;
    }

    match &mut params[index] {
        Parameter::Basic(Value::Int(old_value)) => {
            *old_value = value;
            0
        }
        _ => -3,
    }
}

/// Modify float parameter value
///
/// # Arguments
/// * `command` - Command object pointer
/// * `index` - Parameter index
/// * `value` - New float value
///
/// # Returns
/// 0 on success, non-zero on error or type mismatch
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_SetFloatParameter(
    command: *mut KoiCommand,
    index: usize,
    value: f64,
) -> i32 {
    if command.is_null() {
        return -1;
    }

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

    if index >= params.len() {
        return -2;
    }

    match &mut params[index] {
        Parameter::Basic(Value::Float(old_value)) => {
            *old_value = value;
            0
        }
        _ => -3,
    }
}

/// Modify string parameter value
///
/// # Arguments
/// * `command` - Command object pointer
/// * `index` - Parameter index
/// * `value` - New string value (null-terminated C string)
///
/// # Returns
/// 0 on success, non-zero on error or type mismatch
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_SetStringParameter(
    command: *mut KoiCommand,
    index: usize,
    value: *const c_char,
) -> i32 {
    if command.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 command = unsafe { &mut *(command as *mut Command) };
    let params = &mut command.params;

    if index >= params.len() {
        return -3;
    }

    match &mut params[index] {
        Parameter::Basic(Value::String(old_value)) => {
            *old_value = value_str;
            0
        }
        _ => -4,
    }
}

/// Get boolean value from basic parameter
///
/// # Arguments
/// * `command` - Command object pointer
/// * `index` - Parameter index
/// * `out_value` - Pointer to store boolean value (1 for true, 0 for false)
///
/// # Returns
/// 0 on success, non-zero on error or type mismatch
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_GetBoolParam(
    command: *mut KoiCommand,
    index: usize,
    out_value: *mut i32,
) -> i32 {
    if command.is_null() || out_value.is_null() {
        return -1;
    }

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

    if index >= params.len() {
        return -2;
    }

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

/// Add a new boolean parameter to command
///
/// # Arguments
/// * `command` - Command object pointer
/// * `value` - 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 KoiCommand_AddBoolParameter(command: *mut KoiCommand, value: i32) -> i32 {
    if command.is_null() {
        return -1;
    }

    let command = unsafe { &mut *(command as *mut Command) };
    command.params.push(Value::Bool(value != 0).into());
    0
}

/// Modify boolean parameter value
///
/// # Arguments
/// * `command` - Command object pointer
/// * `index` - Parameter index
/// * `value` - New boolean value (non-zero for true, 0 for false)
///
/// # Returns
/// 0 on success, non-zero on error or type mismatch
#[unsafe(no_mangle)]
pub unsafe extern "C" fn KoiCommand_SetBoolParameter(
    command: *mut KoiCommand,
    index: usize,
    value: i32,
) -> i32 {
    if command.is_null() {
        return -1;
    }

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

    if index >= params.len() {
        return -2;
    }

    match &mut params[index] {
        Parameter::Basic(Value::Bool(old_value)) => {
            *old_value = value != 0;
            0
        }
        _ => -3,
    }
}