-module(glriff). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/glriff.gleam"). -export([to_bit_array/1, from_bit_array/1]). -export_type([chunk/0, from_bit_array_error/0, to_chunk_list_error/0]). -type chunk() :: {chunk, bitstring(), bitstring()} | {list_chunk, list(chunk())} | {riff_chunk, bitstring(), list(chunk())}. -type from_bit_array_error() :: {failed_to_create_chunk_list, to_chunk_list_error()} | invalid_format. -type to_chunk_list_error() :: invalid_id | invalid_size | invalid_data | {size_is_difference, integer(), integer()}. -file("src/glriff.gleam", 12). -spec to_bit_array(chunk()) -> bitstring(). to_bit_array(Chunk) -> case Chunk of {chunk, Four_cc, Data} -> Size = <<(erlang:byte_size(Data)):32/little>>, _pipe = [Four_cc, Size, Data], gleam_stdlib:bit_array_concat(_pipe); {list_chunk, Chunk_list} -> Id = <<"LIST"/utf8>>, Data@1 = begin _pipe@1 = Chunk_list, _pipe@3 = gleam@list:map(_pipe@1, fun(V) -> _pipe@2 = V, to_bit_array(_pipe@2) end), gleam_stdlib:bit_array_concat(_pipe@3) end, Size@1 = <<(erlang:byte_size(Data@1)):32/little>>, _pipe@4 = [Id, Size@1, Data@1], gleam_stdlib:bit_array_concat(_pipe@4); {riff_chunk, Four_cc@1, Chunk_list@1} -> Riff_header = <<"RIFF"/utf8>>, Data@2 = begin _pipe@5 = Chunk_list@1, _pipe@7 = gleam@list:map(_pipe@5, fun(V@1) -> _pipe@6 = V@1, to_bit_array(_pipe@6) end), gleam_stdlib:bit_array_concat(_pipe@7) end, Size@2 = <<(erlang:byte_size(Data@2)):32/little>>, _pipe@8 = [Riff_header, Size@2, Four_cc@1, Data@2], gleam_stdlib:bit_array_concat(_pipe@8) end. -file("src/glriff.gleam", 116). -spec to_chunk_list(bitstring(), integer()) -> {ok, list(chunk())} | {error, to_chunk_list_error()}. to_chunk_list(Bits, Position) -> Total_size = erlang:byte_size(Bits), case Position >= Total_size of true -> {ok, []}; false -> gleam@result:'try'( begin _pipe = gleam_stdlib:bit_array_slice(Bits, Position, 4), gleam@result:replace_error(_pipe, invalid_id) end, fun(Id) -> gleam@result:'try'( begin _pipe@1 = gleam_stdlib:bit_array_slice( Bits, Position + 4, 4 ), gleam@result:replace_error(_pipe@1, invalid_size) end, fun(Size_bits) -> Size@1 = case Size_bits of <> -> Size; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"glriff"/utf8>>, function => <<"to_chunk_list"/utf8>>, line => 136, value => _assert_fail, start => 3472, 'end' => 3519, pattern_start => 3483, pattern_end => 3507}) end, gleam@result:'try'( begin _pipe@2 = gleam_stdlib:bit_array_slice( Bits, Position + 8, Size@1 ), gleam@result:replace_error( _pipe@2, invalid_data ) end, fun(Data) -> gleam@bool:guard( Size@1 /= erlang:byte_size(Data), {error, {size_is_difference, Size@1, erlang:byte_size(Data)}}, fun() -> Next_position = (Position + 8) + Size@1, Chunk = {chunk, Id, Data}, Rest_chunks = to_chunk_list( Bits, Next_position ), case Rest_chunks of {ok, Values} -> {ok, [Chunk | Values]}; {error, Err} -> {error, Err} end end ) end ) end ) end ) end. -file("src/glriff.gleam", 47). -spec from_bit_array(bitstring()) -> {ok, chunk()} | {error, from_bit_array_error()}. from_bit_array(Bits) -> gleam@result:'try'( begin _pipe = gleam_stdlib:bit_array_slice(Bits, 0, 4), gleam@result:replace_error(_pipe, invalid_format) end, fun(Id) -> gleam@result:'try'( begin _pipe@1 = gleam_stdlib:bit_array_slice(Bits, 4, 4), gleam@result:replace_error(_pipe@1, invalid_format) end, fun(Size_bits) -> Size@1 = case Size_bits of <> -> Size; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"glriff"/utf8>>, function => <<"from_bit_array"/utf8>>, line => 58, value => _assert_fail, start => 1526, 'end' => 1573, pattern_start => 1537, pattern_end => 1561}) end, case Id of <<"RIFF"/utf8>> -> gleam@result:'try'( begin _pipe@2 = gleam_stdlib:bit_array_slice( Bits, 8, 4 ), gleam@result:replace_error( _pipe@2, invalid_format ) end, fun(Four_cc) -> Last_index = erlang:byte_size(Bits) - 12, case Last_index of 0 -> {ok, {riff_chunk, Four_cc, []}}; _ -> gleam@result:'try'( begin _pipe@3 = to_chunk_list( Bits, 12 ), gleam@result:map_error( _pipe@3, fun(Field@0) -> {failed_to_create_chunk_list, Field@0} end ) end, fun(Chunks) -> {ok, {riff_chunk, Four_cc, Chunks}} end ) end end ); <<"LIST"/utf8>> -> Last_index@1 = erlang:byte_size(Bits) - 8, case Last_index@1 of 0 -> {ok, {list_chunk, []}}; _ -> gleam@result:'try'( begin _pipe@4 = to_chunk_list(Bits, 8), gleam@result:map_error( _pipe@4, fun(Field@0) -> {failed_to_create_chunk_list, Field@0} end ) end, fun(Chunks@1) -> {ok, {list_chunk, Chunks@1}} end ) end; _ -> Last_index@2 = erlang:byte_size(Bits) - 8, gleam@result:'try'( begin _pipe@5 = gleam_stdlib:bit_array_slice( Bits, 8, Last_index@2 ), gleam@result:replace_error( _pipe@5, invalid_format ) end, fun(Data) -> case Size@1 =:= erlang:byte_size(Data) of true -> {ok, {chunk, Id, Data}}; false -> {error, invalid_format} end end ) end end ) end ).