March Docs

Compress

Compress module: gzip, deflate, zstd, and brotli compression.

All one-shot encode/decode functions operate on Bytes values and return Result(Bytes, Compress.Error). Streaming variants take a Seq of Bytes chunks and return a lazy Seq of per-chunk Result(Bytes, Compress.Error).

Sub-modules are scoped by algorithm so callers can import selectively:

let compressed = Compress.Gzip.encode(data)
let restored   = Compress.Gzip.decode(compressed)

Level types let callers trade speed for ratio without magic numbers:

Compress.Gzip.encode_level(data, Gzip.BestCompression)
Compress.Zstd.encode_level(data, Zstd.Fast(3))

Implementation note: the underlying work is done by zlib (gzip/deflate), libzstd (zstd), and libbrotlienc/libbrotlidec (brotli) via C FFI, one shim per library (runtime/march_compress.c, march_compress_zstd.c, march_compress_brotli.c). A compiled program links only the codecs it calls; one that calls Zstd or Brotli on a build host without that library is a compile error naming the package to install. The codecs are not available in the in-browser REPL (only the pure HTTP helpers are), so codec examples below are plain code blocks.

Types

typeErrorError =#

Shared error type across all sub-modules. InvalidInput(msg) the input was not valid compressed data (decode), or was too large to hand to the codec (encode) InsufficientOutput decoding hit the decompressed-output size cap Io(msg) an environment failure: out of memory, a codec that failed to initialise, or a library not built in msg is the codec's own message, e.g. "gzip_decode: inflate failed".

typeLevelLevel =#

Compression level for gzip encoding.

typeLevelLevel =#

Compression level for zstd encoding.

typeModeMode =#

Encoding mode hint for brotli.

typeLevelLevel = Level(Int)#

Compression level for brotli (0–11).

Functions

fnaccept_encodingaccept_encoding(header_value : String) : List(String)#

List the content-encoding tokens from an Accept-Encoding header value. Strips quality parameters (q=) and excludes encodings with q=0, q=0.0 or q=0.00 (which the client explicitly rejects). Tokens keep header order and their original case; other q-values do not reorder them. Empty entries are skipped, so an empty header gives [].

Examples

march> Compress.accept_encoding("gzip, deflate, br")
["gzip", "deflate", "br"]
march> Compress.accept_encoding("gzip;q=0.9, br;q=0.5")
["gzip", "br"]
march> Compress.accept_encoding(" , gzip ;q=0 ,zstd")
["zstd"]
march> Compress.accept_encoding("")
[]
fnbest_encodingbest_encoding(tokens : List(String)) : Option(String)#

Choose the best compression encoding supported by the client, given the parsed Accept-Encoding tokens. Prefers zstd > br > gzip in that order, regardless of the order (or q-values) the client gave. Matching is exact and case-sensitive; deflate and * are never chosen. Returns None if no supported encoding was advertised.

Examples

march> Compress.best_encoding(["gzip", "br"])
Some("br")
march> Compress.best_encoding(Compress.accept_encoding("gzip, zstd;q=0.1"))
Some("zstd")
march> Compress.best_encoding(["identity", "deflate"])
None