Dextrin.Format (Dextrin v0.3.3)

Copy Markdown View Source

The three forms' names on the wire and on disk, and a cheap test of whether a body is worth decoding as one -- DXN.md §6, so every implementation answers the same.

A form is :text (.dxn), :binary (.dxnb) or :schema (.dxns).

identifies?/2 is a prefix test, not a parse: true means "worth attempting", never "valid". Its text rule mainly tells DXN from JSON in a body that comes without a type, so it accepts only what JSON cannot start with: after whitespace, comments and an optional header, a % -- a map, a struct or a schema document. A list or a scalar is DXN too, but it overlaps with JSON, and stays unrecognised on purpose (Jan, 2026-10-20).

Summary

Types

One of DXN's three forms.

Functions

The file extension of form, with its dot.

The form media_type names: one of the three, or text for any type with a structured +dxn suffix (application/vnd.acme.event+dxn). There is no +dxnb suffix. Media types compare without regard to case; the caller strips any parameters (; charset=...) first.

Whether body is worth decoding as form.

The media type of form.

Types

form()

@type form() :: :text | :binary | :schema

One of DXN's three forms.

Functions

extension(atom)

@spec extension(form()) :: String.t()

The file extension of form, with its dot.

iex> Dextrin.Format.extension(:schema)
".dxns"

form(media_type)

@spec form(String.t()) :: {:ok, form()} | :error

The form media_type names: one of the three, or text for any type with a structured +dxn suffix (application/vnd.acme.event+dxn). There is no +dxnb suffix. Media types compare without regard to case; the caller strips any parameters (; charset=...) first.

iex> Dextrin.Format.form("application/vnd.acme.event+dxn")
{:ok, :text}

iex> Dextrin.Format.form("application/json")
:error

identifies?(body, form)

@spec identifies?(binary(), form()) :: boolean()

Whether body is worth decoding as form.

Binary: the envelope's magic, DX, and a version byte (DXN.md §2.1). Text and schemas: after whitespace (commas included), # comments and an optional @dxn "..." header, the first character is %.

iex> Dextrin.Format.identifies?("# config\n%{a: 1}", :text)
true

iex> Dextrin.Format.identifies?("[1 2]", :text)
false

media_type(form)

@spec media_type(form()) :: String.t()

The media type of form.

iex> Dextrin.Format.media_type(:binary)
"application/vnd.dxnb"