-module(handles@format). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([format_tokenizer_error/2, format_runtime_error/2]). -export_type([position/0]). -type position() :: {position, integer(), integer(), integer()} | out_of_bounds. -spec resolve_position(binary(), integer(), position()) -> position(). resolve_position(Input, Target_index, Current) -> case Current of {position, Index, Row, Col} when Index =:= Target_index -> {position, Target_index, Row, Col}; {position, Index@1, Row@1, Col@1} -> case gleam@string:first(Input) of {ok, Char} -> case Char of <<"\n"/utf8>> -> resolve_position( gleam@string:drop_left(Input, 1), Target_index, {position, Index@1 + 1, Row@1 + 1, 0} ); _ -> resolve_position( gleam@string:drop_left(Input, 1), Target_index, {position, Index@1 + 1, Row@1, Col@1 + 1} ) end; {error, _} -> out_of_bounds end; out_of_bounds -> out_of_bounds end. -spec transform_error(binary(), integer(), binary()) -> {ok, binary()} | {error, nil}. transform_error(Template, Offset, Message) -> case resolve_position(Template, Offset, {position, 0, 0, 0}) of {position, _, Row, Col} -> {ok, <<<<<<<<<>}; out_of_bounds -> {error, nil} end. -spec format_tokenizer_error(handles@error:tokenizer_error(), binary()) -> {ok, binary()} | {error, nil}. format_tokenizer_error(Error, Template) -> case Error of {unbalanced_tag, Index} -> transform_error( Template, Index, <<"Tag is missing closing braces }}"/utf8>> ); {missing_argument, Index@1} -> transform_error( Template, Index@1, <<"Tag is missing an argument"/utf8>> ); {missing_block_kind, Index@2} -> transform_error( Template, Index@2, <<"Tag is missing a block kind"/utf8>> ); {missing_partial_id, Index@3} -> transform_error( Template, Index@3, <<"Tag is missing a partial id"/utf8>> ); {unexpected_block_kind, Index@4} -> transform_error( Template, Index@4, <<"Tag is of an unknown block kind"/utf8>> ); {unexpected_multiple_arguments, Index@5} -> transform_error( Template, Index@5, <<"Tag is receiving too many arguments"/utf8>> ); {unexpected_argument, Index@6} -> transform_error( Template, Index@6, <<"Tag is receiving too many arguments"/utf8>> ); {unbalanced_block, Index@7} -> transform_error( Template, Index@7, <<"Tag is a block but is missing its corresponding end tag"/utf8>> ); {unexpected_block_end, Index@8} -> transform_error( Template, Index@8, <<"Tag is a block end but is missing its corresponsing opening tag"/utf8>> ) end. -spec format_runtime_error(handles@error:runtime_error(), binary()) -> {ok, binary()} | {error, nil}. format_runtime_error(Error, Template) -> case Error of {unexpected_type, Index, Path, Got, Expected} -> transform_error( Template, Index, <<<<<<<<<<"Unexpected type of property "/utf8, (gleam@string:join(Path, <<"."/utf8>>))/binary>>/binary, ", extepced "/utf8>>/binary, (gleam@string:join(Expected, <<" or "/utf8>>))/binary>>/binary, " but found found "/utf8>>/binary, Got/binary>> ); {unknown_property, Index@1, Path@1} -> transform_error( Template, Index@1, <<"Unable to resolve property "/utf8, (gleam@string:join(Path@1, <<"."/utf8>>))/binary>> ); {unknown_partial, Index@2, Id} -> transform_error( Template, Index@2, <<"Unknown partial "/utf8, Id/binary>> ) end.