Current section
Files
Jump to
Current section
Files
src/scrapbook@internal@html.erl
-module(scrapbook@internal@html).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([get_property/4, no_value_to_option/1, no_prefix_to_option/1]).
-export_type([index_search/0, find_key_value_result/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(false).
-type index_search() :: {index_search,
integer(),
binary(),
integer(),
boolean()}.
-type find_key_value_result() :: {find_key_value_result, binary(), binary()}.
-file("src/scrapbook/internal/html.gleam", 23).
?DOC(false).
-spec do_find_value_end_index(
list(binary()),
binary(),
binary(),
index_search()
) -> {ok, integer()} | {error, scrapbook@error:index_search_error()}.
do_find_value_end_index(Graphemes, Start, End, Index_search) ->
case Graphemes of
[] ->
{error, {no_index_found, erlang:element(2, Index_search)}};
[First | Rest] when (erlang:element(3, Index_search) =/= <<"\\"/utf8>>) andalso (First =:= <<"\""/utf8>>) ->
do_find_value_end_index(
Rest,
Start,
End,
{index_search,
erlang:element(2, Index_search) + 1,
First,
erlang:element(4, Index_search),
not erlang:element(5, Index_search)}
);
[First@1 | Rest@1] when (First@1 =:= End) andalso (erlang:element(
5,
Index_search
) =:= false) ->
case erlang:element(4, Index_search) =:= 1 of
true ->
{ok, erlang:element(2, Index_search)};
false ->
do_find_value_end_index(
Rest@1,
Start,
End,
{index_search,
erlang:element(2, Index_search) + 1,
First@1,
erlang:element(4, Index_search) - 1,
erlang:element(5, Index_search)}
)
end;
[First@2 | Rest@2] when (First@2 =:= Start) andalso (erlang:element(
5,
Index_search
) =:= false) ->
do_find_value_end_index(
Rest@2,
Start,
End,
{index_search,
erlang:element(2, Index_search) + 1,
First@2,
erlang:element(4, Index_search) + 1,
erlang:element(5, Index_search)}
);
[First@3 | Rest@3] ->
do_find_value_end_index(
Rest@3,
Start,
End,
{index_search,
erlang:element(2, Index_search) + 1,
First@3,
erlang:element(4, Index_search),
erlang:element(5, Index_search)}
)
end.
-file("src/scrapbook/internal/html.gleam", 86).
?DOC(false).
-spec find_value_end_index(list(binary()), binary(), binary()) -> {ok,
integer()} |
{error, scrapbook@error:index_search_error()}.
find_value_end_index(Graphemes, Start, End) ->
do_find_value_end_index(
Graphemes,
Start,
End,
{index_search, 0, <<""/utf8>>, 0, false}
).
-file("src/scrapbook/internal/html.gleam", 104).
?DOC(false).
-spec find_value(binary()) -> {ok, binary()} |
{error, scrapbook@error:find_value_error()}.
find_value(Html) ->
gleam@bool:guard(
begin
_pipe = Html,
gleam_stdlib:string_starts_with(_pipe, <<"null"/utf8>>)
end,
{error, no_value},
fun() ->
Graphemes = begin
_pipe@1 = Html,
gleam@string:to_graphemes(_pipe@1)
end,
gleam@result:'try'(
gleam@result:replace_error(
begin
_pipe@2 = Graphemes,
gleam@list:first(_pipe@2)
end,
no_graphemes
),
fun(Start_character) ->
gleam@bool:guard(
(Start_character /= <<"{"/utf8>>) andalso (Start_character
/= <<"["/utf8>>),
{error, invalid_start_character},
fun() ->
End_character = case Start_character of
<<"{"/utf8>> ->
<<"}"/utf8>>;
_ ->
<<"]"/utf8>>
end,
gleam@result:'try'(
case find_value_end_index(
Graphemes,
Start_character,
End_character
) of
{ok, Value} ->
{ok, Value};
{error, Err} ->
{error, {no_value_end_found, Err}}
end,
fun(End_index) ->
{ok,
begin
_pipe@3 = Html,
gleam@string:slice(
_pipe@3,
0,
End_index + 1
)
end}
end
)
end
)
end
)
end
).
-file("src/scrapbook/internal/html.gleam", 146).
?DOC(false).
-spec find_key_value(binary(), binary()) -> {ok, find_key_value_result()} |
{error, scrapbook@error:find_key_value_error()}.
find_key_value(Html, Key) ->
Prefix = <<<<"\""/utf8, Key/binary>>/binary, "\":"/utf8>>,
gleam@result:'try'(
gleam@result:replace_error(
begin
_pipe = Html,
gleam@string:split_once(_pipe, Prefix)
end,
{no_prefix, Key}
),
fun(_use0) ->
{_, Html@1} = _use0,
case find_value(Html@1) of
{ok, Value} ->
{ok, {find_key_value_result, Html@1, Value}};
{error, Err} ->
{error, {find_value_error, Err}}
end
end
).
-file("src/scrapbook/internal/html.gleam", 164).
?DOC(false).
-spec do_get_property(
binary(),
binary(),
gleam@dynamic@decode:decoder(NLI),
fun((NLI) -> boolean()),
list(scrapbook@error:property_error())
) -> {ok, NLI} | {error, list(scrapbook@error:property_error())}.
do_get_property(Html, Key, Decoder, Validator, Errors) ->
gleam@result:'try'(case find_key_value(Html, Key) of
{ok, Value} ->
{ok, Value};
{error, Err} ->
case Errors of
[] ->
{error, [{find_key_value_error, Err}]};
_ ->
{error, Errors}
end
end, fun(_use0) ->
{find_key_value_result, Html@1, Json_string} = _use0,
case gleam@json:parse(Json_string, Decoder) of
{ok, Value@1} ->
case Validator(Value@1) of
true ->
{ok, Value@1};
false ->
do_get_property(
Html@1,
Key,
Decoder,
Validator,
[validate_error | Errors]
)
end;
{error, Err@1} ->
do_get_property(
Html@1,
Key,
Decoder,
Validator,
[{decode_error, Err@1} | Errors]
)
end
end).
-file("src/scrapbook/internal/html.gleam", 201).
?DOC(false).
-spec get_property(
binary(),
binary(),
gleam@dynamic@decode:decoder(NLO),
fun((NLO) -> boolean())
) -> {ok, NLO} | {error, list(scrapbook@error:property_error())}.
get_property(Html, Key, Decoder, Validator) ->
do_get_property(Html, Key, Decoder, Validator, []).
-file("src/scrapbook/internal/html.gleam", 212).
?DOC(false).
-spec no_value_to_option(
{ok, NLT} | {error, list(scrapbook@error:property_error())}
) -> {ok, gleam@option:option(NLT)} |
{error, list(scrapbook@error:property_error())}.
no_value_to_option(Result) ->
case Result of
{ok, Value} ->
{ok, {some, Value}};
{error, [{find_key_value_error, {find_value_error, no_value}}]} ->
{ok, none};
{error, Err} ->
{error, Err}
end.
-file("src/scrapbook/internal/html.gleam", 225).
?DOC(false).
-spec no_prefix_to_option(
{ok, NMB} | {error, list(scrapbook@error:property_error())}
) -> {ok, gleam@option:option(NMB)} |
{error, list(scrapbook@error:property_error())}.
no_prefix_to_option(Result) ->
case Result of
{ok, Value} ->
{ok, {some, Value}};
{error, [{find_key_value_error, {no_prefix, _}}]} ->
{ok, none};
{error, Err} ->
{error, Err}
end.