-module(glerd). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([do_main/1, main/0]). -spec field_type(glance:type()) -> binary(). field_type(Typ) -> case Typ of {named_type, Type_name, _, _} when Type_name =:= <<"String"/utf8>> -> <<"types.IsString"/utf8>>; {named_type, Type_name@1, _, _} when Type_name@1 =:= <<"Int"/utf8>> -> <<"types.IsInt"/utf8>>; {named_type, Type_name@2, _, _} when Type_name@2 =:= <<"Float"/utf8>> -> <<"types.IsFloat"/utf8>>; {named_type, Type_name@3, _, _} when Type_name@3 =:= <<"Bool"/utf8>> -> <<"types.IsBool"/utf8>>; {named_type, Type_name@4, _, [Typ@1]} when Type_name@4 =:= <<"List"/utf8>> -> <<<<"types.IsList("/utf8, (field_type(Typ@1))/binary>>/binary, ")"/utf8>>; {named_type, Type_name@5, _, [Key_type, Val_type]} when Type_name@5 =:= <<"Dict"/utf8>> -> <<<<<<<<"types.IsDict("/utf8, (field_type(Key_type))/binary>>/binary, ","/utf8>>/binary, (field_type(Val_type))/binary>>/binary, ")"/utf8>>; {named_type, Type_name@6, _, [Typ@2]} when Type_name@6 =:= <<"Option"/utf8>> -> <<<<"types.IsOption("/utf8, (field_type(Typ@2))/binary>>/binary, ")"/utf8>>; {named_type, Type_name@7, _, [Typ1, Typ2]} when Type_name@7 =:= <<"Result"/utf8>> -> <<<<<<<<"types.IsResult("/utf8, (field_type(Typ1))/binary>>/binary, ","/utf8>>/binary, (field_type(Typ2))/binary>>/binary, ")"/utf8>>; {tuple_type, [Typ1@1, Typ2@1]} -> <<<<<<<<"types.IsTuple2("/utf8, (field_type(Typ1@1))/binary>>/binary, ","/utf8>>/binary, (field_type(Typ2@1))/binary>>/binary, ")"/utf8>>; {tuple_type, [Typ1@2, Typ2@2, Typ3]} -> <<<<<<<<<<<<"types.IsTuple3("/utf8, (field_type(Typ1@2))/binary>>/binary, ","/utf8>>/binary, (field_type(Typ2@2))/binary>>/binary, ","/utf8>>/binary, (field_type(Typ3))/binary>>/binary, ")"/utf8>>; {tuple_type, [Typ1@3, Typ2@3, Typ3@1, Typ4]} -> <<<<<<<<<<<<<<<<"types.IsTuple4("/utf8, (field_type(Typ1@3))/binary>>/binary, ","/utf8>>/binary, (field_type(Typ2@3))/binary>>/binary, ","/utf8>>/binary, (field_type(Typ3@1))/binary>>/binary, ","/utf8>>/binary, (field_type(Typ4))/binary>>/binary, ")"/utf8>>; {tuple_type, [Typ1@4, Typ2@4, Typ3@2, Typ4@1, Typ5]} -> <<<<<<<<<<<<<<<<<<<<"types.IsTuple5("/utf8, (field_type(Typ1@4))/binary>>/binary, ","/utf8>>/binary, (field_type(Typ2@4))/binary>>/binary, ","/utf8>>/binary, (field_type(Typ3@2))/binary>>/binary, ","/utf8>>/binary, (field_type(Typ4@1))/binary>>/binary, ","/utf8>>/binary, (field_type(Typ5))/binary>>/binary, ")"/utf8>>; {tuple_type, [Typ1@5, Typ2@5, Typ3@3, Typ4@2, Typ5@1, Typ6]} -> <<<<<<<<<<<<<<<<<<<<<<<<"types.IsTuple6("/utf8, (field_type(Typ1@5))/binary>>/binary, ","/utf8>>/binary, (field_type(Typ2@5))/binary>>/binary, ","/utf8>>/binary, (field_type(Typ3@3))/binary>>/binary, ","/utf8>>/binary, (field_type(Typ4@2))/binary>>/binary, ","/utf8>>/binary, (field_type(Typ5@1))/binary>>/binary, ","/utf8>>/binary, (field_type(Typ6))/binary>>/binary, ")"/utf8>>; {named_type, Record_name, _, _} -> <<<<"types.IsRecord("/utf8, Record_name/binary>>/binary, ")"/utf8>>; _ -> <<"types.Unknown"/utf8>> end. -spec do_main(binary()) -> {ok, nil} | {error, simplifile:file_error()}. do_main(Root) -> Records_info = begin _pipe = fswalk:builder(), _pipe@1 = fswalk:with_path(_pipe, Root), _pipe@2 = fswalk:walk(_pipe@1), _pipe@3 = gleam@iterator:filter( _pipe@2, fun(Entry_result) -> case Entry_result of {ok, {entry, Path, {stat, Is_dir}}} -> (Is_dir =:= false) andalso gleam@string:ends_with( Path, <<".gleam"/utf8>> ); _ -> false end end ), _pipe@4 = gleam@iterator:map( _pipe@3, fun(Entry_result@1) -> {ok, {entry, Path@1, _}} = case Entry_result@1 of {ok, {entry, _, _}} -> Entry_result@1; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail, module => <<"glerd"/utf8>>, function => <<"do_main"/utf8>>, line => 28}) end, Path@1 end ), _pipe@5 = gleam@iterator:map( _pipe@4, fun(Path@2) -> _assert_subject = simplifile:read(Path@2), {ok, Content} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail@1 -> erlang:error(#{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail@1, module => <<"glerd"/utf8>>, function => <<"do_main"/utf8>>, line => 32}) end, Content end ), _pipe@6 = gleam@iterator:map( _pipe@5, fun(Content@1) -> _assert_subject@1 = glance:module(Content@1), {ok, Module} = case _assert_subject@1 of {ok, _} -> _assert_subject@1; _assert_fail@2 -> erlang:error(#{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail@2, module => <<"glerd"/utf8>>, function => <<"do_main"/utf8>>, line => 36}) end, Module end ), _pipe@7 = gleam@iterator:map( _pipe@6, fun(Module@1) -> {module, _, Custom_types_definitions, _, _, _} = Module@1, Custom_types_definitions end ), _pipe@9 = gleam@iterator:flat_map( _pipe@7, fun(Custom_type_definitions) -> _pipe@8 = (gleam@list:flat_map( Custom_type_definitions, fun(Custom_type_definition) -> {definition, _, Custom_type} = Custom_type_definition, {custom_type, _, _, _, _, Variants} = Custom_type, gleam@list:map( Variants, fun(Variant) -> {variant, Record_name, Fields} = Variant, Record_description = gleam@list:map( Fields, fun(Field) -> {field, Field_name, Typ} = Field, _assert_subject@2 = gleam@option:'or'( Field_name, {some, <<"__none__"/utf8>>} ), {some, Field_name@1} = case _assert_subject@2 of {some, _} -> _assert_subject@2; _assert_fail@3 -> erlang:error( #{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail@3, module => <<"glerd"/utf8>>, function => <<"do_main"/utf8>>, line => 53} ) end, <<<<<<<<"#(\""/utf8, Field_name@1/binary>>/binary, "\", "/utf8>>/binary, (field_type(Typ))/binary>>/binary, ")"/utf8>> end ), Record_fields = gleam@string:join( Record_description, <<","/utf8>> ), <<<<<<< "/utf8>>/binary, "["/utf8>>/binary, Record_fields/binary>>/binary, "]"/utf8>> end ) end )), gleam@iterator:from_list(_pipe@8) end ), gleam@iterator:to_list(_pipe@9) end, Records_keys = begin _pipe@10 = Records_info, _pipe@13 = gleam@list:map( _pipe@10, fun(Ri) -> _assert_subject@3 = begin _pipe@11 = Ri, _pipe@12 = gleam@string:split(_pipe@11, <<"->"/utf8>>), gleam@list:first(_pipe@12) end, {ok, Record_name@1} = case _assert_subject@3 of {ok, _} -> _assert_subject@3; _assert_fail@4 -> erlang:error(#{gleam_error => let_assert, message => <<"Assertion pattern match failed"/utf8>>, value => _assert_fail@4, module => <<"glerd"/utf8>>, function => <<"do_main"/utf8>>, line => 67}) end, gleam@string:trim(Record_name@1) end ), gleam@string:join(_pipe@13, <<"\n"/utf8>>) end, simplifile:write( <<<<"./"/utf8, Root/binary>>/binary, "/glerd_gen.gleam"/utf8>>, <<<<<<<<"// this file was generated via \"gleam run -m glerd\" import glerd/types pub type RecordKey {"/utf8, Records_keys/binary>>/binary, "} pub fn get_record_info(record_key) { case record_key {"/utf8>>/binary, (gleam@string:join(Records_info, <<"\n"/utf8>>))/binary>>/binary, "}}"/utf8>> ). -spec main() -> {ok, nil} | {error, simplifile:file_error()}. main() -> do_main(<<"src"/utf8>>).