Current section
Files
Jump to
Current section
Files
src/utils@env.erl
-module(utils@env).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src\\utils\\env.gleam").
-export([read_required_with_parser/4, read_required_int/2, read_required_float/2, read_required_bool/2, read_required_string/2, read_optional_with_parser/4, read_optional_int/2, read_optional_float/2, read_optional_bool/2, read_optional_string/2]).
-export_type([presence/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 presence() :: required | optional.
-file("src\\utils\\env.gleam", 132).
?DOC(" Parses `true` / `false` string values without case sensitivity.\n").
-spec parse_bool(binary()) -> {ok, boolean()} | {error, nil}.
parse_bool(Field) ->
case string:lowercase(Field) of
<<"true"/utf8>> ->
{ok, true};
<<"false"/utf8>> ->
{ok, false};
_ ->
{error, nil}
end.
-file("src\\utils\\env.gleam", 164).
-spec unwrap_required(
binary(),
{ok, gleam@option:option(DNN)} | {error, envoker@error:env_field_error()}
) -> {ok, DNN} | {error, envoker@error:env_field_error()}.
unwrap_required(Field_name, Value) ->
case Value of
{ok, {some, Value@1}} ->
{ok, Value@1};
{ok, none} ->
{error, {missing_field_error, Field_name}};
{error, Error} ->
{error, Error}
end.
-file("src\\utils\\env.gleam", 175).
-spec handle_missing_or_empty(
binary(),
presence(),
gleam@option:option(DNT),
boolean()
) -> {ok, gleam@option:option(DNT)} | {error, envoker@error:env_field_error()}.
handle_missing_or_empty(Field_name, Presence, Default, Is_empty) ->
case Default of
{some, Default@1} ->
{ok, {some, Default@1}};
none ->
case Presence of
optional ->
{ok, none};
required ->
{error, case Is_empty of
true ->
{empty_field_error, Field_name};
false ->
{missing_field_error, Field_name}
end}
end
end.
-file("src\\utils\\env.gleam", 140).
-spec read_value(
binary(),
presence(),
gleam@option:option(DNG),
binary(),
fun((binary()) -> {ok, DNG} | {error, nil})
) -> {ok, gleam@option:option(DNG)} | {error, envoker@error:env_field_error()}.
read_value(Field_name, Presence, Default, Expected_type, Parser) ->
case envoy_ffi:get(Field_name) of
{ok, Field} ->
Field@1 = gleam@string:trim(Field),
case Field@1 of
<<""/utf8>> ->
handle_missing_or_empty(Field_name, Presence, Default, true);
_ ->
_pipe = Parser(Field@1),
_pipe@1 = gleam@result:map(
_pipe,
fun(Field@0) -> {some, Field@0} end
),
gleam@result:map_error(
_pipe@1,
fun(_) ->
{parse_field_error, Field_name, Expected_type}
end
)
end;
{error, _} ->
handle_missing_or_empty(Field_name, Presence, Default, false)
end.
-file("src\\utils\\env.gleam", 107).
?DOC(
" Reads a required environment variable with a custom parser.\n"
" The value is normalized with `string.trim` before parsing.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_required_with_parser(
binary(),
gleam@option:option(DMR),
binary(),
fun((binary()) -> {ok, DMR} | {error, nil})
) -> {ok, DMR} | {error, envoker@error:env_field_error()}.
read_required_with_parser(Field_name, Default, Expected_type, Parser) ->
unwrap_required(
Field_name,
read_value(Field_name, required, Default, Expected_type, Parser)
).
-file("src\\utils\\env.gleam", 19).
?DOC(
" Reads a required integer environment variable.\n"
" The value is normalized with `string.trim` before parsing.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_required_int(binary(), gleam@option:option(integer())) -> {ok,
integer()} |
{error, envoker@error:env_field_error()}.
read_required_int(Field_name, Default) ->
read_required_with_parser(
Field_name,
Default,
<<"Int"/utf8>>,
fun gleam_stdlib:parse_int/1
).
-file("src\\utils\\env.gleam", 39).
?DOC(
" Reads a required float environment variable.\n"
" The value is normalized with `string.trim` before parsing.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_required_float(binary(), gleam@option:option(float())) -> {ok,
float()} |
{error, envoker@error:env_field_error()}.
read_required_float(Field_name, Default) ->
read_required_with_parser(
Field_name,
Default,
<<"Float"/utf8>>,
fun gleam_stdlib:parse_float/1
).
-file("src\\utils\\env.gleam", 60).
?DOC(
" Reads a required boolean environment variable.\n"
" The value is normalized with `string.trim` before parsing.\n"
" Parsing is case-insensitive.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_required_bool(binary(), gleam@option:option(boolean())) -> {ok,
boolean()} |
{error, envoker@error:env_field_error()}.
read_required_bool(Field_name, Default) ->
read_required_with_parser(
Field_name,
Default,
<<"Bool"/utf8>>,
fun parse_bool/1
).
-file("src\\utils\\env.gleam", 82).
?DOC(
" Reads a required string environment variable.\n"
" This is not a raw read: the value always goes through `string.trim`,\n"
" so leading and trailing whitespace is removed before returning.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_required_string(binary(), gleam@option:option(binary())) -> {ok,
binary()} |
{error, envoker@error:env_field_error()}.
read_required_string(Field_name, Default) ->
read_required_with_parser(
Field_name,
Default,
<<"String"/utf8>>,
fun(Field) -> {ok, Field} end
).
-file("src\\utils\\env.gleam", 122).
?DOC(
" Reads an optional environment variable with a custom parser.\n"
" The value is normalized with `string.trim` before parsing.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_optional_with_parser(
binary(),
gleam@option:option(DMX),
binary(),
fun((binary()) -> {ok, DMX} | {error, nil})
) -> {ok, gleam@option:option(DMX)} | {error, envoker@error:env_field_error()}.
read_optional_with_parser(Field_name, Default, Expected_type, Parser) ->
read_value(Field_name, optional, Default, Expected_type, Parser).
-file("src\\utils\\env.gleam", 29).
?DOC(
" Reads an optional integer environment variable.\n"
" The value is normalized with `string.trim` before parsing.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_optional_int(binary(), gleam@option:option(integer())) -> {ok,
gleam@option:option(integer())} |
{error, envoker@error:env_field_error()}.
read_optional_int(Field_name, Default) ->
read_optional_with_parser(
Field_name,
Default,
<<"Int"/utf8>>,
fun gleam_stdlib:parse_int/1
).
-file("src\\utils\\env.gleam", 49).
?DOC(
" Reads an optional float environment variable.\n"
" The value is normalized with `string.trim` before parsing.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_optional_float(binary(), gleam@option:option(float())) -> {ok,
gleam@option:option(float())} |
{error, envoker@error:env_field_error()}.
read_optional_float(Field_name, Default) ->
read_optional_with_parser(
Field_name,
Default,
<<"Float"/utf8>>,
fun gleam_stdlib:parse_float/1
).
-file("src\\utils\\env.gleam", 71).
?DOC(
" Reads an optional boolean environment variable.\n"
" The value is normalized with `string.trim` before parsing.\n"
" Parsing is case-insensitive.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_optional_bool(binary(), gleam@option:option(boolean())) -> {ok,
gleam@option:option(boolean())} |
{error, envoker@error:env_field_error()}.
read_optional_bool(Field_name, Default) ->
read_optional_with_parser(
Field_name,
Default,
<<"Bool"/utf8>>,
fun parse_bool/1
).
-file("src\\utils\\env.gleam", 95).
?DOC(
" Reads an optional string environment variable.\n"
" This is not a raw read: the value always goes through `string.trim`,\n"
" so leading and trailing whitespace is removed before returning.\n"
" Uses `default` when the field is missing or empty, if provided.\n"
).
-spec read_optional_string(binary(), gleam@option:option(binary())) -> {ok,
gleam@option:option(binary())} |
{error, envoker@error:env_field_error()}.
read_optional_string(Field_name, Default) ->
read_optional_with_parser(
Field_name,
Default,
<<"String"/utf8>>,
fun(Field) -> {ok, Field} end
).