Current section
Files
Jump to
Current section
Files
src/types@limits.erl
-module(types@limits).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([from_raw_data/2, do_from_vector/3, from_vector/1]).
-export_type([limits/0]).
-type limits() :: {limits, integer(), gleam@option:option(integer())}.
-spec from_raw_data(integer(), bitstring()) -> {ok, {limits(), integer()}} |
{error, binary()}.
from_raw_data(Position, Raw_data) ->
gleam@result:'try'(
case gleam_stdlib:bit_array_slice(Raw_data, Position, 1) of
{ok, <<16#00>>} ->
{ok, false};
{ok, <<16#01>>} ->
{ok, true};
{ok, _} ->
{error,
<<"limits::from_raw_data: can't get has_max flag"/utf8>>};
{error, _} ->
{error,
<<"limits::from_raw_data: can't get has_max flag"/utf8>>}
end,
fun(Has_max) ->
gleam@result:'try'(
util:decode_u32leb128(Position + 1, Raw_data),
fun(_use0) ->
{Min, Min_word_size} = _use0,
Bytes_read = 1 + Min_word_size,
gleam@result:'try'(case Has_max of
true ->
gleam@result:'try'(
util:decode_u32leb128(
(Position + Min_word_size) + 1,
Raw_data
),
fun(Mx) -> {ok, {some, Mx}} end
);
false ->
{ok, none}
end, fun(Maybe_max) ->
Bytes_read@1 = case Maybe_max of
{some, {_, Max_word_size}} ->
Bytes_read + Max_word_size;
none ->
Bytes_read
end,
case Maybe_max of
{some, {Max, _}} ->
{ok,
{{limits, Min, {some, Max}},
Bytes_read@1}};
none ->
{ok, {{limits, Min, none}, Bytes_read@1}}
end
end)
end
)
end
).
-spec do_from_vector(integer(), types@vector:vector(), list(limits())) -> {ok,
{list(limits()), integer()}} |
{error, binary()}.
do_from_vector(Position_accumulator, Vec, Result_accumulator) ->
case Position_accumulator < erlang:element(2, Vec) of
true ->
case from_raw_data(Position_accumulator, erlang:element(3, Vec)) of
{ok, Limit} ->
Result_accumulator@1 = lists:append(
Result_accumulator,
[erlang:element(1, Limit)]
),
do_from_vector(
Position_accumulator + erlang:element(2, Limit),
Vec,
Result_accumulator@1
);
{error, Reason} ->
{error, Reason}
end;
false ->
{ok, {Result_accumulator, Position_accumulator}}
end.
-spec from_vector(types@vector:vector()) -> {ok, {list(limits()), integer()}} |
{error, binary()}.
from_vector(Vec) ->
do_from_vector(0, Vec, []).