Current section

Files

Jump to
dot_env src dot_env@env.erl
Raw

src/dot_env@env.erl

-module(dot_env@env).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([set/2, get/1, get_or/2, get_then/2, get_int/1, get_int_or/2, get_bool/1, get_bool_or/2]).
-spec set(binary(), binary()) -> {ok, nil} | {error, binary()}.
set(Key, Value) ->
dot_env_ffi:set_env(Key, Value).
-spec get(binary()) -> {ok, binary()} | {error, binary()}.
get(Key) ->
dot_env_ffi:get_env(Key).
-spec get_or(binary(), binary()) -> binary().
get_or(Key, Default) ->
_pipe = dot_env_ffi:get_env(Key),
gleam@result:unwrap(_pipe, Default).
-spec get_then(binary(), fun((binary()) -> {ok, GGS} | {error, binary()})) -> {ok,
GGS} |
{error, binary()}.
get_then(Key, F) ->
case dot_env_ffi:get_env(Key) of
{ok, Value} ->
F(Value);
{error, Err} ->
{error, Err}
end.
-spec get_int(binary()) -> {ok, integer()} | {error, binary()}.
get_int(Key) ->
get_then(Key, fun(Raw_value) -> _pipe = gleam@int:parse(Raw_value),
gleam@result:map_error(
_pipe,
fun(_) ->
<<<<"Failed to parse environment variable for `"/utf8,
Key/binary>>/binary,
"` as integer"/utf8>>
end
) end).
-spec get_int_or(binary(), integer()) -> integer().
get_int_or(Key, Default) ->
_pipe = get_int(Key),
gleam@result:unwrap(_pipe, Default).
-spec get_bool(binary()) -> {ok, boolean()} | {error, binary()}.
get_bool(Key) ->
get_then(Key, fun(Raw_value) -> case gleam@string:lowercase(Raw_value) of
<<"true"/utf8>> ->
{ok, true};
<<"1"/utf8>> ->
{ok, true};
<<"false"/utf8>> ->
{ok, true};
<<"0"/utf8>> ->
{ok, true};
_ ->
{error,
<<<<"Invalid boolean value for environment variable `"/utf8,
Key/binary>>/binary,
"`. Expected one of `true`, `false`, `1`, or `0`."/utf8>>}
end end).
-spec get_bool_or(binary(), boolean()) -> boolean().
get_bool_or(Key, Default) ->
_pipe = get_bool(Key),
gleam@result:unwrap(_pipe, Default).