-module(dagger@session). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/dagger/session.gleam"). -export([with_session/1]). -export_type([port_/0, session_handle/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. -type port_() :: any(). -type session_handle() :: {session_handle, port_(), dagger@types:client()}. -file("src/dagger/session.gleam", 52). -spec string_of_json_error(gleam@json:decode_error()) -> binary(). string_of_json_error(E) -> case E of unexpected_end_of_input -> <<"unexpected end of input"/utf8>>; {unexpected_byte, B} -> <<"unexpected byte: "/utf8, B/binary>>; {unexpected_sequence, S} -> <<"unexpected sequence: "/utf8, S/binary>>; {unable_to_decode, _} -> <<"unable to decode"/utf8>> end. -file("src/dagger/session.gleam", 37). -spec parse_session_json(binary()) -> {ok, dagger@types:client()} | {error, binary()}. parse_session_json(Json_str) -> Decoder = begin gleam@dynamic@decode:field( <<"port"/utf8>>, {decoder, fun gleam@dynamic@decode:decode_int/1}, fun(Port) -> gleam@dynamic@decode:field( <<"session_token"/utf8>>, {decoder, fun gleam@dynamic@decode:decode_string/1}, fun(Token) -> Endpoint = <<<<"http://127.0.0.1:"/utf8, (erlang:integer_to_binary(Port))/binary>>/binary, "/query"/utf8>>, gleam@dynamic@decode:success({client, Endpoint, Token}) end ) end ) end, _pipe = gleam@json:parse(Json_str, Decoder), gleam@result:map_error( _pipe, fun(E) -> <<<<<<"Failed to decode dagger session JSON: "/utf8, Json_str/binary>>/binary, " — "/utf8>>/binary, (string_of_json_error(E))/binary>> end ). -file("src/dagger/session.gleam", 29). -spec open(fun((session_handle()) -> {ok, FOP} | {error, binary()})) -> {ok, FOP} | {error, binary()}. open(Callback) -> gleam@result:'try'( dagger_session_ffi:open_session(<<"dagger session"/utf8>>), fun(_use0) -> {Port, Json_line} = _use0, gleam@result:'try'( parse_session_json(Json_line), fun(Client) -> Result = Callback({session_handle, Port, Client}), dagger_session_ffi:close_session(Port), Result end ) end ). -file("src/dagger/session.gleam", 24). ?DOC( " Avvia `dagger session`, aspetta il JSON di connessione, esegue\n" " la callback con il Client e chiude la sessione al termine.\n" ). -spec with_session(fun((dagger@types:client()) -> FOM)) -> {ok, FOM} | {error, binary()}. with_session(Callback) -> open(fun(Handle) -> {ok, Callback(erlang:element(3, Handle))} end).