C FFI API
The C API embeds gridwell in any language with C interop. Before 1.0 it may still change between releases. The header is crates/gridwell-ffi/include/gridwell.h. It is generated from the Rust source by cbindgen, and a test fails if the committed copy goes stale, so the header is the authoritative reference. The summary below mirrors it.
Types
typedef struct GridwellTable GridwellTable; // opaque
typedef struct GridwellError GridwellError; // opaque
typedef struct GridwellTextResult {
char *text; // NUL-terminated UTF-8; null on failure
size_t len; // bytes, excluding the terminator
} GridwellTextResult;
typedef struct GridwellBinaryResult {
uint8_t *data; // null on failure
size_t len;
} GridwellBinaryResult;Error codes
| Code | Constant | Meaning |
|---|---|---|
| 1 | GRIDWELL_ERR_PARSE |
Invalid UTF-8 or JSON, or JSON that isn’t a table IR |
| 2 | GRIDWELL_ERR_VALIDATE |
The IR failed validation; the message lists the errors |
| 3 | GRIDWELL_ERR_RENDER |
A writer failed, the format name is unknown, or a binary format was passed to gridwell_render_text (or a text one to gridwell_render_binary) |
| 4 | GRIDWELL_ERR_INVALID_ARG |
A required pointer was null, or a string wasn’t UTF-8 |
| 5 | GRIDWELL_ERR_PANIC |
An internal panic was caught at the boundary (always a gridwell bug; please report it) |
| 6 | GRIDWELL_ERR_OPTIONS |
The writer options are invalid: malformed JSON, not an object, an unknown option, a wrong type, or a value out of range |
No Rust panic ever unwinds into C: each function catches panics and reports them as GRIDWELL_ERR_PANIC.
Functions
// Parse
GridwellTable *gridwell_parse_ir(const char *json, size_t len, GridwellError **err);
// Validate: null on success, else an error listing every problem, one per line.
GridwellError *gridwell_validate(const GridwellTable *table);
// Render text (html, latex, typst, rtf, svg, ansi, pandoc, quarto).
// Both render functions validate first: invalid IR returns an empty result and an
// error with code GRIDWELL_ERR_VALIDATE whose message lists the validation errors.
GridwellTextResult gridwell_render_text(
const GridwellTable *table, const char *format, GridwellError **err);
// Render binary (docx, xlsx, pptx)
GridwellBinaryResult gridwell_render_binary(
const GridwellTable *table, const char *format, GridwellError **err);
// The same with writer options: a JSON object, or null for the defaults.
GridwellTextResult gridwell_render_text_with_options(
const GridwellTable *table, const char *format, const char *options,
GridwellError **err);
GridwellBinaryResult gridwell_render_binary_with_options(
const GridwellTable *table, const char *format, const char *options,
GridwellError **err);
// Warnings, one message per line ("\n"-separated, no trailing newline): first
// "[NEWER_IR_VERSION] …" for a table of a newer minor IR version, then what of the
// table this gridwell leaves out, one "[IGNORED_VALUE] …" or "[IGNORED_SELECTOR] …"
// each, then what the format can't express, one "[UNSUPPORTED_FEATURE] …" each; an
// empty string if none. A null format means every format. Validates first, as
// rendering does. Free the result with gridwell_free_text_result.
GridwellTextResult gridwell_warnings(
const GridwellTable *table, const char *format, GridwellError **err);
// Formats: a static comma-separated list of names (do not free), and a name's kind
// (GRIDWELL_FORMAT_TEXT, GRIDWELL_FORMAT_BINARY or GRIDWELL_FORMAT_UNKNOWN).
const char *gridwell_format_names(void);
int32_t gridwell_format_kind(const char *format);
// Error inspection
const char *gridwell_error_message(const GridwellError *err); // valid until freed
int32_t gridwell_error_code(const GridwellError *err); // 0 if err is null
// Memory management (all accept null / failed results)
void gridwell_free_table(GridwellTable *table);
void gridwell_free_error(GridwellError *err);
void gridwell_free_text_result(GridwellTextResult result);
void gridwell_free_binary_result(GridwellBinaryResult result);Every err out-parameter may be null if you don’t need the error. Format names are case-insensitive. See Writer Options for each format’s options. See Warnings for what each format warns about.
Example
A program that renders a table IR file to LaTeX:
example.c
#include "gridwell.h"
#include <stdio.h>
#include <stdlib.h>
/* The whole file, or NULL. */
static char *read_file(const char *path, size_t *len) {
FILE *f = fopen(path, "rb");
if (!f) return NULL;
char *buf = NULL;
size_t cap = 0, n = 0, got;
do {
if (n == cap) {
char *bigger = realloc(buf, cap = cap ? 2 * cap : 4096);
if (!bigger) { free(buf); fclose(f); return NULL; }
buf = bigger;
}
got = fread(buf + n, 1, cap - n, f);
n += got;
} while (got > 0);
fclose(f);
*len = n;
return buf;
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "usage: %s TABLE.json\n", argv[0]);
return 2;
}
size_t len;
char *json = read_file(argv[1], &len);
if (!json) {
perror(argv[1]);
return 1;
}
GridwellError *err = NULL;
GridwellTable *table = gridwell_parse_ir(json, len, &err);
free(json);
if (!table) {
fprintf(stderr, "parse error: %s\n", gridwell_error_message(err));
gridwell_free_error(err);
return 1;
}
GridwellTextResult result = gridwell_render_text(table, "latex", &err);
gridwell_free_table(table); /* the result doesn't borrow from the table */
if (!result.text) {
fprintf(stderr, "error %d: %s\n", (int)gridwell_error_code(err),
gridwell_error_message(err));
gridwell_free_error(err);
return 1;
}
fwrite(result.text, 1, result.len, stdout);
gridwell_free_text_result(result);
return 0;
}Built and run on the getting-started table, and on an invalid one:
Example output
$ ./example hello.json
\begin{tabular}{lr}
\toprule
Name & Score \\
\midrule
Alice & 95 \\
Bob & 87 \\
\bottomrule
\end{tabular}
$ ./example broken_table.json
error 2: table IR failed validation with 2 errors
- [COL_COUNT] tbody group 0 row 0 has 4 cells but config.table_cols is 3
- [SPAN_OVERFLOW_RIGHT] Cell at tbody group 1 (row=0, col=2) has colspan=2 but table only has 3 columns (would need col index up to 3)Linking
Build the library with cargo build --release -p gridwell-ffi. That produces libgridwell_ffi.{so,dylib} and libgridwell_ffi.a in target/release/. Then:
cc -o example example.c -I crates/gridwell-ffi/include -L target/release -lgridwell_fficrates/gridwell-ffi/tests/c/smoke.c is a complete working example. The test suite compiles it as C99, as C++, and under AddressSanitizer.