plushie/ffi

Erlang FFI wrappers for port operations, error handling, and unique ID generation.

Types

Line data from {line, N} port mode: complete line or partial chunk.

pub type LineData {
  Eol(data: BitArray)
  Noeol(data: BitArray)
}

Constructors

  • Eol(data: BitArray)
  • Noeol(data: BitArray)

Values

pub fn arch_string() -> String

Return the CPU architecture as a string (x86_64, aarch64, or raw).

pub fn crc32(data: BitArray) -> Int

CRC32 of binary data.

pub fn extract_eof(
  msg: dynamic.Dynamic,
) -> Result(Nil, dynamic.Dynamic)

Extract eof signal from a port message {Port, eof}.

pub fn extract_exit_status(
  msg: dynamic.Dynamic,
) -> Result(dynamic.Dynamic, dynamic.Dynamic)

Extract exit status from an Erlang port message tuple.

pub fn extract_line_data(
  msg: dynamic.Dynamic,
) -> Result(LineData, dynamic.Dynamic)

Extract line data from a port message in {line, N} mode. Returns Eol for complete lines, Noeol for partial chunks.

pub fn extract_port_data(
  msg: dynamic.Dynamic,
) -> Result(dynamic.Dynamic, dynamic.Dynamic)

Extract data payload from an Erlang port message tuple. Works for {packet, N} mode where data is a plain binary.

pub fn file_exists(path: String) -> Bool

Check whether a file exists at the given path.

pub fn file_watcher_subscribe(pid: dynamic.Dynamic) -> Nil

Subscribe the calling process to file events from the watcher.

pub fn get_env(name: String) -> Result(String, Nil)

Get an environment variable.

pub fn gleam_build() -> String

Run gleam build and return the output.

pub fn json_port_options() -> dynamic.Dynamic

Port options for JSONL wire format (newline-delimited).

pub fn list_beam_files(
  dir: String,
) -> List(#(dynamic.Dynamic, dynamic.Dynamic))

List .beam files in a directory, returning (module_atom, mtime) tuples.

pub fn monotonic_time_ms() -> Int

Return the current monotonic time in milliseconds.

pub fn msgpack_port_options() -> dynamic.Dynamic

Port options for MessagePack wire format (4-byte length prefix).

pub fn null_port() -> port.Port

Return a null port value for iostream transport (never used for I/O).

pub fn open_fd_port(
  input_fd: Int,
  output_fd: Int,
  options: dynamic.Dynamic,
) -> port.Port

Open an fd port (for stdin/stdout stdio transport).

pub fn open_port_spawn(
  path: String,
  args: List(String),
  env: dynamic.Dynamic,
  options: dynamic.Dynamic,
) -> port.Port

Open a port to spawn an external process. Uses {spawn_executable, Path} with explicit args and environment.

pub fn platform_string() -> String

Return the platform as a string (linux, darwin, windows, unknown).

pub fn port_close(port: port.Port) -> Bool

Close a port.

pub fn port_command(port: port.Port, data: BitArray) -> Bool

Send data to a port. Returns True on success.

pub fn reload_modules(modules: List(dynamic.Dynamic)) -> Nil

Reload a list of module atoms (purge + load_file).

pub fn set_env(name: String, value: String) -> Nil

Set an environment variable.

pub fn sha256_hex(data: BitArray) -> String

Compute SHA-256 hash and return as lowercase hex string.

pub fn stable_hash_key(value: dynamic.Dynamic) -> String

Return a stable hash key for any value as a string. Uses erlang:phash2 for consistent results regardless of how the value is wrapped (raw term vs Dynamic).

pub fn start_file_watcher(dirs: List(String)) -> dynamic.Dynamic

Start a file_system watcher on the given directories.

pub fn stdio_port_options_json() -> dynamic.Dynamic

Port options for stdio transport with JSON (eof, no exit_status).

pub fn stdio_port_options_msgpack() -> dynamic.Dynamic

Port options for stdio transport with MessagePack (eof, no exit_status).

pub fn try_call(f: fn() -> a) -> Result(a, dynamic.Dynamic)

Call a function with try/catch error handling. Catches panics and exceptions, returning Result.

pub fn unique_id() -> String

Generate a unique monotonic ID string.

pub fn unset_env(name: String) -> Nil

Unset an environment variable.

pub fn zlib_compress(data: BitArray) -> BitArray

Zlib compress binary data.

Search Document