Current section

Files

Jump to
glitch src glitch@types@emote.erl
Raw

src/glitch@types@emote.erl

-module(glitch@types@emote).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([format_to_string/1, format_from_string/1, format_decoder/0, decoder/0]).
-export_type([emote/0, format/0]).
-type emote() :: {emote, binary(), binary(), binary(), format()}.
-type format() :: animated | static.
-spec format_to_string(format()) -> binary().
format_to_string(Format) ->
case Format of
animated ->
<<"animated"/utf8>>;
static ->
<<"static"/utf8>>
end.
-spec format_from_string(binary()) -> {ok, format()} | {error, nil}.
format_from_string(String) ->
case String of
<<"animated"/utf8>> ->
{ok, animated};
<<"static"/utf8>> ->
{ok, static};
_ ->
{error, nil}
end.
-spec format_decoder() -> fun((gleam@dynamic:dynamic_()) -> {ok, format()} |
{error, list(gleam@dynamic:decode_error())}).
format_decoder() ->
fun(Data) ->
gleam@result:'try'(
gleam@dynamic:string(Data),
fun(String) -> _pipe = String,
_pipe@1 = format_from_string(_pipe),
gleam@result:replace_error(
_pipe@1,
[{decode_error,
<<"Format"/utf8>>,
<<<<"String("/utf8, String/binary>>/binary,
")"/utf8>>,
[]}]
) end
)
end.
-spec decoder() -> fun((gleam@dynamic:dynamic_()) -> {ok, emote()} |
{error, list(gleam@dynamic:decode_error())}).
decoder() ->
gleam@dynamic:decode4(
fun(Field@0, Field@1, Field@2, Field@3) -> {emote, Field@0, Field@1, Field@2, Field@3} end,
gleam@dynamic:field(<<"id"/utf8>>, fun gleam@dynamic:string/1),
gleam@dynamic:field(<<"emote_set_id"/utf8>>, fun gleam@dynamic:string/1),
gleam@dynamic:field(<<"owner_id"/utf8>>, fun gleam@dynamic:string/1),
gleam@dynamic:field(<<"format"/utf8>>, format_decoder())
).