Packages
toon_codec
1.0.0
TOON (Token-Oriented Object Notation) encoder/decoder - a compact, token-efficient format for LLM input
Current section
Files
Jump to
Current section
Files
src/toon_codec@error.erl
-module(toon_codec@error).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/toon_codec/error.gleam").
-export([error_to_string/1, parse_error/3, validation_error/1, structure_error/1, invalid_escape/2, unterminated_string/1, count_mismatch/3, indentation_error/2, missing_colon/1, invalid_header/2, delimiter_mismatch/2]).
-export_type([toon_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 types and utility functions for TOON encoding/decoding.\n"
"\n"
" This module defines all error types that can occur during TOON operations\n"
" and provides helper functions for error formatting and handling.\n"
" Comprehensive error type for TOON operations.\n"
"\n"
" Each variant provides context about what went wrong, including line numbers\n"
" and positions where applicable for better error reporting.\n"
" Convert a ToonError to a human-readable error message.\n"
"\n"
" ## Examples\n"
"\n"
" ```gleam\n"
" let error = MissingColon(line: 5)\n"
" error_to_string(error)\n"
" // -> \"Missing colon after key at line 5\"\n"
" ```\n"
" Create a parse error with context.\n"
" Create a validation error.\n"
" Create a structure error.\n"
" Create an invalid escape error.\n"
" Create an unterminated string error.\n"
" Create a count mismatch error.\n"
" Create an indentation error.\n"
" Create a missing colon error.\n"
" Create an invalid header error.\n"
" Create a delimiter mismatch error.\n"
).
-type toon_error() :: {parse_error, binary(), integer(), integer()} |
{validation_error, binary()} |
{structure_error, binary()} |
{invalid_escape, binary(), integer()} |
{unterminated_string, integer()} |
{count_mismatch, integer(), integer(), binary()} |
{indentation_error, binary(), integer()} |
{missing_colon, integer()} |
empty_input |
{invalid_header, binary(), integer()} |
{delimiter_mismatch, binary(), integer()}.
-file("src/toon_codec/error.gleam", 60).
-spec error_to_string(toon_error()) -> binary().
error_to_string(Error) ->
case Error of
{parse_error, Message, Line, Column} ->
<<<<<<<<<<"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>>;
{validation_error, Message@1} ->
<<"Validation error: "/utf8, Message@1/binary>>;
{structure_error, Message@2} ->
<<"Structure error: "/utf8, Message@2/binary>>;
{invalid_escape, Sequence, Position} ->
<<<<<<"Invalid escape sequence '\\"/utf8, Sequence/binary>>/binary,
"' at position "/utf8>>/binary,
(erlang:integer_to_binary(Position))/binary>>;
{unterminated_string, Position@1} ->
<<"Unterminated string: missing closing quote at position "/utf8,
(erlang:integer_to_binary(Position@1))/binary>>;
{count_mismatch, Expected, Actual, Context} ->
<<<<<<<<<<"Count mismatch in "/utf8, Context/binary>>/binary,
": expected "/utf8>>/binary,
(erlang:integer_to_binary(Expected))/binary>>/binary,
" but got "/utf8>>/binary,
(erlang:integer_to_binary(Actual))/binary>>;
{indentation_error, Message@3, Line@1} ->
<<<<<<"Indentation error at line "/utf8,
(erlang:integer_to_binary(Line@1))/binary>>/binary,
": "/utf8>>/binary,
Message@3/binary>>;
{missing_colon, Line@2} ->
<<"Missing colon after key at line "/utf8,
(erlang:integer_to_binary(Line@2))/binary>>;
empty_input ->
<<"Cannot decode empty input: input must be a non-empty string"/utf8>>;
{invalid_header, Message@4, Line@3} ->
<<<<<<"Invalid header at line "/utf8,
(erlang:integer_to_binary(Line@3))/binary>>/binary,
": "/utf8>>/binary,
Message@4/binary>>;
{delimiter_mismatch, Expected@1, Line@4} ->
<<<<<<<<"Delimiter mismatch at line "/utf8,
(erlang:integer_to_binary(Line@4))/binary>>/binary,
": expected '"/utf8>>/binary,
Expected@1/binary>>/binary,
"'"/utf8>>
end.
-file("src/toon_codec/error.gleam", 114).
-spec parse_error(binary(), integer(), integer()) -> toon_error().
parse_error(Message, Line, Column) ->
{parse_error, Message, Line, Column}.
-file("src/toon_codec/error.gleam", 118).
-spec validation_error(binary()) -> toon_error().
validation_error(Message) ->
{validation_error, Message}.
-file("src/toon_codec/error.gleam", 122).
-spec structure_error(binary()) -> toon_error().
structure_error(Message) ->
{structure_error, Message}.
-file("src/toon_codec/error.gleam", 126).
-spec invalid_escape(binary(), integer()) -> toon_error().
invalid_escape(Sequence, Position) ->
{invalid_escape, Sequence, Position}.
-file("src/toon_codec/error.gleam", 130).
-spec unterminated_string(integer()) -> toon_error().
unterminated_string(Position) ->
{unterminated_string, Position}.
-file("src/toon_codec/error.gleam", 134).
-spec count_mismatch(integer(), integer(), binary()) -> toon_error().
count_mismatch(Expected, Actual, Context) ->
{count_mismatch, Expected, Actual, Context}.
-file("src/toon_codec/error.gleam", 138).
-spec indentation_error(binary(), integer()) -> toon_error().
indentation_error(Message, Line) ->
{indentation_error, Message, Line}.
-file("src/toon_codec/error.gleam", 142).
-spec missing_colon(integer()) -> toon_error().
missing_colon(Line) ->
{missing_colon, Line}.
-file("src/toon_codec/error.gleam", 146).
-spec invalid_header(binary(), integer()) -> toon_error().
invalid_header(Message, Line) ->
{invalid_header, Message, Line}.
-file("src/toon_codec/error.gleam", 150).
-spec delimiter_mismatch(binary(), integer()) -> toon_error().
delimiter_mismatch(Expected, Line) ->
{delimiter_mismatch, Expected, Line}.