-module(deriv@util). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([decode_type_field/3, decoder_uuid/0, encode_uuid/1, indent/2, snake_case/1, replace_function/3, update_funcs/2, then/2, func_name/1, gleam_format/1, func_str/1, encode_birl_to_iso8601/1, encode_birl_to_naive/1, encode_birl_to_http/1, encode_birl_to_unix/1, encode_birl_to_unix_milli/1, encode_birl_to_unix_micro/1, decoder_birl_parse/0, decoder_birl_from_naive/0, decoder_birl_from_http/0, decoder_birl_from_unix/0, decoder_birl_from_unix_milli/0, decoder_birl_from_unix_micro/0, get_field_opts/4, birl_time_kind/4, fetch_module/1, fetch_custom_type/2]). -export_type([sc/0, birl_time_kind/0]). -type sc() :: {sc, list(list(binary())), list(binary()), boolean()}. -type birl_time_kind() :: birl_time_iso8601 | birl_time_naive | birl_time_http | birl_time_unix | birl_time_unix_milli | birl_time_unix_micro. -file("src/deriv/util.gleam", 17). -spec decode_type_field(binary(), binary(), decode:decoder(QDD)) -> decode:decoder(QDD). decode_type_field(Variant, Json_field, Decoder) -> _pipe = decode:into( begin decode:parameter(fun(Type_field) -> Type_field end) end ), _pipe@1 = decode:field( _pipe, Json_field, {decoder, fun gleam@dynamic:string/1} ), decode:then(_pipe@1, fun(Type_field@1) -> case Type_field@1 =:= Variant of true -> Decoder; false -> _pipe@2 = (<<<<<<<<"`"/utf8, Variant/binary>>/binary, "` failed to match `"/utf8>>/binary, Json_field/binary>>/binary, "`"/utf8>>), decode:fail(_pipe@2) end end). -file("src/deriv/util.gleam", 37). -spec decoder_uuid() -> decode:decoder(youid@uuid:uuid()). decoder_uuid() -> decode:then( {decoder, fun gleam@dynamic:string/1}, fun(Str) -> case youid@uuid:from_string(Str) of {ok, Uuid} -> decode:into(Uuid); {error, nil} -> decode:fail(<<"Failed to parse UUID"/utf8>>) end end ). -file("src/deriv/util.gleam", 45). -spec encode_uuid(youid@uuid:uuid()) -> gleam@json:json(). encode_uuid(Uuid) -> _pipe = Uuid, _pipe@1 = youid@uuid:to_string(_pipe), _pipe@2 = string:lowercase(_pipe@1), gleam@json:string(_pipe@2). -file("src/deriv/util.gleam", 52). -spec indent(binary(), integer()) -> binary(). indent(Str, Level) -> Pad = begin _pipe = <<" "/utf8>>, _pipe@1 = gleam@list:repeat(_pipe, Level), gleam@string:join(_pipe@1, <<""/utf8>>) end, <>. -file("src/deriv/util.gleam", 114). -spec step_snake_case(gleam@regexp:regexp(), sc(), binary()) -> sc(). step_snake_case(Is_capital, State, Char) -> {sc, Acc, Curr, Next_is_capital} = State, Char_is_capital = gleam@regexp:check(Is_capital, Char), case {Char_is_capital, Next_is_capital} of {true, false} -> {sc, lists:append(Acc, [lists:append(Curr, [Char])]), [], Char_is_capital}; {true, true} -> {sc, Acc, lists:append(Curr, [Char]), not Char_is_capital}; {_, _} -> {sc, Acc, lists:append(Curr, [Char]), Char_is_capital} end. -file("src/deriv/util.gleam", 61). -spec snake_case(binary()) -> binary(). snake_case(Str) -> _assert_subject = gleam@regexp:from_string(<<"[A-Z]"/utf8>>), {ok, Re} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail, module => <<"deriv/util"/utf8>>, function => <<"snake_case"/utf8>>, line => 62}) end, Step_snake_case = fun(State, Char) -> step_snake_case(Re, State, Char) end, _pipe = Str, _pipe@1 = gleam@string:split(_pipe, <<""/utf8>>), _pipe@2 = lists:reverse(_pipe@1), _pipe@3 = gleam@list:fold(_pipe@2, {sc, [], [], true}, Step_snake_case), (fun(Sc) -> _pipe@4 = case erlang:element(3, Sc) of [] -> Sc; _ -> _record = Sc, {sc, lists:append(erlang:element(2, Sc), [erlang:element(3, Sc)]), erlang:element(3, _record), erlang:element(4, _record)} end, _pipe@5 = (fun(Sc@1) -> case erlang:element(2, Sc@1) of [[Last, Second_to_last | Rest] | Rest_chunks] -> Last_is_uppercase = Last =:= string:uppercase(Last), Second_to_last_is_lowercase = Second_to_last =:= string:lowercase( Second_to_last ), case Last_is_uppercase andalso Second_to_last_is_lowercase of false -> erlang:element(2, Sc@1); true -> Second_to_last_chunk = [Second_to_last | Rest], Last_chunk = [Last], [Last_chunk, Second_to_last_chunk | Rest_chunks] end; _ -> erlang:element(2, Sc@1) end end)(_pipe@4), _pipe@9 = gleam@list:map(_pipe@5, fun(Group) -> _pipe@6 = Group, _pipe@7 = lists:reverse(_pipe@6), _pipe@8 = gleam@string:join(_pipe@7, <<""/utf8>>), string:lowercase(_pipe@8) end), _pipe@10 = lists:reverse(_pipe@9), gleam@string:join(_pipe@10, <<"_"/utf8>>) end)(_pipe@3). -file("src/deriv/util.gleam", 162). -spec drop_lines_up_to_and_including_lone_closing_brace(binary()) -> binary(). drop_lines_up_to_and_including_lone_closing_brace(Str) -> _pipe = Str, _pipe@1 = gleam@string:split(_pipe, <<"\n"/utf8>>), _pipe@2 = gleam@list:drop_while( _pipe@1, fun(Line) -> Line /= <<"}"/utf8>> end ), _pipe@6 = (fun(Lines) -> _pipe@3 = Lines, _pipe@4 = gleam@list:first(_pipe@3), (fun(Line@1) -> case Line@1 of {ok, <<"}"/utf8>>} -> _pipe@5 = Lines, gleam@list:drop(_pipe@5, 1); _ -> Lines end end)(_pipe@4) end)(_pipe@2), _pipe@7 = gleam@string:join(_pipe@6, <<"\n"/utf8>>), gleam@string:trim_start(_pipe@7). -file("src/deriv/util.gleam", 146). -spec replace_function(binary(), binary(), binary()) -> binary(). replace_function(Full_src, Func_name, Func_src) -> Re_str = <<<<"^(pub )?fn "/utf8, Func_name/binary>>/binary, "[(].*"/utf8>>, _assert_subject = gleam@regexp:compile(Re_str, {options, false, true}), {ok, Re} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail, module => <<"deriv/util"/utf8>>, function => <<"replace_function"/utf8>>, line => 148}) end, _assert_subject@1 = begin _pipe = gleam@regexp:split(Re, Full_src), gleam@list:filter( _pipe, fun(Str) -> (Str /= <<"pub "/utf8>>) andalso (Str /= <<""/utf8>>) end ) end, [Before, After] = case _assert_subject@1 of [_, _] -> _assert_subject@1; _assert_fail@1 -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail@1, module => <<"deriv/util"/utf8>>, function => <<"replace_function"/utf8>>, line => 150}) end, New_before = gleam@string:trim_end(Before), New_after = drop_lines_up_to_and_including_lone_closing_brace(After), _pipe@1 = [New_before, Func_src, New_after], gleam@string:join(_pipe@1, <<"\n\n"/utf8>>). -file("src/deriv/util.gleam", 181). -spec update_funcs(binary(), list({binary(), binary()})) -> binary(). update_funcs(Init_src, Funcs) -> gleam@list:fold( Funcs, Init_src, fun(Src, Func) -> {Func_name, Func_src} = Func, Re_str = <<<<"^(pub )?fn "/utf8, Func_name/binary>>/binary, "[(].*"/utf8>>, _assert_subject = gleam@regexp:compile( Re_str, {options, false, true} ), {ok, Re} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail, module => <<"deriv/util"/utf8>>, function => <<"update_funcs"/utf8>>, line => 186}) end, case gleam@regexp:check(Re, Src) of true -> replace_function(Src, Func_name, Func_src); false -> Newlines = case gleam_stdlib:string_ends_with( Src, <<"\n"/utf8>> ) of true -> <<"\n"/utf8>>; false -> <<"\n\n"/utf8>> end, <<<>/binary, Func_src/binary>> end end ). -file("src/deriv/util.gleam", 215). -spec log_and_discard_error(any()) -> nil. log_and_discard_error(Err) -> gleam@io:debug(Err), nil. -file("src/deriv/util.gleam", 204). -spec then({ok, QDL} | {error, any()}, fun((QDL) -> {ok, QDP} | {error, any()})) -> {ok, QDP} | {error, nil}. then(Result, Fun) -> _pipe = Result, _pipe@1 = gleam@result:map_error(_pipe, fun log_and_discard_error/1), gleam@result:then(_pipe@1, fun(X) -> _pipe@2 = Fun(X), gleam@result:map_error(_pipe@2, fun log_and_discard_error/1) end). -file("src/deriv/util.gleam", 245). -spec func_name(glance:definition(glance:function_())) -> binary(). func_name(Func) -> {definition, _, {function, Name, _, _, _, _, _}} = Func, Name. -file("src/deriv/util.gleam", 256). -spec gleam_format(binary()) -> binary(). gleam_format(Src) -> Escaped_src = begin _pipe = Src, _pipe@1 = gleam@string:replace(_pipe, <<"\""/utf8>>, <<"\\\""/utf8>>), gleam@string:replace(_pipe@1, <<"'"/utf8>>, <<"\\'"/utf8>>) end, Cmd = <<<<"echo \""/utf8, Escaped_src/binary>>/binary, "\" | gleam format --stdin"/utf8>>, _assert_subject = shellout:command( <<"sh"/utf8>>, [<<"-c"/utf8>>, Cmd], <<"."/utf8>>, [] ), {ok, Formatted_enc} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail, module => <<"deriv/util"/utf8>>, function => <<"gleam_format"/utf8>>, line => 263}) end, Formatted_enc. -file("src/deriv/util.gleam", 250). -spec func_str(glance:definition(glance:function_())) -> binary(). func_str(Func) -> _pipe = {module, [], [], [], [], [Func]}, _pipe@1 = glance_printer:print(_pipe), gleam_format(_pipe@1). -file("src/deriv/util.gleam", 301). -spec encode_birl_to_iso8601(birl:time()) -> gleam@json:json(). encode_birl_to_iso8601(Time) -> _pipe = Time, _pipe@1 = birl:to_iso8601(_pipe), gleam@json:string(_pipe@1). -file("src/deriv/util.gleam", 307). -spec encode_birl_to_naive(birl:time()) -> gleam@json:json(). encode_birl_to_naive(Time) -> _pipe = Time, _pipe@1 = birl:to_naive(_pipe), gleam@json:string(_pipe@1). -file("src/deriv/util.gleam", 313). -spec encode_birl_to_http(birl:time()) -> gleam@json:json(). encode_birl_to_http(Time) -> _pipe = Time, _pipe@1 = birl:to_naive(_pipe), gleam@json:string(_pipe@1). -file("src/deriv/util.gleam", 319). -spec encode_birl_to_unix(birl:time()) -> gleam@json:json(). encode_birl_to_unix(Time) -> _pipe = Time, _pipe@1 = birl:to_unix(_pipe), gleam@json:int(_pipe@1). -file("src/deriv/util.gleam", 325). -spec encode_birl_to_unix_milli(birl:time()) -> gleam@json:json(). encode_birl_to_unix_milli(Time) -> _pipe = Time, _pipe@1 = birl:to_unix_milli(_pipe), gleam@json:int(_pipe@1). -file("src/deriv/util.gleam", 331). -spec encode_birl_to_unix_micro(birl:time()) -> gleam@json:json(). encode_birl_to_unix_micro(Time) -> _pipe = Time, _pipe@1 = birl:to_unix_micro(_pipe), gleam@json:int(_pipe@1). -file("src/deriv/util.gleam", 337). -spec decoder_birl_string_to_result( fun((binary()) -> {ok, birl:time()} | {error, nil}), binary() ) -> decode:decoder(birl:time()). decoder_birl_string_to_result(Func, Func_name) -> _pipe = {decoder, fun gleam@dynamic:string/1}, decode:then(_pipe, fun(Str) -> case Func(Str) of {ok, Time} -> decode:into(Time); {error, _} -> decode:fail( <<<<<<"Failed to `"/utf8, Func_name/binary>>/binary, "`: "/utf8>>/binary, Str/binary>> ) end end). -file("src/deriv/util.gleam", 268). -spec decoder_birl_parse() -> decode:decoder(birl:time()). decoder_birl_parse() -> decoder_birl_string_to_result(fun birl:parse/1, <<"parse"/utf8>>). -file("src/deriv/util.gleam", 275). -spec decoder_birl_from_naive() -> decode:decoder(birl:time()). decoder_birl_from_naive() -> decoder_birl_string_to_result(fun birl:from_naive/1, <<"from_naive"/utf8>>). -file("src/deriv/util.gleam", 282). -spec decoder_birl_from_http() -> decode:decoder(birl:time()). decoder_birl_from_http() -> decoder_birl_string_to_result(fun birl:from_http/1, <<"from_http"/utf8>>). -file("src/deriv/util.gleam", 350). -spec decoder_birl_int_to_time(fun((integer()) -> birl:time())) -> decode:decoder(birl:time()). decoder_birl_int_to_time(Func) -> _pipe = {decoder, fun gleam@dynamic:int/1}, decode:then(_pipe, fun(Int) -> _pipe@1 = Int, _pipe@2 = Func(_pipe@1), decode:into(_pipe@2) end). -file("src/deriv/util.gleam", 289). -spec decoder_birl_from_unix() -> decode:decoder(birl:time()). decoder_birl_from_unix() -> decoder_birl_int_to_time(fun birl:from_unix/1). -file("src/deriv/util.gleam", 293). -spec decoder_birl_from_unix_milli() -> decode:decoder(birl:time()). decoder_birl_from_unix_milli() -> decoder_birl_int_to_time(fun birl:from_unix_milli/1). -file("src/deriv/util.gleam", 297). -spec decoder_birl_from_unix_micro() -> decode:decoder(birl:time()). decoder_birl_from_unix_micro() -> decoder_birl_int_to_time(fun birl:from_unix_micro/1). -file("src/deriv/util.gleam", 362). -spec get_field_opts( gleam@dict:dict(deriv@types:deriv_field(), list(deriv@types:deriv_field_opt())), glance:custom_type(), glance:variant(), binary() ) -> list(deriv@types:deriv_field_opt()). get_field_opts(All_field_opts, Type_, Variant, Field) -> Key = {deriv_field, erlang:element(2, Type_), erlang:element(2, Variant), Field}, _pipe = All_field_opts, _pipe@1 = gleam_stdlib:map_get(_pipe, Key), gleam@result:unwrap(_pipe@1, []). -file("src/deriv/util.gleam", 389). -spec birl_time_kind( glance:custom_type(), glance:variant(), binary(), gleam@dict:dict(deriv@types:deriv_field(), list(deriv@types:deriv_field_opt())) ) -> birl_time_kind(). birl_time_kind(Type_, Variant, Field, All_field_opts) -> case get_field_opts(All_field_opts, Type_, Variant, Field) of [] -> birl_time_iso8601; Opts -> _pipe = Opts, _pipe@1 = gleam@list:find_map( _pipe, fun(Opt) -> case erlang:element(2, Opt) of [<<"json"/utf8>>, <<"birl"/utf8>>, Val] -> {ok, Val}; _ -> {error, nil} end end ), _pipe@2 = gleam@result:unwrap(_pipe@1, <<"iso8601"/utf8>>), (fun(Val@1) -> case Val@1 of <<"iso8601"/utf8>> -> birl_time_iso8601; <<"naive"/utf8>> -> birl_time_naive; <<"http"/utf8>> -> birl_time_http; <<"unix"/utf8>> -> birl_time_unix; <<"unix_milli"/utf8>> -> birl_time_unix_milli; <<"unix_micro"/utf8>> -> birl_time_unix_micro; _ -> erlang:error(#{gleam_error => panic, message => (<<"Not a supported `json(birl(VALUE))`: "/utf8, Val@1/binary>>), module => <<"deriv/util"/utf8>>, function => <<"birl_time_kind"/utf8>>, line => 414}) end end)(_pipe@2) end. -file("src/deriv/util.gleam", 420). -spec fetch_module(binary()) -> {ok, glance:module_()} | {error, deriv@types:module_reader_err()}. fetch_module(Path) -> Filepath = <<<<"src/"/utf8, Path/binary>>/binary, ".gleam"/utf8>>, gleam@result:'try'( begin _pipe = simplifile:read(Filepath), gleam@result:map_error( _pipe, fun(Field@0) -> {file_err, Field@0} end ) end, fun(Src) -> gleam@result:'try'( begin _pipe@1 = glance:module(Src), gleam@result:map_error( _pipe@1, fun(Field@0) -> {glance_err, Field@0} end ) end, fun(Module) -> {ok, Module} end ) end ). -file("src/deriv/util.gleam", 439). -spec parse_ident(binary()) -> {ok, {binary(), binary()}} | {error, deriv@types:module_reader_err()}. parse_ident(Ident) -> case gleam@string:split(Ident, <<"."/utf8>>) of [A, B] -> {ok, {A, B}}; _ -> {error, {bad_ident, Ident}} end. -file("src/deriv/util.gleam", 446). -spec find_custom_type(binary(), glance:module_()) -> {ok, glance:definition(glance:custom_type())} | {error, deriv@types:module_reader_err()}. find_custom_type(Ref, Module) -> _pipe = erlang:element(3, Module), _pipe@1 = gleam@list:find( _pipe, fun(Type_) -> erlang:element(2, erlang:element(3, Type_)) =:= Ref end ), gleam@result:replace_error(_pipe@1, {custom_type_missing_err, Ref}). -file("src/deriv/util.gleam", 428). -spec fetch_custom_type( binary(), fun((binary()) -> {ok, glance:module_()} | {error, deriv@types:module_reader_err()}) ) -> {ok, {binary(), glance:definition(glance:custom_type())}} | {error, deriv@types:module_reader_err()}. fetch_custom_type(Ident, Read_module) -> gleam@result:'try'( parse_ident(Ident), fun(_use0) -> {Module_name, Ref} = _use0, gleam@result:'try'( Read_module(Module_name), fun(Module) -> gleam@result:'try'( find_custom_type(Ref, Module), fun(Type_) -> {ok, {Module_name, Type_}} end ) end ) end ).