atc_router/ffi/
schema.rs

1use crate::ast::Type;
2use crate::schema::Schema;
3use std::ffi;
4use std::os::raw::c_char;
5
6#[no_mangle]
7pub extern "C" fn schema_new() -> *mut Schema {
8    Box::into_raw(Box::default())
9}
10
11/// Deallocate the schema object.
12///
13/// # Errors
14///
15/// This function never fails.
16///
17/// # Safety
18///
19/// Violating any of the following constraints will result in undefined behavior:
20///
21/// - `schema` must be a valid pointer returned by [`schema_new`].
22#[no_mangle]
23pub unsafe extern "C" fn schema_free(schema: *mut Schema) {
24    drop(Box::from_raw(schema));
25}
26
27/// Add a new field with the specified type to the schema.
28///
29/// # Arguments
30///
31/// - `schema`: a valid pointer to the [`Schema`] object returned by [`schema_new`].
32/// - `field`: the C-style string representing the field name.
33/// - `typ`: the type of the field.
34///
35/// # Panics
36///
37/// This function will panic if the C-style string
38/// pointed by `field` is not a valid UTF-8 string.
39///
40/// # Safety
41///
42/// Violating any of the following constraints will result in undefined behavior:
43///
44/// - `schema` must be a valid pointer returned by [`schema_new`].
45/// - `field` must be a valid pointer to a C-style string, must be properly aligned,
46///   and must not have '\0' in the middle.
47#[no_mangle]
48pub unsafe extern "C" fn schema_add_field(schema: &mut Schema, field: *const i8, typ: Type) {
49    let field = ffi::CStr::from_ptr(field as *const c_char)
50        .to_str()
51        .unwrap();
52
53    schema.add_field(field, typ)
54}