Current section
Files
Jump to
Current section
Files
src/discord_gleam@ws@packets@message.erl
-module(discord_gleam@ws@packets@message).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([string_to_data/1]).
-export_type([message_author/0, message_packet_data/0, message_packet/0]).
-type message_author() :: {message_author, binary(), binary()}.
-type message_packet_data() :: {message_packet_data,
binary(),
binary(),
gleam@option:option(binary()),
binary(),
message_author()}.
-type message_packet() :: {message_packet,
binary(),
integer(),
integer(),
message_packet_data()}.
-file("src/discord_gleam/ws/packets/message.gleam", 25).
-spec string_to_data(binary()) -> {ok, message_packet()} | {error, binary()}.
string_to_data(Encoded) ->
Decoder = begin
gleam@dynamic@decode:field(
<<"t"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(T) ->
gleam@dynamic@decode:field(
<<"s"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(S) ->
gleam@dynamic@decode:field(
<<"op"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Op) ->
gleam@dynamic@decode:field(
<<"d"/utf8>>,
begin
gleam@dynamic@decode:field(
<<"content"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(Content) ->
gleam@dynamic@decode:field(
<<"id"/utf8>>,
discord_gleam@discord@snowflake:decoder(
),
fun(Id) ->
gleam@dynamic@decode:optional_field(
<<"guild_id"/utf8>>,
none,
begin
_pipe = discord_gleam@discord@snowflake:decoder(
),
gleam@dynamic@decode:map(
_pipe,
fun(Field@0) -> {some, Field@0} end
)
end,
fun(Guild_id) ->
gleam@dynamic@decode:field(
<<"channel_id"/utf8>>,
discord_gleam@discord@snowflake:decoder(
),
fun(
Channel_id
) ->
gleam@dynamic@decode:field(
<<"author"/utf8>>,
begin
gleam@dynamic@decode:field(
<<"id"/utf8>>,
discord_gleam@discord@snowflake:decoder(
),
fun(
Id@1
) ->
gleam@dynamic@decode:field(
<<"username"/utf8>>,
{decoder,
fun gleam@dynamic@decode:decode_string/1},
fun(
Username
) ->
gleam@dynamic@decode:success(
{message_author,
Id@1,
Username}
)
end
)
end
)
end,
fun(
Author
) ->
gleam@dynamic@decode:success(
{message_packet_data,
Content,
Id,
Guild_id,
Channel_id,
Author}
)
end
)
end
)
end
)
end
)
end
)
end,
fun(D) ->
gleam@dynamic@decode:success(
{message_packet, T, S, Op, D}
)
end
)
end
)
end
)
end
)
end,
_pipe@1 = gleam@json:parse(Encoded, Decoder),
gleam@result:map_error(
_pipe@1,
fun(_) -> <<"Failed to decode MessagePacket"/utf8>> end
).