Current section
Files
Jump to
Current section
Files
src/bliss@dict_path_parser.erl
-module(bliss@dict_path_parser).
-compile(no_auto_import).
-export([parse/2]).
-export_type([error/0, response/0]).
-type error() :: not_enough_segments | {expected, binary()}.
-type response() :: {exact_match, gleam@map:map_(binary(), binary())} |
{partial_match, gleam@map:map_(binary(), binary()), list(binary())}.
-spec do_parse(
gleam@map:map_(binary(), binary()),
list(binary()),
list(binary())
) -> {ok, response()} | {error, error()}.
do_parse(Acc, Pattern_segments, Path_segments) ->
case Pattern_segments of
[] ->
case gleam@list:is_empty(Path_segments) of
true ->
{ok, {exact_match, Acc}};
false ->
{ok, {partial_match, Acc, Path_segments}}
end;
[First_pattern | Rest_pattern] ->
case Path_segments of
[] ->
{error, not_enough_segments};
[First_segment | Rest_path] ->
case gleam@string:starts_with(First_pattern, <<":"/utf8>>) of
true ->
Key = gleam@string:drop_left(First_pattern, 1),
Next_acc = begin
_pipe = Acc,
gleam@map:insert(_pipe, Key, First_segment)
end,
do_parse(Next_acc, Rest_pattern, Rest_path);
false ->
case First_pattern =:= First_segment of
true ->
do_parse(Acc, Rest_pattern, Rest_path);
false ->
{error, {expected, First_pattern}}
end
end
end
end.
-spec parse(binary(), binary()) -> {ok, response()} | {error, error()}.
parse(Pattern, Path) ->
Pattern_segments = bliss@utils:segments(Pattern),
Path_segments = bliss@utils:segments(Path),
do_parse(gleam@map:new(), Pattern_segments, Path_segments).