Packages

A pure-Gleam YAML parser.

Current section

Files

Jump to
yamleam src yamleam@error.erl
Raw

src/yamleam@error.erl

-module(yamleam@error).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/yamleam/error.gleam").
-export([to_string/1]).
-export_type([yaml_error/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Error type for yamleam.\n"
"\n"
" `YamlError` distinguishes three failure modes:\n"
" - `ParseError` — the source is not valid YAML (or not valid in the\n"
" subset we support); includes a free-form message and the 1-indexed\n"
" line/column of the offending character\n"
" - `Unsupported` — the source uses a YAML feature yamleam does not yet\n"
" implement; the `feature` field describes what was encountered and\n"
" the version it's targeted for\n"
" - `DecodeError` — the source parsed correctly but a decoder rejected\n"
" the resulting shape\n"
).
-type yaml_error() :: {parse_error, binary(), integer(), integer()} |
{unsupported, binary(), integer(), integer()} |
{decode_error, list(gleam@dynamic@decode:decode_error())}.
-file("src/yamleam/error.gleam", 25).
?DOC(
" Format an error as a single human-readable line for logging or\n"
" display. Intentionally simple — consumers wanting structured output\n"
" should pattern-match on the variants themselves.\n"
).
-spec to_string(yaml_error()) -> binary().
to_string(Err) ->
case Err of
{parse_error, Message, Line, Column} ->
<<<<<<<<<<"yaml parse error at line "/utf8,
(erlang:integer_to_binary(Line))/binary>>/binary,
", column "/utf8>>/binary,
(erlang:integer_to_binary(Column))/binary>>/binary,
": "/utf8>>/binary,
Message/binary>>;
{unsupported, Feature, Line@1, Column@1} ->
<<<<<<<<<<"yaml feature not supported at line "/utf8,
(erlang:integer_to_binary(Line@1))/binary>>/binary,
", column "/utf8>>/binary,
(erlang:integer_to_binary(Column@1))/binary>>/binary,
": "/utf8>>/binary,
Feature/binary>>;
{decode_error, _} ->
<<"yaml decode error: could not extract requested shape"/utf8>>
end.