-module(glitch@types@message). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([messsage_type_to_string/1, messsage_type_from_string/1, messsage_type_decoder/0, fragment_type_to_string/1, fragment_type_from_string/1, fragment_type_decoder/0, reply_decoder/0, mention_decoder/0, decoder/0]). -export_type([message/0, message_type/0, fragment/0, fragment_type/0, reply/0, mention/0]). -type message() :: {message, binary(), list(fragment())}. -type message_type() :: text | channel_points_highlighted | channel_points_sub_only | user_intro. -type fragment() :: {fragment, fragment_type(), binary(), gleam@option:option(glitch@types@cheermote:cheermote()), gleam@option:option(glitch@types@emote:emote()), gleam@option:option(mention())}. -type fragment_type() :: text_fragment | cheermote_fragment | emote_fragment | mention_fragment. -type reply() :: {reply, binary(), binary(), binary(), binary(), binary(), binary(), binary(), binary()}. -type mention() :: {mention, binary(), binary(), binary()}. -spec messsage_type_to_string(message_type()) -> binary(). messsage_type_to_string(Messsage_type) -> case Messsage_type of text -> <<"text"/utf8>>; channel_points_highlighted -> <<"channel_point_highlighted"/utf8>>; channel_points_sub_only -> <<"channel_points_sub_only"/utf8>>; user_intro -> <<"user_intro"/utf8>> end. -spec messsage_type_from_string(binary()) -> {ok, message_type()} | {error, nil}. messsage_type_from_string(String) -> case String of <<"text"/utf8>> -> {ok, text}; <<"channel_point_highlighted"/utf8>> -> {ok, channel_points_highlighted}; <<"channel_points_sub_only"/utf8>> -> {ok, channel_points_sub_only}; <<"user_intro"/utf8>> -> {ok, user_intro}; _ -> {error, nil} end. -spec messsage_type_decoder() -> fun((gleam@dynamic:dynamic_()) -> {ok, message_type()} | {error, list(gleam@dynamic:decode_error())}). messsage_type_decoder() -> fun(Data) -> gleam@result:'try'( gleam@dynamic:string(Data), fun(String) -> _pipe = String, _pipe@1 = messsage_type_from_string(_pipe), gleam@result:replace_error( _pipe@1, [{decode_error, <<"MessageType"/utf8>>, <<<<"String("/utf8, String/binary>>/binary, ")"/utf8>>, []}] ) end ) end. -spec fragment_type_to_string(fragment_type()) -> binary(). fragment_type_to_string(Fragment_type) -> case Fragment_type of text_fragment -> <<"text"/utf8>>; cheermote_fragment -> <<"cheermote"/utf8>>; emote_fragment -> <<"emote"/utf8>>; mention_fragment -> <<"mentiond"/utf8>> end. -spec fragment_type_from_string(binary()) -> {ok, fragment_type()} | {error, nil}. fragment_type_from_string(String) -> case String of <<"text"/utf8>> -> {ok, text_fragment}; <<"cheermote"/utf8>> -> {ok, cheermote_fragment}; <<"emote"/utf8>> -> {ok, emote_fragment}; <<"mentiond"/utf8>> -> {ok, mention_fragment}; _ -> {error, nil} end. -spec fragment_type_decoder() -> fun((gleam@dynamic:dynamic_()) -> {ok, fragment_type()} | {error, list(gleam@dynamic:decode_error())}). fragment_type_decoder() -> fun(Data) -> gleam@result:'try'( gleam@dynamic:string(Data), fun(String) -> _pipe = String, _pipe@1 = fragment_type_from_string(_pipe), gleam@result:replace_error( _pipe@1, [{decode_error, <<"FragmentType"/utf8>>, <<<<"String("/utf8, String/binary>>/binary, ")"/utf8>>, []}] ) end ) end. -spec reply_decoder() -> fun((gleam@dynamic:dynamic_()) -> {ok, reply()} | {error, list(gleam@dynamic:decode_error())}). reply_decoder() -> gleam@dynamic:decode8( fun(Field@0, Field@1, Field@2, Field@3, Field@4, Field@5, Field@6, Field@7) -> {reply, Field@0, Field@1, Field@2, Field@3, Field@4, Field@5, Field@6, Field@7} end, gleam@dynamic:field( <<"parent_message_id"/utf8>>, fun gleam@dynamic:string/1 ), gleam@dynamic:field( <<"parent_message_body"/utf8>>, fun gleam@dynamic:string/1 ), gleam@dynamic:field( <<"parent_message_body"/utf8>>, fun gleam@dynamic:string/1 ), gleam@dynamic:field( <<"parent_user_name"/utf8>>, fun gleam@dynamic:string/1 ), gleam@dynamic:field( <<"parent_user_name"/utf8>>, fun gleam@dynamic:string/1 ), gleam@dynamic:field( <<"thread_user_id"/utf8>>, fun gleam@dynamic:string/1 ), gleam@dynamic:field( <<"thread_user_id"/utf8>>, fun gleam@dynamic:string/1 ), gleam@dynamic:field( <<"thread_user_login"/utf8>>, fun gleam@dynamic:string/1 ) ). -spec mention_decoder() -> fun((gleam@dynamic:dynamic_()) -> {ok, mention()} | {error, list(gleam@dynamic:decode_error())}). mention_decoder() -> gleam@dynamic:decode3( fun(Field@0, Field@1, Field@2) -> {mention, Field@0, Field@1, Field@2} end, gleam@dynamic:field(<<"user_id"/utf8>>, fun gleam@dynamic:string/1), gleam@dynamic:field(<<"user_name"/utf8>>, fun gleam@dynamic:string/1), gleam@dynamic:field(<<"user_login"/utf8>>, fun gleam@dynamic:string/1) ). -spec message_fragment_decoder() -> fun((gleam@dynamic:dynamic_()) -> {ok, fragment()} | {error, list(gleam@dynamic:decode_error())}). message_fragment_decoder() -> gleam@dynamic:decode5( fun(Field@0, Field@1, Field@2, Field@3, Field@4) -> {fragment, Field@0, Field@1, Field@2, Field@3, Field@4} end, gleam@dynamic:field(<<"type"/utf8>>, fragment_type_decoder()), gleam@dynamic:field(<<"text"/utf8>>, fun gleam@dynamic:string/1), gleam@dynamic:optional_field( <<"cheermote"/utf8>>, glitch@types@cheermote:decoder() ), gleam@dynamic:optional_field( <<"emote"/utf8>>, glitch@types@emote:decoder() ), gleam@dynamic:optional_field(<<"mention"/utf8>>, mention_decoder()) ). -spec decoder() -> fun((gleam@dynamic:dynamic_()) -> {ok, message()} | {error, list(gleam@dynamic:decode_error())}). decoder() -> gleam@dynamic:decode2( fun(Field@0, Field@1) -> {message, Field@0, Field@1} end, gleam@dynamic:field(<<"text"/utf8>>, fun gleam@dynamic:string/1), gleam@dynamic:field( <<"fragments"/utf8>>, gleam@dynamic:list(message_fragment_decoder()) ) ).