Sovite.Core.Postfix.TomlWriter (sovite v0.6.0)

Copy Markdown View Source

Writes the TOML of a generated config file, for Sovite.Core.Postfix.Migration. Only what a Sovite config needs: strings, integers, booleans, arrays of those, inline tables, tables, and arrays of tables, with comments.

Summary

Types

A key and its value, optionally with a comment written above it.

A part of the document. Top-level :entries must come before the tables.

A value: string, integer, boolean, an array, or {:inline, pairs}.

Functions

A key: bare when it only has letters, digits, _, and -, otherwise quoted.

Renders parts. Tables without entries are left out; each array table is written, even when empty. A table's comment goes after its header.

A basic string, with \, ", and control characters escaped. Invalid UTF-8 is replaced.

A value.

Wraps text into lines of at most width characters, at spaces.

Types

entry()

@type entry() :: {String.t(), value()} | {String.t(), value(), String.t() | nil}

A key and its value, optionally with a comment written above it.

part()

@type part() ::
  {:comment, String.t()}
  | {:entries, [entry()]}
  | {:table, String.t(), [entry()]}
  | {:table, String.t(), [entry()], String.t() | nil}
  | {:array_table, String.t(), [entry()]}
  | {:array_table, String.t(), [entry()], String.t() | nil}

A part of the document. Top-level :entries must come before the tables.

value()

@type value() ::
  String.t()
  | integer()
  | boolean()
  | [String.t() | integer()]
  | {:inline, [{String.t(), value()}]}

A value: string, integer, boolean, an array, or {:inline, pairs}.

Functions

key(key)

@spec key(String.t()) :: String.t()

A key: bare when it only has letters, digits, _, and -, otherwise quoted.

iex> Sovite.Core.Postfix.TomlWriter.key("alice@example.com")
~s("alice@example.com")

render(parts)

@spec render([part()]) :: String.t()

Renders parts. Tables without entries are left out; each array table is written, even when empty. A table's comment goes after its header.

string(text)

@spec string(String.t()) :: String.t()

A basic string, with \, ", and control characters escaped. Invalid UTF-8 is replaced.

iex> Sovite.Core.Postfix.TomlWriter.string(~s(a "b" \\ c\td))
~S("a \"b\" \\ c\td")

value(value)

@spec value(value()) :: String.t()

A value.

wrap(text, width)

@spec wrap(String.t(), pos_integer()) :: [String.t()]

Wraps text into lines of at most width characters, at spaces.