Current section

Files

Jump to
caffeine_lang src caffeine_lang@phase_1@parser@utils@general_common.erl
Raw

src/caffeine_lang@phase_1@parser@utils@general_common.erl

-module(caffeine_lang@phase_1@parser@utils@general_common).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/caffeine_lang/phase_1/parser/utils/general_common.gleam").
-export([parse_specification/4, string_to_accepted_type/1, extract_params_from_file_path/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/caffeine_lang/phase_1/parser/utils/general_common.gleam", 12).
?DOC(" Parses a specification file into a list of glaml documents according to the given parse function.\n").
-spec parse_specification(
binary(),
gleam@dict:dict(binary(), binary()),
fun((glaml:node_(), gleam@dict:dict(binary(), binary())) -> {ok, OTF} |
{error, binary()}),
binary()
) -> {ok, list(OTF)} | {error, binary()}.
parse_specification(File_path, Params, Parse_fn, Key) ->
gleam@result:'try'(
begin
_pipe = yaml_ffi:parse_file(File_path),
gleam@result:map_error(
_pipe,
fun(_) ->
<<"Failed to parse YAML file: "/utf8, File_path/binary>>
end
)
end,
fun(Doc) ->
Parse_fn_two = fun(Doc@1, _) ->
deps@glaml_extended@extractors:iteratively_parse_collection(
glaml:document_root(Doc@1),
Params,
Parse_fn,
Key
)
end,
case Doc of
[First | _] ->
Parse_fn_two(First, Params);
_ ->
{error, <<"Empty YAML file: "/utf8, File_path/binary>>}
end
end
).
-file("src/caffeine_lang/phase_1/parser/utils/general_common.gleam", 43).
?DOC(" Converts a string to an accepted type.\n").
-spec string_to_accepted_type(binary()) -> {ok,
caffeine_lang@types@common@accepted_types:accepted_types()} |
{error, binary()}.
string_to_accepted_type(String_val) ->
case String_val of
<<"String"/utf8>> ->
{ok, string};
<<"Integer"/utf8>> ->
{ok, integer};
<<"Boolean"/utf8>> ->
{ok, boolean};
<<"Decimal"/utf8>> ->
{ok, decimal};
<<"NonEmptyList(String)"/utf8>> ->
{ok, {non_empty_list, string}};
<<"NonEmptyList(Integer)"/utf8>> ->
{ok, {non_empty_list, integer}};
<<"NonEmptyList(Boolean)"/utf8>> ->
{ok, {non_empty_list, boolean}};
<<"NonEmptyList(Decimal)"/utf8>> ->
{ok, {non_empty_list, decimal}};
<<"Optional(String)"/utf8>> ->
{ok, {optional, string}};
<<"Optional(Integer)"/utf8>> ->
{ok, {optional, integer}};
<<"Optional(Boolean)"/utf8>> ->
{ok, {optional, boolean}};
<<"Optional(Decimal)"/utf8>> ->
{ok, {optional, decimal}};
<<"Optional(NonEmptyList(String))"/utf8>> ->
{ok, {optional, {non_empty_list, string}}};
<<"Optional(NonEmptyList(Integer))"/utf8>> ->
{ok, {optional, {non_empty_list, integer}}};
<<"Optional(NonEmptyList(Boolean))"/utf8>> ->
{ok, {optional, {non_empty_list, boolean}}};
<<"Optional(NonEmptyList(Decimal))"/utf8>> ->
{ok, {optional, {non_empty_list, decimal}}};
_ ->
case gleam_stdlib:string_starts_with(
String_val,
<<"List(List("/utf8>>
) of
true ->
{error,
<<"Only one level of recursion is allowed for lists: "/utf8,
String_val/binary>>};
false ->
case gleam_stdlib:string_starts_with(
String_val,
<<"Optional(List(List("/utf8>>
) of
true ->
{error,
<<"Only one level of recursion is allowed for lists, even in optional: "/utf8,
String_val/binary>>};
false ->
{error,
<<<<"Unknown attribute type: "/utf8,
String_val/binary>>/binary,
". Supported: String, Integer, Boolean, Decimal, NonEmptyList(String), NonEmptyList(Integer), NonEmptyList(Boolean), NonEmptyList(Decimal), Optional(String), Optional(Integer), Optional(Boolean), Optional(Decimal), Optional(NonEmptyList(String)), Optional(NonEmptyList(Integer)), Optional(NonEmptyList(Boolean)), Optional(NonEmptyList(Decimal))"/utf8>>}
end
end
end.
-file("src/caffeine_lang/phase_1/parser/utils/general_common.gleam", 113).
?DOC(" Extracts team and service name parameters from the file path.\n").
-spec extract_params_from_file_path(binary()) -> {ok,
gleam@dict:dict(binary(), binary())} |
{error, binary()}.
extract_params_from_file_path(File_path) ->
gleam@result:'try'(
case begin
_pipe = File_path,
_pipe@1 = gleam@string:split(_pipe, <<"/"/utf8>>),
lists:reverse(_pipe@1)
end of
[File, Team | _] ->
{ok,
{Team,
gleam@string:replace(
File,
<<".yaml"/utf8>>,
<<""/utf8>>
)}};
_ ->
{error,
<<"Invalid file path: expected at least 'team/service.yaml'"/utf8>>}
end,
fun(_use0) ->
{Team_name, Service_name} = _use0,
Params = maps:from_list(
[{<<"team_name"/utf8>>, Team_name},
{<<"service_name"/utf8>>, Service_name}]
),
{ok, Params}
end
).