-module(glemcached). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([new/2, with_timeout/2, with_authentication/3, connect/1, close/1]). -export_type([connection_options/0, connection_error/0]). -type connection_options() :: {connection_options, binary(), integer(), integer(), binary(), binary()}. -type connection_error() :: {connect_error, mug:error()} | authentication_failure. -file("/home/runner/work/glemcached/glemcached/src/glemcached.gleam", 23). -spec new(binary(), integer()) -> connection_options(). new(Host, Port) -> {connection_options, Host, Port, 1000, <<""/utf8>>, <<""/utf8>>}. -file("/home/runner/work/glemcached/glemcached/src/glemcached.gleam", 28). -spec with_timeout(connection_options(), integer()) -> connection_options(). with_timeout(Conn, Timeout) -> erlang:setelement(4, Conn, Timeout). -file("/home/runner/work/glemcached/glemcached/src/glemcached.gleam", 33). -spec with_authentication(connection_options(), binary(), binary()) -> connection_options(). with_authentication(Conn, Username, Password) -> erlang:setelement(6, erlang:setelement(5, Conn, Username), Password). -file("/home/runner/work/glemcached/glemcached/src/glemcached.gleam", 54). -spec check_auth(glemcached@types:memcached()) -> {ok, boolean()} | {error, connection_error()}. check_auth(Mem) -> case glemcached@text:add(Mem, <<"auth"/utf8>>, 0, -1, <<1>>) of {error, {command_error, {client_error, Error}}} -> case Error of <<"unauthenticated\r\n"/utf8>> -> {ok, true}; _ -> {ok, false} end; _ -> {ok, false} end. -file("/home/runner/work/glemcached/glemcached/src/glemcached.gleam", 68). -spec authenticate(glemcached@types:memcached(), binary(), binary()) -> boolean(). authenticate(Mem, User, Pass) -> Data = <<<>/binary, Pass/binary>>, case glemcached@text:set( Mem, <<"auth"/utf8>>, 0, 0, gleam_stdlib:identity(Data) ) of {ok, _} -> true; _ -> false end. -file("/home/runner/work/glemcached/glemcached/src/glemcached.gleam", 100). -spec connect(connection_options()) -> {ok, glemcached@types:memcached()} | {error, connection_error()}. connect(Opts) -> {connection_options, Host, Port, Timeout, Username, Password} = Opts, Conn = begin _pipe = mug:new(Host, Port), _pipe@1 = mug:timeout(_pipe, Timeout), _pipe@2 = mug:connect(_pipe@1), gleam@result:map_error( _pipe@2, fun(Field@0) -> {connect_error, Field@0} end ) end, gleam@result:'try'( Conn, fun(Socket) -> Mem = {memcached, Socket, Timeout}, gleam@result:'try'( check_auth(Mem), fun(Auth_required) -> case Auth_required of true -> case authenticate(Mem, Username, Password) of true -> {ok, Mem}; false -> {error, authentication_failure} end; _ -> {ok, Mem} end end ) end ). -file("/home/runner/work/glemcached/glemcached/src/glemcached.gleam", 123). -spec close(glemcached@types:memcached()) -> {ok, nil} | {error, mug:error()}. close(Mem) -> mug_ffi:shutdown(erlang:element(2, Mem)).