Packages

Handles is a templating language written in pure Gleam. Heavily inspired by Mustache and Handlebars.js

Current section

Files

Jump to
handles src handles@internal@ctx_utils.erl
Raw

src/handles@internal@ctx_utils.erl

-module(handles@internal@ctx_utils).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([get/3, get_property/3, get_list/3, get_bool/3]).
-spec drill_ctx(list(binary()), handles@ctx:value()) -> {ok,
handles@ctx:value()} |
{error, handles@error:runtime_error()}.
drill_ctx(Path, Ctx) ->
case Path of
[] ->
{ok, Ctx};
[Key | Rest] ->
case Ctx of
{dict, Arr} ->
case gleam@list:find(
Arr,
fun(It) -> erlang:element(2, It) =:= Key end
) of
{ok, {prop, _, Value}} ->
drill_ctx(Rest, Value);
{error, _} ->
{error, {unknown_property, 0, []}}
end;
{list, _} ->
{error,
{unexpected_type,
0,
[],
<<"List"/utf8>>,
[<<"Dict"/utf8>>]}};
{str, _} ->
{error,
{unexpected_type,
0,
[],
<<"Str"/utf8>>,
[<<"Dict"/utf8>>]}};
{int, _} ->
{error,
{unexpected_type,
0,
[],
<<"Int"/utf8>>,
[<<"Dict"/utf8>>]}};
{float, _} ->
{error,
{unexpected_type,
0,
[],
<<"Float"/utf8>>,
[<<"Dict"/utf8>>]}};
{bool, _} ->
{error,
{unexpected_type,
0,
[],
<<"Bool"/utf8>>,
[<<"Dict"/utf8>>]}}
end
end.
-spec get(list(binary()), handles@ctx:value(), integer()) -> {ok,
handles@ctx:value()} |
{error, handles@error:runtime_error()}.
get(Path, Ctx, Index) ->
_pipe = drill_ctx(Path, Ctx),
gleam@result:map_error(_pipe, fun(Err) -> case Err of
{unexpected_type, _, _, Got, Expected} ->
{unexpected_type, Index, Path, Got, Expected};
{unknown_property, _, _} ->
{unknown_property, Index, Path};
Err@1 ->
Err@1
end end).
-spec get_property(list(binary()), handles@ctx:value(), integer()) -> {ok,
binary()} |
{error, handles@error:runtime_error()}.
get_property(Path, Root_ctx, Index) ->
_pipe = get(Path, Root_ctx, Index),
gleam@result:'try'(_pipe, fun(It) -> case It of
{str, Value} ->
_pipe@1 = Value,
{ok, _pipe@1};
{int, Value@1} ->
_pipe@2 = Value@1,
_pipe@3 = gleam@int:to_string(_pipe@2),
{ok, _pipe@3};
{float, Value@2} ->
_pipe@4 = Value@2,
_pipe@5 = gleam@float:to_string(_pipe@4),
{ok, _pipe@5};
{list, _} ->
{error,
{unexpected_type,
Index,
Path,
<<"List"/utf8>>,
[<<"Str"/utf8>>, <<"Int"/utf8>>, <<"Float"/utf8>>]}};
{dict, _} ->
{error,
{unexpected_type,
Index,
Path,
<<"Dict"/utf8>>,
[<<"Str"/utf8>>, <<"Int"/utf8>>, <<"Float"/utf8>>]}};
{bool, _} ->
{error,
{unexpected_type,
Index,
Path,
<<"Bool"/utf8>>,
[<<"Str"/utf8>>, <<"Int"/utf8>>, <<"Float"/utf8>>]}}
end end).
-spec get_list(list(binary()), handles@ctx:value(), integer()) -> {ok,
list(handles@ctx:value())} |
{error, handles@error:runtime_error()}.
get_list(Path, Root_ctx, Index) ->
_pipe = get(Path, Root_ctx, Index),
gleam@result:'try'(_pipe, fun(It) -> case It of
{list, Value} ->
_pipe@1 = Value,
{ok, _pipe@1};
{bool, _} ->
{error,
{unexpected_type,
Index,
Path,
<<"Bool"/utf8>>,
[<<"List"/utf8>>]}};
{str, _} ->
{error,
{unexpected_type,
Index,
Path,
<<"Str"/utf8>>,
[<<"List"/utf8>>]}};
{int, _} ->
{error,
{unexpected_type,
Index,
Path,
<<"Int"/utf8>>,
[<<"List"/utf8>>]}};
{float, _} ->
{error,
{unexpected_type,
Index,
Path,
<<"Float"/utf8>>,
[<<"List"/utf8>>]}};
{dict, _} ->
{error,
{unexpected_type,
Index,
Path,
<<"Dict"/utf8>>,
[<<"List"/utf8>>]}}
end end).
-spec get_bool(list(binary()), handles@ctx:value(), integer()) -> {ok,
boolean()} |
{error, handles@error:runtime_error()}.
get_bool(Path, Root_ctx, Index) ->
_pipe = get(Path, Root_ctx, Index),
gleam@result:'try'(_pipe, fun(It) -> case It of
{bool, Value} ->
_pipe@1 = Value,
{ok, _pipe@1};
{list, _} ->
{error,
{unexpected_type,
Index,
Path,
<<"List"/utf8>>,
[<<"Bool"/utf8>>]}};
{str, _} ->
{error,
{unexpected_type,
Index,
Path,
<<"Str"/utf8>>,
[<<"Bool"/utf8>>]}};
{int, _} ->
{error,
{unexpected_type,
Index,
Path,
<<"Int"/utf8>>,
[<<"Bool"/utf8>>]}};
{float, _} ->
{error,
{unexpected_type,
Index,
Path,
<<"Float"/utf8>>,
[<<"Bool"/utf8>>]}};
{dict, _} ->
{error,
{unexpected_type,
Index,
Path,
<<"Dict"/utf8>>,
[<<"Bool"/utf8>>]}}
end end).