Dextrin.Text.Formatter (Dextrin v0.1.0)

Copy Markdown View Source

Multi-line, indented .dxn rendering — Dextrin.encode/2's pretty: true opt, mix dextrin.format's --mode pretty, and mix dextrin.decode's default output (a CLI decode is a human reading the result, so encode/2's own single-line default isn't the right default there). --mode condense needs no separate implementation at all: it's exactly what Dextrin.encode/2/Dextrin.Text.Printer already produce with pretty: false (the default).

Comments cannot be preserved here, or by any formatter built on this library's decode-then-reprint approach: Dextrin.Text.Grammar's lexer folds #-comments into the same auto-spliced trivia as whitespace and discards them before the parser (and therefore any value) ever exists. There is no comment text left to put back by the time a value reaches this module. A genuine comment-preserving formatter would need a second, independent re-lexing pipeline that never goes through the value-producing parse at all — real, separate work, not a flag on this one.

Summary

Functions

Renders value as multi-line, indented .dxn text. Returns {:ok, _} | {:error, _}, matching Dextrin.Text.Printer.print/2 — the only way this can fail is the same as print/2's: a struct with no tag_encoder/schema module registered (or one that itself returns {:error, _}).

Functions

pretty(value, opts \\ [])

@spec pretty(
  term(),
  keyword()
) :: {:ok, String.t()} | {:error, Dextrin.Error.t()}

Renders value as multi-line, indented .dxn text. Returns {:ok, _} | {:error, _}, matching Dextrin.Text.Printer.print/2 — the only way this can fail is the same as print/2's: a struct with no tag_encoder/schema module registered (or one that itself returns {:error, _}).

opts[:indent] sets the number of spaces per nesting level (default 2); every other opt (registry:, ...) is the same as print/2's.