Skip to main content

src/examples@stories.erl

-module(examples@stories).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/examples/stories.gleam").
-export([main/0]).

-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.

?MODULEDOC(
    " Latest scrobble per user, optionally filtered by feed or restricted to\n"
    " people the viewer follows.\n"
    "\n"
    " Run with:\n"
    "   gleam run -m examples/stories\n"
).

-file("src/examples/stories.gleam", 16).
-spec main() -> nil.
main() ->
    Client = rocksky:new(),
    Payload@1 = case begin
        _pipe = rocksky@feed:get_stories(),
        _pipe@1 = rocksky:int_param(_pipe, <<"size"/utf8>>, 10),
        _pipe@2 = rocksky:param(
            _pipe@1,
            <<"feed"/utf8>>,
            <<"at://did:plc:vegqomyce4ssoqs7zwqvgqty/app.rocksky.feed.generator/metalcore"/utf8>>
        ),
        rocksky:send(_pipe@2, Client)
    end of
        {ok, Payload} -> Payload;
        _assert_fail ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"examples/stories"/utf8>>,
                        function => <<"main"/utf8>>,
                        line => 20,
                        value => _assert_fail,
                        start => 488,
                        'end' => 640,
                        pattern_start => 499,
                        pattern_end => 510})
    end,
    Row_decoder = begin
        gleam@dynamic@decode:optional_field(
            <<"handle"/utf8>>,
            <<"?"/utf8>>,
            {decoder, fun gleam@dynamic@decode:decode_string/1},
            fun(Handle) ->
                gleam@dynamic@decode:optional_field(
                    <<"artist"/utf8>>,
                    <<"?"/utf8>>,
                    {decoder, fun gleam@dynamic@decode:decode_string/1},
                    fun(Artist) ->
                        gleam@dynamic@decode:optional_field(
                            <<"title"/utf8>>,
                            <<"?"/utf8>>,
                            {decoder, fun gleam@dynamic@decode:decode_string/1},
                            fun(Title) ->
                                gleam@dynamic@decode:success(
                                    {Handle, Artist, Title}
                                )
                            end
                        )
                    end
                )
            end
        )
    end,
    Result_decoder = begin
        gleam@dynamic@decode:optional_field(
            <<"stories"/utf8>>,
            [],
            gleam@dynamic@decode:list(Row_decoder),
            fun(Stories) -> gleam@dynamic@decode:success(Stories) end
        )
    end,
    Stories@2 = case gleam@dynamic@decode:run(Payload@1, Result_decoder) of
        {ok, Stories@1} -> Stories@1;
        _assert_fail@1 ->
            erlang:error(#{gleam_error => let_assert,
                        message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
                        file => <<?FILEPATH/utf8>>,
                        module => <<"examples/stories"/utf8>>,
                        function => <<"main"/utf8>>,
                        line => 40,
                        value => _assert_fail@1,
                        start => 1087,
                        'end' => 1147,
                        pattern_start => 1098,
                        pattern_end => 1109})
    end,
    gleam@list:each(
        Stories@2,
        fun(S) ->
            {Handle@1, Artist@1, Title@1} = S,
            gleam_stdlib:println(
                <<<<<<<<<<"@"/utf8, Handle@1/binary>>/binary, "  "/utf8>>/binary,
                            Artist@1/binary>>/binary,
                        " — "/utf8>>/binary,
                    Title@1/binary>>
            )
        end
    ),
    gleam_stdlib:println(
        <<<<"\n"/utf8,
                (erlang:integer_to_binary(erlang:length(Stories@2)))/binary>>/binary,
            " stories"/utf8>>
    ).