-module(dotenv_conf). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([read_string/2, read_string_or/3, read_int_or/3, read_float_or/3, read_file/2]). -export_type([env_file_error/0]). -type env_file_error() :: missing_file | invalid_file | file_error | bad_config | missing_var. -file("/home/pierre/Programming/gleam/dotenv_conf/src/dotenv_conf.gleam", 47). -spec read_string( binary(), {ok, gleam@dict:dict(binary(), binary())} | {error, env_file_error()} ) -> {ok, binary()} | {error, env_file_error()}. read_string(Name, From_file) -> case {envoy_ffi:get(Name), From_file} of {{ok, Value}, _} -> {ok, Value}; {_, {error, Err}} -> {error, Err}; {_, {ok, Values}} -> case gleam_stdlib:map_get(Values, Name) of {ok, Value@1} -> {ok, Value@1}; _ -> {error, missing_var} end end. -file("/home/pierre/Programming/gleam/dotenv_conf/src/dotenv_conf.gleam", 66). -spec read_string_or( binary(), {ok, gleam@dict:dict(binary(), binary())} | {error, env_file_error()}, binary() ) -> binary(). read_string_or(Name, From_file, Default) -> case read_string(Name, From_file) of {ok, Value} -> Value; _ -> Default end. -file("/home/pierre/Programming/gleam/dotenv_conf/src/dotenv_conf.gleam", 81). -spec read_int_or( binary(), {ok, gleam@dict:dict(binary(), binary())} | {error, env_file_error()}, integer() ) -> integer(). read_int_or(Name, From_file, Default) -> case read_string(Name, From_file) of {error, _} -> Default; {ok, Value_str} -> case gleam_stdlib:parse_int(Value_str) of {ok, Value} -> Value; _ -> Default end end. -file("/home/pierre/Programming/gleam/dotenv_conf/src/dotenv_conf.gleam", 96). -spec read_float_or( binary(), {ok, gleam@dict:dict(binary(), binary())} | {error, env_file_error()}, float() ) -> float(). read_float_or(Name, From_file, Default) -> case read_string(Name, From_file) of {error, _} -> Default; {ok, Value_str} -> case gleam_stdlib:parse_float(Value_str) of {ok, Value} -> Value; _ -> Default end end. -file("/home/pierre/Programming/gleam/dotenv_conf/src/dotenv_conf.gleam", 139). -spec parse_config_lines(list(binary()), list({binary(), binary()})) -> {ok, gleam@dict:dict(binary(), binary())} | {error, env_file_error()}. parse_config_lines(Lines, Acc) -> case Lines of [] -> {ok, maps:from_list(Acc)}; [Line | Rest] -> case gleam@string:split_once(Line, <<"="/utf8>>) of {error, nil} -> {error, bad_config}; {ok, L} -> parse_config_lines(Rest, [L | Acc]) end end. -file("/home/pierre/Programming/gleam/dotenv_conf/src/dotenv_conf.gleam", 131). -spec parse_dotenv_file(binary()) -> {ok, gleam@dict:dict(binary(), binary())} | {error, env_file_error()}. parse_dotenv_file(Content) -> _pipe = Content, _pipe@1 = gleam@string:trim(_pipe), _pipe@2 = gleam@string:replace(_pipe@1, <<"\r\n"/utf8>>, <<"\n"/utf8>>), _pipe@3 = gleam@string:split(_pipe@2, <<"\n"/utf8>>), parse_config_lines(_pipe@3, []). -file("/home/pierre/Programming/gleam/dotenv_conf/src/dotenv_conf.gleam", 117). -spec read_file( binary(), fun(({ok, gleam@dict:dict(binary(), binary())} | {error, env_file_error()}) -> FQZ) ) -> FQZ. read_file(Path, Then) -> File = case simplifile_erl:is_file(Path) of {error, _} -> {error, missing_file}; {ok, false} -> {error, invalid_file}; _ -> case simplifile:read(Path) of {ok, Content} -> parse_dotenv_file(Content); {error, _} -> {error, file_error} end end, Then(File).