-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>> ); {unexpected_block_argument, Index@1} -> transform_error( Template, Index@1, <<"Tag is a closing block, which does not take any arguments"/utf8>> ); {missing_block_argument, Index@2} -> transform_error( Template, Index@2, <<"Tag is missing block argument"/utf8>> ); {missing_property_path, Index@3} -> transform_error( Template, Index@3, <<"Tag is missing property path"/utf8>> ); {unexpected_block_kind, Index@4} -> transform_error( Template, Index@4, <<"Tag is of unknown block kind"/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_error, 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_error, Index@1, Path@1} -> transform_error( Template, Index@1, <<"Unable to resolve property "/utf8, (gleam@string:join(Path@1, <<"."/utf8>>))/binary>> ) end.