Packages

Run midas effects and tasks on the BEAM.

Current section

Files

Jump to
midas_beam src midas@beam.erl
Raw

src/midas@beam.erl

-module(midas@beam).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/midas/beam.gleam").
-export([run/1]).
-file("src/midas/beam.gleam", 59).
-spec handle(
gleam@erlang@process:subject(gleam@uri:uri()),
gleam@http@request:request(any())
) -> gleam@http@response:response(mist:response_data()).
handle(Parent, Request) ->
gleam@erlang@process:send(Parent, gleam@http@request:to_uri(Request)),
_pipe = gleam@http@response:new(200),
gleam@http@response:set_body(
_pipe,
{bytes, gleam_stdlib:wrap_list(<<"hi"/utf8>>)}
).
-file("src/midas/beam.gleam", 46).
-spec do_follow(gleam@uri:uri()) -> {ok, gleam@uri:uri()} | {error, any()}.
do_follow(Uri) ->
Self = gleam@erlang@process:new_subject(),
case begin
_pipe = mist:new(fun(_capture) -> handle(Self, _capture) end),
_pipe@1 = mist:port(_pipe, 8080),
mist:start(_pipe@1)
end of
{ok, _} -> nil;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"midas/beam"/utf8>>,
function => <<"do_follow"/utf8>>,
line => 48,
value => _assert_fail,
start => 1263,
'end' => 1354,
pattern_start => 1274,
pattern_end => 1279})
end,
case shellout:command(
<<"open"/utf8>>,
[gleam@uri:to_string(Uri)],
<<"."/utf8>>,
[]
) of
{ok, _} -> nil;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"midas/beam"/utf8>>,
function => <<"do_follow"/utf8>>,
line => 53,
value => _assert_fail@1,
start => 1358,
'end' => 1456,
pattern_start => 1369,
pattern_end => 1374})
end,
Url = gleam_erlang_ffi:'receive'(Self),
{ok, Url}.
-file("src/midas/beam.gleam", 12).
-spec run(midas@effect:effect(QGN, any())) -> QGN.
run(Effect) ->
case Effect of
{done, Value} ->
Value;
{fetch, Request, Resume} ->
Reply = case gleam@httpc:send_bits(Request) of
{ok, Response} ->
{ok, Response};
{error, Reason} ->
Reason@1 = case Reason of
{failed_to_connect, _, _} ->
{network_error, <<"Failed to connect"/utf8>>};
response_timeout ->
{network_error, <<"Response timeout"/utf8>>};
invalid_utf8_response ->
unable_to_read_body
end,
{error, Reason@1}
end,
run(Resume(Reply));
{follow, Uri, Resume@1} ->
run(Resume@1(do_follow(Uri)));
{strong_random, Length, Resume@2} ->
run(Resume@2({ok, crypto:strong_rand_bytes(Length)}));
{hash, Algorithm, Bytes, Resume@3} ->
Algorithm@1 = case Algorithm of
sha256 ->
sha256;
_ ->
erlang:error(#{gleam_error => panic,
message => <<"unsupported"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"midas/beam"/utf8>>,
function => <<"run"/utf8>>,
line => 36})
end,
run(Resume@3({ok, gleam@crypto:hash(Algorithm@1, Bytes)}));
_ ->
erlang:error(#{gleam_error => panic,
message => <<"unsupported effect"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"midas/beam"/utf8>>,
function => <<"run"/utf8>>,
line => 41})
end.