|
1 | | --- SPDX-License-Identifier: PMPL-1.0-or-later |
2 | | -||| UNIVERSAL-CHAT-EXTRACTOR — FFI Bridge Declarations |
| 1 | +||| SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +||| Foreign Function Interface Declarations for UNIVERSAL_CHAT_EXTRACTOR |
3 | 3 | ||| |
4 | | -||| This module defines the formal bridge to the native chat-extraction |
5 | | -||| kernel. It provides the low-level signatures required to process |
6 | | -||| heterogeneous chat logs (Slack, Discord, Teams) via a unified API. |
| 4 | +||| This module declares all C-compatible functions that will be |
| 5 | +||| implemented in the Zig FFI layer. |
| 6 | +||| |
| 7 | +||| All functions are declared here with type signatures and safety proofs. |
| 8 | +||| Implementations live in ffi/zig/ |
7 | 9 |
|
8 | | -module UNIVERSAL_CHAT_EXTRACTOR.ABI.Foreign |
| 10 | +module UniversalChatExtractor.ABI.Foreign |
9 | 11 |
|
10 | | -import UNIVERSAL_CHAT_EXTRACTOR.ABI.Types |
11 | | -import UNIVERSAL_CHAT_EXTRACTOR.ABI.Layout |
| 12 | +import UniversalChatExtractor.ABI.Types |
| 13 | +import UniversalChatExtractor.ABI.Layout |
12 | 14 |
|
13 | 15 | %default total |
14 | 16 |
|
15 | 17 | -------------------------------------------------------------------------------- |
16 | | --- Lifecycle |
| 18 | +-- Library Lifecycle |
17 | 19 | -------------------------------------------------------------------------------- |
18 | 20 |
|
19 | | -||| Initializes the extraction engine. |
| 21 | +||| Initialize the library |
| 22 | +||| Returns a handle to the library instance, or Nothing on failure |
20 | 23 | export |
21 | | -%foreign "C:chat_extractor_init, libextractor" |
| 24 | +%foreign "C:universal_chat_extractor_init, libuniversal_chat_extractor" |
22 | 25 | prim__init : PrimIO Bits64 |
23 | 26 |
|
24 | | -||| Safe initialization wrapper. |
| 27 | +||| Safe wrapper for library initialization |
25 | 28 | export |
26 | 29 | init : IO (Maybe Handle) |
27 | 30 | init = do |
28 | 31 | ptr <- primIO prim__init |
29 | 32 | pure (createHandle ptr) |
30 | 33 |
|
31 | | -||| Shuts down the engine and releases native buffers. |
| 34 | +||| Clean up library resources |
32 | 35 | export |
33 | | -%foreign "C:chat_extractor_free, libextractor" |
| 36 | +%foreign "C:universal_chat_extractor_free, libuniversal_chat_extractor" |
34 | 37 | prim__free : Bits64 -> PrimIO () |
35 | 38 |
|
36 | | -||| Safe cleanup wrapper. |
| 39 | +||| Safe wrapper for cleanup |
37 | 40 | export |
38 | 41 | free : Handle -> IO () |
39 | 42 | free h = primIO (prim__free (handlePtr h)) |
| 43 | + |
| 44 | +-------------------------------------------------------------------------------- |
| 45 | +-- Core Operations |
| 46 | +-------------------------------------------------------------------------------- |
| 47 | + |
| 48 | +||| Example operation: process data |
| 49 | +export |
| 50 | +%foreign "C:universal_chat_extractor_process, libuniversal_chat_extractor" |
| 51 | +prim__process : Bits64 -> Bits32 -> PrimIO Bits32 |
| 52 | + |
| 53 | +||| Safe wrapper with error handling |
| 54 | +export |
| 55 | +process : Handle -> Bits32 -> IO (Either Result Bits32) |
| 56 | +process h input = do |
| 57 | + result <- primIO (prim__process (handlePtr h) input) |
| 58 | + pure $ case result of |
| 59 | + 0 => Left Error |
| 60 | + n => Right n |
| 61 | + |
| 62 | +-------------------------------------------------------------------------------- |
| 63 | +-- String Operations |
| 64 | +-------------------------------------------------------------------------------- |
| 65 | + |
| 66 | +||| Convert C string to Idris String |
| 67 | +export |
| 68 | +%foreign "support:idris2_getString, libidris2_support" |
| 69 | +prim__getString : Bits64 -> String |
| 70 | + |
| 71 | +||| Free C string |
| 72 | +export |
| 73 | +%foreign "C:universal_chat_extractor_free_string, libuniversal_chat_extractor" |
| 74 | +prim__freeString : Bits64 -> PrimIO () |
| 75 | + |
| 76 | +||| Get string result from library |
| 77 | +export |
| 78 | +%foreign "C:universal_chat_extractor_get_string, libuniversal_chat_extractor" |
| 79 | +prim__getResult : Bits64 -> PrimIO Bits64 |
| 80 | + |
| 81 | +||| Safe string getter |
| 82 | +export |
| 83 | +getString : Handle -> IO (Maybe String) |
| 84 | +getString h = do |
| 85 | + ptr <- primIO (prim__getResult (handlePtr h)) |
| 86 | + if ptr == 0 |
| 87 | + then pure Nothing |
| 88 | + else do |
| 89 | + let str = prim__getString ptr |
| 90 | + primIO (prim__freeString ptr) |
| 91 | + pure (Just str) |
| 92 | + |
| 93 | +-------------------------------------------------------------------------------- |
| 94 | +-- Array/Buffer Operations |
| 95 | +-------------------------------------------------------------------------------- |
| 96 | + |
| 97 | +||| Process array data |
| 98 | +export |
| 99 | +%foreign "C:universal_chat_extractor_process_array, libuniversal_chat_extractor" |
| 100 | +prim__processArray : Bits64 -> Bits64 -> Bits32 -> PrimIO Bits32 |
| 101 | + |
| 102 | +||| Safe array processor |
| 103 | +export |
| 104 | +processArray : Handle -> (buffer : Bits64) -> (len : Bits32) -> IO (Either Result ()) |
| 105 | +processArray h buf len = do |
| 106 | + result <- primIO (prim__processArray (handlePtr h) buf len) |
| 107 | + pure $ case resultFromInt result of |
| 108 | + Just Ok => Right () |
| 109 | + Just err => Left err |
| 110 | + Nothing => Left Error |
| 111 | + where |
| 112 | + resultFromInt : Bits32 -> Maybe Result |
| 113 | + resultFromInt 0 = Just Ok |
| 114 | + resultFromInt 1 = Just Error |
| 115 | + resultFromInt 2 = Just InvalidParam |
| 116 | + resultFromInt 3 = Just OutOfMemory |
| 117 | + resultFromInt 4 = Just NullPointer |
| 118 | + resultFromInt _ = Nothing |
| 119 | + |
| 120 | +-------------------------------------------------------------------------------- |
| 121 | +-- Error Handling |
| 122 | +-------------------------------------------------------------------------------- |
| 123 | + |
| 124 | +||| Get last error message |
| 125 | +export |
| 126 | +%foreign "C:universal_chat_extractor_last_error, libuniversal_chat_extractor" |
| 127 | +prim__lastError : PrimIO Bits64 |
| 128 | + |
| 129 | +||| Retrieve last error as string |
| 130 | +export |
| 131 | +lastError : IO (Maybe String) |
| 132 | +lastError = do |
| 133 | + ptr <- primIO prim__lastError |
| 134 | + if ptr == 0 |
| 135 | + then pure Nothing |
| 136 | + else pure (Just (prim__getString ptr)) |
| 137 | + |
| 138 | +||| Get error description for result code |
| 139 | +export |
| 140 | +errorDescription : Result -> String |
| 141 | +errorDescription Ok = "Success" |
| 142 | +errorDescription Error = "Generic error" |
| 143 | +errorDescription InvalidParam = "Invalid parameter" |
| 144 | +errorDescription OutOfMemory = "Out of memory" |
| 145 | +errorDescription NullPointer = "Null pointer" |
| 146 | + |
| 147 | +-------------------------------------------------------------------------------- |
| 148 | +-- Version Information |
| 149 | +-------------------------------------------------------------------------------- |
| 150 | + |
| 151 | +||| Get library version |
| 152 | +export |
| 153 | +%foreign "C:universal_chat_extractor_version, libuniversal_chat_extractor" |
| 154 | +prim__version : PrimIO Bits64 |
| 155 | + |
| 156 | +||| Get version as string |
| 157 | +export |
| 158 | +version : IO String |
| 159 | +version = do |
| 160 | + ptr <- primIO prim__version |
| 161 | + pure (prim__getString ptr) |
| 162 | + |
| 163 | +||| Get library build info |
| 164 | +export |
| 165 | +%foreign "C:universal_chat_extractor_build_info, libuniversal_chat_extractor" |
| 166 | +prim__buildInfo : PrimIO Bits64 |
| 167 | + |
| 168 | +||| Get build information |
| 169 | +export |
| 170 | +buildInfo : IO String |
| 171 | +buildInfo = do |
| 172 | + ptr <- primIO prim__buildInfo |
| 173 | + pure (prim__getString ptr) |
| 174 | + |
| 175 | +-------------------------------------------------------------------------------- |
| 176 | +-- Callback Support |
| 177 | +-------------------------------------------------------------------------------- |
| 178 | + |
| 179 | +||| Callback function type (C ABI) |
| 180 | +public export |
| 181 | +Callback : Type |
| 182 | +Callback = Bits64 -> Bits32 -> Bits32 |
| 183 | + |
| 184 | +||| Register a callback |
| 185 | +export |
| 186 | +%foreign "C:universal_chat_extractor_register_callback, libuniversal_chat_extractor" |
| 187 | +prim__registerCallback : Bits64 -> AnyPtr -> PrimIO Bits32 |
| 188 | + |
| 189 | +||| Safe callback registration |
| 190 | +export |
| 191 | +registerCallback : Handle -> Callback -> IO (Either Result ()) |
| 192 | +registerCallback h cb = do |
| 193 | + result <- primIO (prim__registerCallback (handlePtr h) (cast cb)) |
| 194 | + pure $ case resultFromInt result of |
| 195 | + Just Ok => Right () |
| 196 | + Just err => Left err |
| 197 | + Nothing => Left Error |
| 198 | + where |
| 199 | + resultFromInt : Bits32 -> Maybe Result |
| 200 | + resultFromInt 0 = Just Ok |
| 201 | + resultFromInt _ = Just Error |
| 202 | + |
| 203 | +-------------------------------------------------------------------------------- |
| 204 | +-- Utility Functions |
| 205 | +-------------------------------------------------------------------------------- |
| 206 | + |
| 207 | +||| Check if library is initialized |
| 208 | +export |
| 209 | +%foreign "C:universal_chat_extractor_is_initialized, libuniversal_chat_extractor" |
| 210 | +prim__isInitialized : Bits64 -> PrimIO Bits32 |
| 211 | + |
| 212 | +||| Check initialization status |
| 213 | +export |
| 214 | +isInitialized : Handle -> IO Bool |
| 215 | +isInitialized h = do |
| 216 | + result <- primIO (prim__isInitialized (handlePtr h)) |
| 217 | + pure (result /= 0) |
0 commit comments