Skip to main content

Crate koicore_ffi

Crate koicore_ffi 

Source
Expand description

§KoiCore FFI

This crate provides a C-compatible foreign function interface (FFI) for the KoiCore library, enabling C, C++, and other languages to interact with KoiLang parsing functionality.

§Features

  • Parse KoiLang text from various input sources (strings, files, custom callbacks)
  • Access and manipulate KoiLang commands and parameters
  • Handle composite data structures (lists and dictionaries)
  • Comprehensive error handling with detailed error information
  • CMake build integration for easy embedding in C/C++ projects

§Modules

  • command: Functions for creating and manipulating KoiLang commands
  • parser: Functions for parsing KoiLang text and managing parser state
  • writer: Functions for writing KoiLang commands back to text

§CMake Integration

This crate provides full CMake build support using Corrosion. External projects can integrate koicore_ffi without manual Rust builds.

cmake_minimum_required(VERSION 3.22)
project(your_project LANGUAGES C CXX)

include(FetchContent)

# Fetch Corrosion (Rust-CMake integration tool)
FetchContent_Declare(
    Corrosion
    GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
    GIT_TAG v0.6
)

# Fetch koicore_ffi
FetchContent_Declare(
    koicore_ffi
    GIT_REPOSITORY https://github.com/Visecy/koicore.git
    GIT_TAG main
    SOURCE_SUBDIR crates/koicore_ffi
)

FetchContent_MakeAvailable(Corrosion koicore_ffi)

# Link using the namespaced target
add_executable(your_app main.c)
target_link_libraries(your_app PRIVATE KoiFFI::koicore_ffi)

§Method 2: Using find_package (After Installation)

# Install koicore_ffi
cd crates/koicore_ffi
mkdir build && cd build
cmake ..
cmake --build .
cmake --install . --prefix /usr/local
# In your CMakeLists.txt
find_package(KoicoreFFI REQUIRED)
add_executable(your_app main.c)
target_link_libraries(your_app PRIVATE KoiFFI::koicore_ffi)

§Safety

This FFI uses raw pointers and requires careful memory management. Users must:

  • Always check for null pointers before dereferencing
  • Properly free allocated objects using the provided _Del functions
  • Ensure thread safety when using the same parser from multiple threads
  • Follow the documentation for each function regarding parameter validation

§C Example

#include "koicore.h"

// Create a parser from a string
const char* text = "#command param1 param2";
struct KoiInputSource* input = KoiInputSource_FromString(text);
struct KoiParserConfig config;
KoiParserConfig_Init(&config);

struct KoiParser* parser = KoiParser_New(input, &config);

// Parse commands
struct KoiCommand* cmd = KoiParser_NextCommand(parser);
if (cmd) {
    // Process command
    KoiCommand_Del(cmd);
}

// Clean up
KoiParser_Del(parser);
KoiInputSource_Del(input);

Modules§

command
Command Module
parser
Parser Module
writer