crates/edge/cli/src/templates/library/engine.c.in 1.4 KiB 58 lines Raw
1 /*2  * The placeholder engine for a generated `engine = "library"` bundle.3  *4  * The Rust shim loads this shared library and calls the four exports below.5  * A real bundle replaces this file with a thin adapter over the C API it6  * hosts. The adapter maps the library's open, generate, and close calls onto7  * the four exports below.8  *9  * `inf3r_engine_open_fd` takes ownership of the descriptor. The placeholder10  * copies the prompt into the caller's buffer and returns its length.11  */12 #include <stdint.h>13 #include <stdlib.h>14 #include <string.h>15 #include <unistd.h>16 17 #define INF3R_ENGINE_ABI 1u18 19 struct inf3r_engine {20     int fd;21 };22 23 uint32_t inf3r_engine_abi(void) {24     return INF3R_ENGINE_ABI;25 }26 27 void *inf3r_engine_open_fd(int fd) {28     struct inf3r_engine *engine = malloc(sizeof(*engine));29     if (engine == NULL) {30         return NULL;31     }32     engine->fd = fd;33     return engine;34 }35 36 long inf3r_engine_generate(void *raw, const uint8_t *in, size_t in_len,37                            uint8_t *out, size_t out_cap) {38     struct inf3r_engine *engine = raw;39     if (engine == NULL || out == NULL) {40         return -1;41     }42     size_t take = in_len < out_cap ? in_len : out_cap;43     if (take != 0) {44         memcpy(out, in, take);45     }46     return (long)take;47 }48 49 void inf3r_engine_close(void *raw) {50     struct inf3r_engine *engine = raw;51     if (engine == NULL) {52         return;53     }54     if (engine->fd >= 0) {55         close(engine->fd);56     }57     free(engine);58 }