Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

readstat-sys

Raw FFI bindings to the ReadStat C library.

The build.rs script compiles ~49 C source files from the vendored vendor/ReadStat/ git submodule via the cc crate. Platform-specific linking for iconv and zlib is handled automatically (see docs/BUILDING.md for details).

These bindings expose the full ReadStat API — all 125 functions and all 8 enum types — including support for SAS (.sas7bdat, .xpt), SPSS (.sav, .zsav, .por), and Stata (.dta) file formats. If you need to work with SPSS or Stata files from Rust, this crate provides the complete FFI surface to do so.

This is a sys crate — it exposes raw C types and functions. The higher-level readstat library crate provides a safe API but currently only implements support for SAS .sas7bdat files.

Vendored ReadStat version

This crate vendors the ReadStat C sources directly into the published package, so consumers do not need the git submodule. The current pin is:

  • ReadStat v1.1.9-50-g3add3a5 (commit 3add3a5)

Because the crate ships the C as real files (not a submodule reference), the published version on crates.io is self-contained; the submodule pointer is not visible to downstream consumers, which is why the vendored revision is recorded here.

Bindings and libclang

Rust bindings are pre-generated per (os, arch) and checked in under src/bindings/bindings_<os>_<arch>.rs. The default build simply copies the file matching the current target, so building this crate requires no libclang on any of the supported targets (Linux x86_64/aarch64, macOS x86_64/aarch64, Windows x86_64).

Targets without a checked-in bindings file (e.g. wasm32-unknown-emscripten) must enable the buildtime_bindgen feature, which regenerates bindings from wrapper.h at build time and requires libclang. Maintainers also use this feature to refresh the checked-in files when the vendored C surface changes:

cargo build -p readstat-sys --features buildtime_bindgen

API Coverage

All 125 public C functions and all 8 enum types from readstat.h are bound. All 49 library source files are compiled.

Functions by Category

CategoryCountFormats
Metadata accessors15All
Value accessors14All
Variable accessors14All
Parser lifecycle3All
Parser callbacks7All
Parser I/O handlers6All
Parser config4All
File parsers (readers)10SAS (sas7bdat, sas7bcat, xport), SPSS (sav, por), Stata (dta), text schema (sas_commands, spss_commands, stata_dictionary, txt)
Schema parsing1All
Writer lifecycle3All
Writer label sets5All
Writer variable definition11All
Writer notes/strings3All
Writer metadata setters8All
Writer begin6SAS (sas7bdat, sas7bcat, xport), SPSS (sav, por), Stata (dta)
Writer validation2All
Writer row insertion12All
Error handling1All
Total125

Compiled Source Files

DirectoryFilesDescription
src/ (core)11Hash table, parser, value/variable handling, writer, I/O, error
src/sas/11SAS7BDAT, SAS7BCAT, XPORT read/write, IEEE float, RLE compression
src/spss/16SAV, POR, ZSAV read/write, compression, SPSS parsing
src/stata/4DTA read/write, timestamp parsing
src/txt/7SAS commands, SPSS commands, Stata dictionary, plain text, schema
Total49

Enum Types

C EnumRust Type AliasDescription
readstat_type_ereadstat_type_eData types (string, int8/16/32, float, double, string_ref)
readstat_type_class_ereadstat_type_class_eType classes (string, numeric)
readstat_measure_ereadstat_measure_eMeasurement levels (nominal, ordinal, scale)
readstat_alignment_ereadstat_alignment_eColumn alignment (left, center, right)
readstat_compress_ereadstat_compress_eCompression types (none, rows, binary)
readstat_endian_ereadstat_endian_eByte order (big, little)
readstat_error_ereadstat_error_eError codes (41 variants)
readstat_io_flags_ereadstat_io_flags_eI/O flags

Verifying Bindings

To confirm that the Rust bindings stay in sync with the vendored C header and source files, run the verification script:

# Bash (Linux, macOS, Windows Git Bash)
bash crates/readstat-sys/verify_bindings.sh

# Rebuild first, then verify
bash crates/readstat-sys/verify_bindings.sh --rebuild
# PowerShell (Windows)
.\crates\readstat-sys\verify_bindings.ps1

# Rebuild first, then verify
.\crates\readstat-sys\verify_bindings.ps1 -Rebuild

The script checks three things:

  1. Every function declared in readstat.h has a pub fn binding in the generated bindings.rs
  2. Every typedef enum in the header has a corresponding Rust type alias
  3. Every .c library source file in the vendor directory is listed in build.rs

Run this after updating the ReadStat submodule to catch any new or removed API surface.