Current section

Files

Jump to
glenvy src glenvy@env.erl
Raw

src/glenvy@env.erl

-module(glenvy@env).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/glenvy/env.gleam").
-export([all/0, string/1, parse/2, int/1, float/1, bool/1]).
-export_type([error/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(" Strongly-typed access to environment variables.\n").
-type error() :: {not_found, binary()} | {failed_to_parse, binary()}.
-file("src/glenvy/env.gleam", 19).
?DOC(" Returns all of the available environment variables for the current process.\n").
-spec all() -> gleam@dict:dict(binary(), binary()).
all() ->
glenvy_ffi:get_all_env().
-file("src/glenvy/env.gleam", 24).
?DOC(" Returns the value for the environment variable with the given name as a `String`.\n").
-spec string(binary()) -> {ok, binary()} | {error, error()}.
string(Name) ->
_pipe = glenvy_ffi:get_env(Name),
gleam@result:map_error(_pipe, fun(_) -> {not_found, Name} end).
-file("src/glenvy/env.gleam", 34).
?DOC(
" Returns the value for the environment variable with the given name.\n"
"\n"
" Uses the provided `parser` to parse the value.\n"
"\n"
" Returns `Error(FailedToParse)` if the provided `parser` returns an error.\n"
).
-spec parse(binary(), fun((binary()) -> {ok, USI} | {error, any()})) -> {ok,
USI} |
{error, error()}.
parse(Name, Parse) ->
gleam@result:'try'(string(Name), fun(Value) -> _pipe = Value,
_pipe@1 = Parse(_pipe),
gleam@result:map_error(
_pipe@1,
fun(_) -> {failed_to_parse, Name} end
) end).
-file("src/glenvy/env.gleam", 48).
?DOC(
" Returns the value for the environment variable with the given name as an `Int`.\n"
"\n"
" Returns `Error(FailedToParse)` if the environment variable cannot be parsed as an `Int`.\n"
).
-spec int(binary()) -> {ok, integer()} | {error, error()}.
int(Name) ->
_pipe = Name,
parse(_pipe, fun gleam_stdlib:parse_int/1).
-file("src/glenvy/env.gleam", 56).
?DOC(
" Returns the value for the environment variable with the given name as a `Float`.\n"
"\n"
" Returns `Error(FailedToParse)` if the environment variable cannot be parsed as a `Float`.\n"
).
-spec float(binary()) -> {ok, float()} | {error, error()}.
float(Name) ->
_pipe = Name,
parse(_pipe, fun gleam_stdlib:parse_float/1).
-file("src/glenvy/env.gleam", 82).
?DOC(
" Returns the value for the environment variable with the given name as a `Bool`.\n"
"\n"
" The following values are parsed as `True`:\n"
" - `true`\n"
" - `t`\n"
" - `yes`\n"
" - `y`\n"
" - `1`\n"
"\n"
" The following values are parsed as `False`:\n"
" - `false`\n"
" - `f`\n"
" - `no`\n"
" - `n`\n"
" - `0`\n"
"\n"
" The parsing is case-insensitive.\n"
"\n"
" Returns `Error(FailedToParse)` if the environment variable cannot be parsed as a `Bool`.\n"
"\n"
" Use `get` if you want to provide your own parser.\n"
).
-spec bool(binary()) -> {ok, boolean()} | {error, error()}.
bool(Name) ->
Parse_bool = fun(Value) ->
Value@1 = begin
_pipe = Value,
string:lowercase(_pipe)
end,
case Value@1 of
<<"true"/utf8>> ->
{ok, true};
<<"t"/utf8>> ->
{ok, true};
<<"yes"/utf8>> ->
{ok, true};
<<"y"/utf8>> ->
{ok, true};
<<"1"/utf8>> ->
{ok, true};
<<"false"/utf8>> ->
{ok, false};
<<"f"/utf8>> ->
{ok, false};
<<"no"/utf8>> ->
{ok, false};
<<"n"/utf8>> ->
{ok, false};
<<"0"/utf8>> ->
{ok, false};
_ ->
{error, nil}
end
end,
_pipe@1 = Name,
parse(_pipe@1, Parse_bool).