-module(acrostic@decoding). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([decode_to_string/1, read_i32/1, read_i64/1, read_bytes/2, read_varint/1, read_string/1, read_bool/1, read_len_field/2, read_len_packed_field/2, read_key/1]). -export_type([key/0]). -type key() :: {key, integer(), integer()}. -file("/Users/windy/study/gleam/acrostic/src/acrostic/decoding.gleam", 28). -spec decode_to_string(bitstring()) -> {ok, binary()} | {error, binary()}. decode_to_string(Binary) -> case begin _pipe = Binary, gleam@bit_array:to_string(_pipe) end of {ok, Str} -> {ok, Str}; {error, _} -> {error, <<"unable decode to string"/utf8>>} end. -file("/Users/windy/study/gleam/acrostic/src/acrostic/decoding.gleam", 49). -spec read_i32(bitstring()) -> {float(), bitstring()}. read_i32(Binary) -> case Binary of <> -> {First, Rest}; _ -> erlang:error(#{gleam_error => panic, message => <<"can't read i32"/utf8>>, module => <<"acrostic/decoding"/utf8>>, function => <<"read_i32"/utf8>>, line => 52}) end. -file("/Users/windy/study/gleam/acrostic/src/acrostic/decoding.gleam", 56). -spec read_i64(bitstring()) -> {float(), bitstring()}. read_i64(Binary) -> case Binary of <> -> {First, Rest}; _ -> erlang:error(#{gleam_error => panic, message => <<"can't read i64"/utf8>>, module => <<"acrostic/decoding"/utf8>>, function => <<"read_i64"/utf8>>, line => 59}) end. -file("/Users/windy/study/gleam/acrostic/src/acrostic/decoding.gleam", 107). -spec decode_to_list( bitstring(), fun((bitstring()) -> {MMA, bitstring()}), list(MMA) ) -> {ok, list(MMA)} | {error, binary()}. decode_to_list(Binary, Reader, Results) -> case Binary of <<>> -> {ok, Results}; _ -> {R, Rest} = Reader(Binary), decode_to_list(Rest, Reader, [R | Results]) end. -file("/Users/windy/study/gleam/acrostic/src/acrostic/decoding.gleam", 122). -spec calc_varint(list(integer()), integer()) -> integer(). calc_varint(Bytes, Sum) -> case erlang:length(Bytes) of Len when Len > 0 -> [First | Rest] = case Bytes of [_ | _] -> Bytes; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail, module => <<"acrostic/decoding"/utf8>>, function => <<"calc_varint"/utf8>>, line => 125}) end, calc_varint(Rest, erlang:'bsl'(First, (Len - 1) * 7) + Sum); _ -> Sum end. -file("/Users/windy/study/gleam/acrostic/src/acrostic/decoding.gleam", 148). -spec read_bytes(bitstring(), integer()) -> {ok, {bitstring(), bitstring()}} | {error, nil}. read_bytes(Bin, Length) -> case erlang:byte_size(Bin) >= Length of true -> {ok, {begin _pipe = gleam_stdlib:bit_array_slice(Bin, 0, Length), gleam@result:unwrap(_pipe, <<>>) end, begin _pipe@1 = gleam_stdlib:bit_array_slice( Bin, Length, erlang:byte_size(Bin) - Length ), gleam@result:unwrap(_pipe@1, <<>>) end}}; false -> {error, nil} end. -file("/Users/windy/study/gleam/acrostic/src/acrostic/decoding.gleam", 163). -spec read_byte(bitstring()) -> {bitstring(), integer()}. read_byte(Bin) -> case Bin of <<>> -> {<<>>, 0}; <> -> {Rest, Byte}; _ -> erlang:error(#{gleam_error => panic, message => <<"`panic` expression evaluated."/utf8>>, module => <<"acrostic/decoding"/utf8>>, function => <<"read_byte"/utf8>>, line => 167}) end. -file("/Users/windy/study/gleam/acrostic/src/acrostic/decoding.gleam", 132). -spec read_varint_bytes(bitstring(), list(integer())) -> {bitstring(), list(integer())}. read_varint_bytes(Bin, Results) -> case read_byte(Bin) of {Bin@1, 0} -> {Bin@1, Results}; {Bin@2, Byte} -> case erlang:'band'(Byte, 16#80) of Flag when Flag > 0 -> read_varint_bytes( Bin@2, [erlang:'band'(Byte, 16#7F) | Results] ); _ -> {Bin@2, [Byte | Results]} end end. -file("/Users/windy/study/gleam/acrostic/src/acrostic/decoding.gleam", 79). -spec read_varint(bitstring()) -> {integer(), bitstring()}. read_varint(Binary) -> {Binary@1, Bytes} = read_varint_bytes(Binary, []), {calc_varint(Bytes, 0), Binary@1}. -file("/Users/windy/study/gleam/acrostic/src/acrostic/decoding.gleam", 38). -spec read_string(bitstring()) -> {binary(), bitstring()}. read_string(Binary) -> {Len, Binary@1} = read_varint(Binary), case read_bytes(Binary@1, Len) of {ok, {Readed, Reset}} -> {begin _pipe = Readed, _pipe@1 = gleam@bit_array:to_string(_pipe), gleam@result:unwrap(_pipe@1, <<""/utf8>>) end, Reset}; _ -> erlang:error(#{gleam_error => panic, message => <<"`panic` expression evaluated."/utf8>>, module => <<"acrostic/decoding"/utf8>>, function => <<"read_string"/utf8>>, line => 45}) end. -file("/Users/windy/study/gleam/acrostic/src/acrostic/decoding.gleam", 70). -spec read_bool(bitstring()) -> {boolean(), bitstring()}. read_bool(Binary) -> {Num, Binary@1} = read_varint(Binary), case Num of 0 -> {false, Binary@1}; 1 -> {true, Binary@1}; X -> erlang:error(#{gleam_error => panic, message => (<<"Invalid bool: "/utf8, (gleam@int:to_string(X))/binary>>), module => <<"acrostic/decoding"/utf8>>, function => <<"read_bool"/utf8>>, line => 75}) end. -file("/Users/windy/study/gleam/acrostic/src/acrostic/decoding.gleam", 86). -spec read_len_field( bitstring(), fun((bitstring()) -> {ok, MLV} | {error, binary()}) ) -> {MLV, bitstring()}. read_len_field(Binary, Decoder) -> {Length, Binary@1} = read_varint(Binary), _assert_subject = read_bytes(Binary@1, Length), {ok, {Bytes, Rest}} = case _assert_subject of {ok, {_, _}} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail, module => <<"acrostic/decoding"/utf8>>, function => <<"read_len_field"/utf8>>, line => 91}) end, _assert_subject@1 = Decoder(Bytes), {ok, R} = case _assert_subject@1 of {ok, _} -> _assert_subject@1; _assert_fail@1 -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail@1, module => <<"acrostic/decoding"/utf8>>, function => <<"read_len_field"/utf8>>, line => 92}) end, {R, Rest}. -file("/Users/windy/study/gleam/acrostic/src/acrostic/decoding.gleam", 97). -spec read_len_packed_field( bitstring(), fun((bitstring()) -> {MLY, bitstring()}) ) -> {list(MLY), bitstring()}. read_len_packed_field(Binary, Reader) -> {Length, Binary@1} = read_varint(Binary), _assert_subject = read_bytes(Binary@1, Length), {ok, {Bytes, Rest}} = case _assert_subject of {ok, {_, _}} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail, module => <<"acrostic/decoding"/utf8>>, function => <<"read_len_packed_field"/utf8>>, line => 102}) end, _assert_subject@1 = decode_to_list(Bytes, Reader, []), {ok, R} = case _assert_subject@1 of {ok, _} -> _assert_subject@1; _assert_fail@1 -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail@1, module => <<"acrostic/decoding"/utf8>>, function => <<"read_len_packed_field"/utf8>>, line => 103}) end, {R, Rest}. -file("/Users/windy/study/gleam/acrostic/src/acrostic/decoding.gleam", 63). -spec read_key(bitstring()) -> {key(), bitstring()}. read_key(Binary) -> {Num, Binary@1} = read_varint(Binary), Wire_type = erlang:'band'(Num, 2#111), Field_number = erlang:'bsr'(Num - Wire_type, 3), {{key, Field_number, Wire_type}, Binary@1}.