Current section
Files
Jump to
Current section
Files
src/handles.erl
-module(handles).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([prepare/1, run/2]).
-export_type([handles_error/0, template/0]).
-type handles_error() :: {lex_error, handles@lexer:lex_error()} |
{parse_error, list(handles@parser:parse_error())} |
{runtime_error, handles@engine:runtime_error()}.
-type template() :: {template, list(handles@parser:ast())}.
-spec prepare(binary()) -> {ok, template()} | {error, handles_error()}.
prepare(Template) ->
gleam@result:'try'(
gleam@result:map_error(
handles@lexer:run(Template),
fun(Field@0) -> {lex_error, Field@0} end
),
fun(Tokens) ->
gleam@result:'try'(
gleam@result:map_error(
handles@parser:run(
Tokens,
[<<"if"/utf8>>, <<"unless"/utf8>>, <<"each"/utf8>>]
),
fun(Err) -> {parse_error, Err} end
),
fun(Ast) -> {ok, {template, Ast}} end
)
end
).
-spec run(template(), gleam@dynamic:dynamic_()) -> {ok, binary()} |
{error, handles_error()}.
run(Template, Ctx) ->
{template, Ast} = Template,
_pipe = handles@engine:run(Ast, Ctx),
gleam@result:map_error(_pipe, fun(Field@0) -> {runtime_error, Field@0} end).