-module(dove@response@decoder). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]). -export([decode/1]). -export_type([decode_result/0]). -type decode_result() :: more | {eoh, binary()} | {header, {binary(), binary()}, binary()} | {status_line, {{integer(), integer()}, integer(), binary()}, binary()}. -spec decode_headers(binary(), list({binary(), binary()})) -> {ok, {list({binary(), binary()}), binary()}} | {error, dove@error:error()}. decode_headers(Binary, Storage) -> gleam@result:then( begin _pipe = dove_ffi:decode_header(Binary), gleam@result:replace_error(_pipe, invalid_header) end, fun(Result) -> case Result of {eoh, Rest} -> {ok, {Storage, Rest}}; {header, Header, Rest@1} -> decode_headers(Rest@1, gleam@list:append(Storage, [Header])); more -> {error, more_needed}; {status_line, _, _} -> {error, wrong_packet_type} end end ). -spec decode(bitstring()) -> {ok, {integer(), list({binary(), binary()}), binary()}} | {error, dove@error:error()}. decode(Response) -> gleam@result:then( begin _pipe = gleam@bit_array:to_string(Response), gleam@result:replace_error(_pipe, is_not_string) end, fun(Response@1) -> case dove_ffi:decode_status_line(Response@1) of {ok, {status_line, {_, Status, _}, Rest}} -> gleam@result:then( decode_headers(Rest, []), fun(_use0) -> {Headers, Rest@1} = _use0, {ok, {Status, Headers, Rest@1}} end ); {ok, more} -> {error, more_needed}; {error, nil} -> {error, invalid_status_line}; _ -> {error, wrong_packet_type} end end ).