Current section
Files
Jump to
Current section
Files
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]).
-export_type([drill_error/0]).
-type drill_error() :: unknown_property | {unexpected_type, binary()}.
-spec drill_ctx(list(binary()), handles@ctx:value()) -> {ok,
handles@ctx:value()} |
{error, drill_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}
end;
{list, _} ->
{error, {unexpected_type, <<"List"/utf8>>}};
{str, _} ->
{error, {unexpected_type, <<"Str"/utf8>>}};
{int, _} ->
{error, {unexpected_type, <<"Int"/utf8>>}};
{float, _} ->
{error, {unexpected_type, <<"Float"/utf8>>}};
{bool, _} ->
{error, {unexpected_type, <<"Bool"/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} ->
{unexpected_type, Index, Path, Got, [<<"Dict"/utf8>>]};
unknown_property ->
{unknown_property, Index, Path}
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, _} ->
_pipe@6 = {unexpected_type,
Index,
Path,
<<"List"/utf8>>,
[<<"Str"/utf8>>, <<"Int"/utf8>>, <<"Float"/utf8>>]},
{error, _pipe@6};
{dict, _} ->
_pipe@7 = {unexpected_type,
Index,
Path,
<<"Dict"/utf8>>,
[<<"Str"/utf8>>, <<"Int"/utf8>>, <<"Float"/utf8>>]},
{error, _pipe@7};
{bool, _} ->
_pipe@8 = {unexpected_type,
Index,
Path,
<<"Bool"/utf8>>,
[<<"Str"/utf8>>, <<"Int"/utf8>>, <<"Float"/utf8>>]},
{error, _pipe@8}
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};
{str, _} ->
_pipe@2 = {unexpected_type,
Index,
Path,
<<"Str"/utf8>>,
[<<"List"/utf8>>]},
{error, _pipe@2};
{int, _} ->
_pipe@3 = {unexpected_type,
Index,
Path,
<<"Int"/utf8>>,
[<<"List"/utf8>>]},
{error, _pipe@3};
{bool, _} ->
_pipe@4 = {unexpected_type,
Index,
Path,
<<"Bool"/utf8>>,
[<<"List"/utf8>>]},
{error, _pipe@4};
{float, _} ->
_pipe@5 = {unexpected_type,
Index,
Path,
<<"Float"/utf8>>,
[<<"List"/utf8>>]},
{error, _pipe@5};
{dict, _} ->
_pipe@6 = {unexpected_type,
Index,
Path,
<<"Dict"/utf8>>,
[<<"List"/utf8>>]},
{error, _pipe@6}
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, _} ->
_pipe@2 = {unexpected_type,
Index,
Path,
<<"List"/utf8>>,
[<<"Bool"/utf8>>]},
{error, _pipe@2};
{str, _} ->
_pipe@3 = {unexpected_type,
Index,
Path,
<<"Str"/utf8>>,
[<<"Bool"/utf8>>]},
{error, _pipe@3};
{int, _} ->
_pipe@4 = {unexpected_type,
Index,
Path,
<<"Int"/utf8>>,
[<<"Bool"/utf8>>]},
{error, _pipe@4};
{float, _} ->
_pipe@5 = {unexpected_type,
Index,
Path,
<<"Float"/utf8>>,
[<<"Bool"/utf8>>]},
{error, _pipe@5};
{dict, _} ->
_pipe@6 = {unexpected_type,
Index,
Path,
<<"Dict"/utf8>>,
[<<"Bool"/utf8>>]},
{error, _pipe@6}
end end).