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 return Seq(Bytes).

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. Each algorithm shim lives in runtime/march_compress.c.

Types

typeErrorError =#
typeLevelLevel =#
typeLevelLevel =#
typeModeMode =#
typeLevelLevel = Level(Int)#

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 (which the client explicitly rejects).

    Compress.accept_encoding("gzip, deflate, br")
    -- ["gzip", "deflate", "br"]

    Compress.accept_encoding("gzip;q=0.9, br;q=0.5")
    -- ["gzip", "br"]

    Compress.accept_encoding("gzip;q=0, br")
    -- ["br"]

Returns an empty list if the header is absent or empty.
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. Returns None if no supported encoding was advertised.

    Compress.best_encoding(["gzip", "br"])
    -- Some("br")

    Compress.best_encoding(["identity"])
    -- None