spikard-ffi 0.16.1

Rust-centric multi-language HTTP framework with polyglot bindings
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
#![allow(
    clippy::too_many_arguments,
    clippy::not_unsafe_ptr_arg_deref,
    unused_variables,
    unused_mut
)]

use std::ffi::{c_char, c_void, CStr, CString};
use std::panic;
use std::sync::Arc;
/// FFI handler bridge for the `Handler` contract.
///
/// Wraps a C callback function pointer so it can be called from Rust async code.
/// The callback receives JSON-serialized request and returns JSON response.
pub struct FfiHandlerBridge {
    callback: extern "C" fn(*mut c_void, *const c_char) -> *mut c_char,
    context: *mut c_void,
}

// SAFETY: The C callback function pointer and context pointer are opaque handles.
// The caller is responsible for maintaining the invariant that the context
// pointer remains valid for the lifetime of the bridge. The callback itself
// must be safe to call from async Rust code.
unsafe impl Send for FfiHandlerBridge {}
unsafe impl Sync for FfiHandlerBridge {}
impl spikard::Handler for FfiHandlerBridge {
    fn call(
        &self,
        _request: spikard::Request<spikard::Body>,
        request_data: spikard::RequestData,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = spikard::HandlerResult> + Send + '_>> {
        Box::pin(async move {
            let outcome: Result<spikard::Response, Box<dyn std::error::Error + Send + Sync>> = async move {
                // Serialize request to JSON
                let req_json = serde_json::to_string(&request_data)
                    .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;
                let req_c_str =
                    CString::new(req_json).map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;

                // Call the C callback on a blocking thread to avoid stalling the async executor.
                // Raw pointers are not `Send`, so the context and result pointers cross the
                // spawn_blocking boundary as `usize`; the owned `CString` moves in to stay alive.
                let callback = self.callback;
                let context = self.context as usize;
                let resp_addr = tokio::task::spawn_blocking(move || {
                    (callback)(context as *mut c_void, req_c_str.as_ptr()) as usize
                })
                .await
                .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;
                let resp_ptr = resp_addr as *mut c_char;

                if resp_ptr.is_null() {
                    return Err("C callback returned null response".into());
                }

                // SAFETY: resp_ptr was returned by the C callback and must be a null-terminated string.
                let resp_c_str = unsafe { CStr::from_ptr(resp_ptr) };
                let resp_json = resp_c_str.to_string_lossy();

                // Deserialize response from JSON
                let response: spikard::Response = serde_json::from_str(&resp_json)
                    .map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;

                // Free the C-allocated response string. The host allocates it via malloc/strdup
                // and hands ownership to us; we release it with the C runtime's free.
                // SAFETY: resp_ptr is null-checked above and was produced by the host callback.
                unsafe {
                    extern "C" {
                        fn free(ptr: *mut c_void);
                    }
                    free(resp_ptr as *mut c_void);
                }

                Ok(response)
            }
            .await;

            spikard::handler_result_from_response(outcome)
        })
    }
}
/// Opaque handle to a App service instance.
/// Allocated by spikard_app_new(), freed by spikard_app_free().
///
/// `inner` is `Option<Box<…>>` so a `Finalize` entrypoint can `.take()` the
/// owner out without invalidating the C pointer. Consumers must still call
/// `spikard_app_free()` after a `Finalize` returns to release the opaque
/// shell; the shell drops trivially when `inner` is `None`.
#[repr(C)]
pub struct AppOpaque {
    inner: Option<Box<spikard::App>>,
}

/// Allocate a new App instance.
///
/// # Safety
/// The returned pointer must be freed via spikard_app_free().
/// Never access the pointer after freeing it.
#[no_mangle]
pub extern "C" fn spikard_app_new() -> *mut AppOpaque {
    let owner = spikard::App::new();
    Box::into_raw(Box::new(AppOpaque {
        inner: Some(Box::new(owner)),
    }))
}

/// Free a App instance allocated by spikard_app_new().
///
/// # Safety
/// - `ptr` must have been allocated by spikard_app_new().
/// - After this call, `ptr` is invalid and must not be dereferenced.
/// - Calling this twice on the same pointer causes undefined behavior.
/// - Safe to call even after a `Finalize` entrypoint has emptied `inner`.
#[no_mangle]
pub extern "C" fn spikard_app_free(ptr: *mut AppOpaque) {
    if !ptr.is_null() {
        // SAFETY: ptr was allocated by into_raw above;
        // we are the sole owner and this is the final drop.
        unsafe {
            drop(Box::from_raw(ptr));
        }
    }
}
/// Register a handler callback for method 'route'.
///
/// # Safety
/// - `owner` must be a valid pointer returned by `spikard_app_new()` and not yet freed.
/// - `callback` must be a valid function pointer that remains valid for the lifetime
///   of this service instance.
/// - `context` is an opaque pointer passed to the callback on each invocation.
///   The caller is responsible for keeping it valid.
/// Returns 0 on success, non-zero error code on failure.
#[no_mangle]
pub extern "C" fn spikard_app_register_route(
    owner: *mut AppOpaque,
    callback: extern "C" fn(*mut c_void, *const c_char) -> *mut c_char,
    context: *mut c_void,
    builder: *mut spikard::RouteBuilder,
) -> i32 {
    if owner.is_null() {
        return 1; // Error: null pointer
    }
    if builder.is_null() {
        return 1; // Error: null pointer;
    }

    // SAFETY: pointer was produced by the matching opaque `_new`/builder export.
    // Borrow it as a reference; caller retains ownership and is responsible for freeing.
    let builder = unsafe { &*builder };

    let bridge = FfiHandlerBridge { callback, context };
    let handler: Arc<dyn spikard::Handler> = Arc::new(bridge);

    // SAFETY: owner was allocated by _new() and is valid until freed.
    match unsafe {
        match (*owner).inner.as_mut() {
            Some(owner_ref) => owner_ref.route(builder.clone(), handler),
            None => return 1, // Error: service already consumed
        }
    } {
        Ok(_) => 0,  // Success
        Err(_) => 1, // Error
    }
}
/// Register a GET route at the given path.
///
/// # Safety
/// - `owner` must be a valid pointer returned by `spikard_app_new()` and not yet freed.
/// - `callback` must be a valid function pointer that remains valid for the lifetime
///   of this service instance.
/// - `context` is an opaque pointer passed to the callback on each invocation.
///   The caller is responsible for keeping it valid.
/// Returns 0 on success, non-zero error code on failure.
#[no_mangle]
pub extern "C" fn spikard_app_get(
    owner: *mut AppOpaque,
    callback: extern "C" fn(*mut c_void, *const c_char) -> *mut c_char,
    context: *mut c_void,
    path: *const c_char,
) -> i32 {
    if owner.is_null() {
        return 1; // Error: null pointer
    }
    if path.is_null() {
        return 1; // Error: null pointer;
    }

    let path = if path.is_null() {
        String::new()
    } else {
        // SAFETY: caller guarantees a valid null-terminated C string.
        unsafe { CStr::from_ptr(path) }.to_string_lossy().into_owned()
    };

    let builder = spikard::RouteBuilder::new(spikard::Method::Get, path);

    let bridge = FfiHandlerBridge { callback, context };
    let handler: Arc<dyn spikard::Handler> = Arc::new(bridge);

    // SAFETY: owner was allocated by _new() and is valid until freed.
    match unsafe {
        match (*owner).inner.as_mut() {
            Some(owner_ref) => owner_ref.route(builder, handler),
            None => return 1, // Error: service already consumed
        }
    } {
        Ok(_) => 0,  // Success
        Err(_) => 1, // Error
    }
}
/// Register a POST route at the given path.
///
/// # Safety
/// - `owner` must be a valid pointer returned by `spikard_app_new()` and not yet freed.
/// - `callback` must be a valid function pointer that remains valid for the lifetime
///   of this service instance.
/// - `context` is an opaque pointer passed to the callback on each invocation.
///   The caller is responsible for keeping it valid.
/// Returns 0 on success, non-zero error code on failure.
#[no_mangle]
pub extern "C" fn spikard_app_post(
    owner: *mut AppOpaque,
    callback: extern "C" fn(*mut c_void, *const c_char) -> *mut c_char,
    context: *mut c_void,
    path: *const c_char,
) -> i32 {
    if owner.is_null() {
        return 1; // Error: null pointer
    }
    if path.is_null() {
        return 1; // Error: null pointer;
    }

    let path = if path.is_null() {
        String::new()
    } else {
        // SAFETY: caller guarantees a valid null-terminated C string.
        unsafe { CStr::from_ptr(path) }.to_string_lossy().into_owned()
    };

    let builder = spikard::RouteBuilder::new(spikard::Method::Post, path);

    let bridge = FfiHandlerBridge { callback, context };
    let handler: Arc<dyn spikard::Handler> = Arc::new(bridge);

    // SAFETY: owner was allocated by _new() and is valid until freed.
    match unsafe {
        match (*owner).inner.as_mut() {
            Some(owner_ref) => owner_ref.route(builder, handler),
            None => return 1, // Error: service already consumed
        }
    } {
        Ok(_) => 0,  // Success
        Err(_) => 1, // Error
    }
}
/// Register a PUT route at the given path.
///
/// # Safety
/// - `owner` must be a valid pointer returned by `spikard_app_new()` and not yet freed.
/// - `callback` must be a valid function pointer that remains valid for the lifetime
///   of this service instance.
/// - `context` is an opaque pointer passed to the callback on each invocation.
///   The caller is responsible for keeping it valid.
/// Returns 0 on success, non-zero error code on failure.
#[no_mangle]
pub extern "C" fn spikard_app_put(
    owner: *mut AppOpaque,
    callback: extern "C" fn(*mut c_void, *const c_char) -> *mut c_char,
    context: *mut c_void,
    path: *const c_char,
) -> i32 {
    if owner.is_null() {
        return 1; // Error: null pointer
    }
    if path.is_null() {
        return 1; // Error: null pointer;
    }

    let path = if path.is_null() {
        String::new()
    } else {
        // SAFETY: caller guarantees a valid null-terminated C string.
        unsafe { CStr::from_ptr(path) }.to_string_lossy().into_owned()
    };

    let builder = spikard::RouteBuilder::new(spikard::Method::Put, path);

    let bridge = FfiHandlerBridge { callback, context };
    let handler: Arc<dyn spikard::Handler> = Arc::new(bridge);

    // SAFETY: owner was allocated by _new() and is valid until freed.
    match unsafe {
        match (*owner).inner.as_mut() {
            Some(owner_ref) => owner_ref.route(builder, handler),
            None => return 1, // Error: service already consumed
        }
    } {
        Ok(_) => 0,  // Success
        Err(_) => 1, // Error
    }
}
/// Register a PATCH route at the given path.
///
/// # Safety
/// - `owner` must be a valid pointer returned by `spikard_app_new()` and not yet freed.
/// - `callback` must be a valid function pointer that remains valid for the lifetime
///   of this service instance.
/// - `context` is an opaque pointer passed to the callback on each invocation.
///   The caller is responsible for keeping it valid.
/// Returns 0 on success, non-zero error code on failure.
#[no_mangle]
pub extern "C" fn spikard_app_patch(
    owner: *mut AppOpaque,
    callback: extern "C" fn(*mut c_void, *const c_char) -> *mut c_char,
    context: *mut c_void,
    path: *const c_char,
) -> i32 {
    if owner.is_null() {
        return 1; // Error: null pointer
    }
    if path.is_null() {
        return 1; // Error: null pointer;
    }

    let path = if path.is_null() {
        String::new()
    } else {
        // SAFETY: caller guarantees a valid null-terminated C string.
        unsafe { CStr::from_ptr(path) }.to_string_lossy().into_owned()
    };

    let builder = spikard::RouteBuilder::new(spikard::Method::Patch, path);

    let bridge = FfiHandlerBridge { callback, context };
    let handler: Arc<dyn spikard::Handler> = Arc::new(bridge);

    // SAFETY: owner was allocated by _new() and is valid until freed.
    match unsafe {
        match (*owner).inner.as_mut() {
            Some(owner_ref) => owner_ref.route(builder, handler),
            None => return 1, // Error: service already consumed
        }
    } {
        Ok(_) => 0,  // Success
        Err(_) => 1, // Error
    }
}
/// Register a DELETE route at the given path.
///
/// # Safety
/// - `owner` must be a valid pointer returned by `spikard_app_new()` and not yet freed.
/// - `callback` must be a valid function pointer that remains valid for the lifetime
///   of this service instance.
/// - `context` is an opaque pointer passed to the callback on each invocation.
///   The caller is responsible for keeping it valid.
/// Returns 0 on success, non-zero error code on failure.
#[no_mangle]
pub extern "C" fn spikard_app_delete(
    owner: *mut AppOpaque,
    callback: extern "C" fn(*mut c_void, *const c_char) -> *mut c_char,
    context: *mut c_void,
    path: *const c_char,
) -> i32 {
    if owner.is_null() {
        return 1; // Error: null pointer
    }
    if path.is_null() {
        return 1; // Error: null pointer;
    }

    let path = if path.is_null() {
        String::new()
    } else {
        // SAFETY: caller guarantees a valid null-terminated C string.
        unsafe { CStr::from_ptr(path) }.to_string_lossy().into_owned()
    };

    let builder = spikard::RouteBuilder::new(spikard::Method::Delete, path);

    let bridge = FfiHandlerBridge { callback, context };
    let handler: Arc<dyn spikard::Handler> = Arc::new(bridge);

    // SAFETY: owner was allocated by _new() and is valid until freed.
    match unsafe {
        match (*owner).inner.as_mut() {
            Some(owner_ref) => owner_ref.route(builder, handler),
            None => return 1, // Error: service already consumed
        }
    } {
        Ok(_) => 0,  // Success
        Err(_) => 1, // Error
    }
}
/// Register a HEAD route at the given path.
///
/// # Safety
/// - `owner` must be a valid pointer returned by `spikard_app_new()` and not yet freed.
/// - `callback` must be a valid function pointer that remains valid for the lifetime
///   of this service instance.
/// - `context` is an opaque pointer passed to the callback on each invocation.
///   The caller is responsible for keeping it valid.
/// Returns 0 on success, non-zero error code on failure.
#[no_mangle]
pub extern "C" fn spikard_app_head(
    owner: *mut AppOpaque,
    callback: extern "C" fn(*mut c_void, *const c_char) -> *mut c_char,
    context: *mut c_void,
    path: *const c_char,
) -> i32 {
    if owner.is_null() {
        return 1; // Error: null pointer
    }
    if path.is_null() {
        return 1; // Error: null pointer;
    }

    let path = if path.is_null() {
        String::new()
    } else {
        // SAFETY: caller guarantees a valid null-terminated C string.
        unsafe { CStr::from_ptr(path) }.to_string_lossy().into_owned()
    };

    let builder = spikard::RouteBuilder::new(spikard::Method::Head, path);

    let bridge = FfiHandlerBridge { callback, context };
    let handler: Arc<dyn spikard::Handler> = Arc::new(bridge);

    // SAFETY: owner was allocated by _new() and is valid until freed.
    match unsafe {
        match (*owner).inner.as_mut() {
            Some(owner_ref) => owner_ref.route(builder, handler),
            None => return 1, // Error: service already consumed
        }
    } {
        Ok(_) => 0,  // Success
        Err(_) => 1, // Error
    }
}
/// Register an OPTIONS route at the given path.
///
/// # Safety
/// - `owner` must be a valid pointer returned by `spikard_app_new()` and not yet freed.
/// - `callback` must be a valid function pointer that remains valid for the lifetime
///   of this service instance.
/// - `context` is an opaque pointer passed to the callback on each invocation.
///   The caller is responsible for keeping it valid.
/// Returns 0 on success, non-zero error code on failure.
#[no_mangle]
pub extern "C" fn spikard_app_options(
    owner: *mut AppOpaque,
    callback: extern "C" fn(*mut c_void, *const c_char) -> *mut c_char,
    context: *mut c_void,
    path: *const c_char,
) -> i32 {
    if owner.is_null() {
        return 1; // Error: null pointer
    }
    if path.is_null() {
        return 1; // Error: null pointer;
    }

    let path = if path.is_null() {
        String::new()
    } else {
        // SAFETY: caller guarantees a valid null-terminated C string.
        unsafe { CStr::from_ptr(path) }.to_string_lossy().into_owned()
    };

    let builder = spikard::RouteBuilder::new(spikard::Method::Options, path);

    let bridge = FfiHandlerBridge { callback, context };
    let handler: Arc<dyn spikard::Handler> = Arc::new(bridge);

    // SAFETY: owner was allocated by _new() and is valid until freed.
    match unsafe {
        match (*owner).inner.as_mut() {
            Some(owner_ref) => owner_ref.route(builder, handler),
            None => return 1, // Error: service already consumed
        }
    } {
        Ok(_) => 0,  // Success
        Err(_) => 1, // Error
    }
}
/// Register a CONNECT route at the given path.
///
/// # Safety
/// - `owner` must be a valid pointer returned by `spikard_app_new()` and not yet freed.
/// - `callback` must be a valid function pointer that remains valid for the lifetime
///   of this service instance.
/// - `context` is an opaque pointer passed to the callback on each invocation.
///   The caller is responsible for keeping it valid.
/// Returns 0 on success, non-zero error code on failure.
#[no_mangle]
pub extern "C" fn spikard_app_connect(
    owner: *mut AppOpaque,
    callback: extern "C" fn(*mut c_void, *const c_char) -> *mut c_char,
    context: *mut c_void,
    path: *const c_char,
) -> i32 {
    if owner.is_null() {
        return 1; // Error: null pointer
    }
    if path.is_null() {
        return 1; // Error: null pointer;
    }

    let path = if path.is_null() {
        String::new()
    } else {
        // SAFETY: caller guarantees a valid null-terminated C string.
        unsafe { CStr::from_ptr(path) }.to_string_lossy().into_owned()
    };

    let builder = spikard::RouteBuilder::new(spikard::Method::Connect, path);

    let bridge = FfiHandlerBridge { callback, context };
    let handler: Arc<dyn spikard::Handler> = Arc::new(bridge);

    // SAFETY: owner was allocated by _new() and is valid until freed.
    match unsafe {
        match (*owner).inner.as_mut() {
            Some(owner_ref) => owner_ref.route(builder, handler),
            None => return 1, // Error: service already consumed
        }
    } {
        Ok(_) => 0,  // Success
        Err(_) => 1, // Error
    }
}
/// Register a TRACE route at the given path.
///
/// # Safety
/// - `owner` must be a valid pointer returned by `spikard_app_new()` and not yet freed.
/// - `callback` must be a valid function pointer that remains valid for the lifetime
///   of this service instance.
/// - `context` is an opaque pointer passed to the callback on each invocation.
///   The caller is responsible for keeping it valid.
/// Returns 0 on success, non-zero error code on failure.
#[no_mangle]
pub extern "C" fn spikard_app_trace(
    owner: *mut AppOpaque,
    callback: extern "C" fn(*mut c_void, *const c_char) -> *mut c_char,
    context: *mut c_void,
    path: *const c_char,
) -> i32 {
    if owner.is_null() {
        return 1; // Error: null pointer
    }
    if path.is_null() {
        return 1; // Error: null pointer;
    }

    let path = if path.is_null() {
        String::new()
    } else {
        // SAFETY: caller guarantees a valid null-terminated C string.
        unsafe { CStr::from_ptr(path) }.to_string_lossy().into_owned()
    };

    let builder = spikard::RouteBuilder::new(spikard::Method::Trace, path);

    let bridge = FfiHandlerBridge { callback, context };
    let handler: Arc<dyn spikard::Handler> = Arc::new(bridge);

    // SAFETY: owner was allocated by _new() and is valid until freed.
    match unsafe {
        match (*owner).inner.as_mut() {
            Some(owner_ref) => owner_ref.route(builder, handler),
            None => return 1, // Error: service already consumed
        }
    } {
        Ok(_) => 0,  // Success
        Err(_) => 1, // Error
    }
}
/// Configure the service via 'config'.
///
/// # Safety
/// - `owner` must be a valid pointer returned by `spikard_app_new()` and not yet freed.
/// - The same `owner` pointer is returned on success — the caller does **not**
///   need to swap the handle they hold. Returns `null` on failure (the original
///   handle is still valid in that case but should be inspected for usability).
#[no_mangle]
pub extern "C" fn spikard_app_config(owner: *mut AppOpaque, config: *mut spikard::ServerConfig) -> *mut AppOpaque {
    if owner.is_null() {
        return std::ptr::null_mut();
    }
    if config.is_null() {
        return std::ptr::null_mut();
    }

    // SAFETY: pointer was produced by the matching opaque `_new`/builder export.
    // Borrow it as a reference; caller retains ownership and is responsible for freeing.
    let config = unsafe { &*config };

    // SAFETY: owner was allocated by _new() and is valid until freed.
    // Take the inner box out, transform it, and put the result back. The opaque
    // shell stays at the same address so the caller's handle remains valid.
    unsafe {
        let inner = match (*owner).inner.take() {
            Some(boxed) => *boxed,
            None => return std::ptr::null_mut(),
        };
        (*owner).inner = Some(Box::new(inner.config(config.clone())));
    }
    owner
}
/// Run the service entrypoint 'run'.
///
/// # Safety
/// - `owner` must be a valid pointer returned by `spikard_app_new()` and not yet freed.
/// - The inner owner value is moved out by this call and the opaque shell is left
///   with `inner = None`. The caller may still invoke `()`
///   afterwards to release the shell; subsequent registration/configurator calls
///   on the same pointer will fail with the null-return error code.
#[no_mangle]
pub extern "C" fn spikard_app_ep_run(owner: *mut AppOpaque) -> i32 {
    if owner.is_null() {
        return 1;
    }

    // SAFETY: owner was allocated by _new() and is valid until freed.
    // Move the inner owner out; leave `None` behind so the consumer's deferred
    // `_free` call drops the empty shell instead of touching a moved-from box.
    let inner = match unsafe { (*owner).inner.take() } {
        Some(boxed) => *boxed,
        None => return 1,
    };
    let rt = tokio::runtime::Runtime::new().expect("failed to create tokio runtime");
    match rt.block_on(inner.run()) {
        Ok(_) => 0,
        Err(_) => 1,
    }
}
/// Run the service entrypoint 'into_router'.
///
/// # Safety
/// - `owner` must be a valid pointer returned by `spikard_app_new()` and not yet freed.
/// - The inner owner value is moved out by this call and the opaque shell is left
///   with `inner = None`. The caller may still invoke `()`
///   afterwards to release the shell; subsequent registration/configurator calls
///   on the same pointer will fail with the null-return error code.
#[no_mangle]
pub extern "C" fn spikard_app_ep_into_router(owner: *mut AppOpaque) -> i32 {
    if owner.is_null() {
        return 1;
    }

    // SAFETY: owner was allocated by _new() and is valid until freed.
    // Move the inner owner out; leave `None` behind so the consumer's deferred
    // `_free` call drops the empty shell instead of touching a moved-from box.
    let inner = match unsafe { (*owner).inner.take() } {
        Some(boxed) => *boxed,
        None => return 1,
    };
    match inner.into_router() {
        Ok(_) => 0,
        Err(_) => 1,
    }
}