-module(oaspec@util@naming). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/oaspec/util/naming.gleam"). -export([capitalize/1, to_pascal_case/1, schema_to_type_name/1, to_snake_case/1, operation_to_function_name/1, to_module_name/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/oaspec/util/naming.gleam", 28). ?DOC(" Gleam reserved keywords that cannot be used as identifiers.\n"). -spec escape_keyword(binary()) -> binary(). escape_keyword(Name) -> case Name of <<"as"/utf8>> -> <>; <<"assert"/utf8>> -> <>; <<"auto"/utf8>> -> <>; <<"case"/utf8>> -> <>; <<"const"/utf8>> -> <>; <<"external"/utf8>> -> <>; <<"fn"/utf8>> -> <>; <<"if"/utf8>> -> <>; <<"import"/utf8>> -> <>; <<"let"/utf8>> -> <>; <<"opaque"/utf8>> -> <>; <<"panic"/utf8>> -> <>; <<"pub"/utf8>> -> <>; <<"test"/utf8>> -> <>; <<"todo"/utf8>> -> <>; <<"type"/utf8>> -> <>; <<"use"/utf8>> -> <>; _ -> Name end. -file("src/oaspec/util/naming.gleam", 70). ?DOC(" Capitalize the first letter of a string.\n"). -spec capitalize(binary()) -> binary(). capitalize(Input) -> case gleam_stdlib:string_pop_grapheme(Input) of {ok, {First, Rest}} -> <<(string:uppercase(First))/binary, Rest/binary>>; {error, _} -> Input end. -file("src/oaspec/util/naming.gleam", 87). ?DOC(" Split camelCase/PascalCase into separate words.\n"). -spec split_camel_case(binary()) -> list(binary()). split_camel_case(Input) -> Re@1 = case gleam@regexp:from_string( <<"([A-Z]+(?=[A-Z][a-z])|[A-Z]?[a-z]+|[A-Z]+|[0-9]+)"/utf8>> ) of {ok, Re} -> Re; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"oaspec/util/naming"/utf8>>, function => <<"split_camel_case"/utf8>>, line => 88, value => _assert_fail, start => 2183, 'end' => 2278, pattern_start => 2194, pattern_end => 2200}) end, Matches = gleam@regexp:scan(Re@1, Input), case Matches of [] -> [Input]; _ -> gleam@list:map( Matches, fun(M) -> {match, Content, _} = M, Content end ) end. -file("src/oaspec/util/naming.gleam", 78). ?DOC(" Split a string into words by common separators.\n"). -spec split_words(binary()) -> list(binary()). split_words(Input) -> Re@1 = case gleam@regexp:from_string(<<"[_\\-\\s./]+"/utf8>>) of {ok, Re} -> Re; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"oaspec/util/naming"/utf8>>, function => <<"split_words"/utf8>>, line => 79, value => _assert_fail, start => 1899, 'end' => 1953, pattern_start => 1910, pattern_end => 1916}) end, Parts = gleam@regexp:split(Re@1, Input), _pipe = Parts, _pipe@1 = gleam@list:flat_map(_pipe, fun split_camel_case/1), gleam@list:filter(_pipe@1, fun(S) -> S /= <<""/utf8>> end). -file("src/oaspec/util/naming.gleam", 7). ?DOC( " Convert a string to PascalCase for Gleam type names.\n" " Examples: \"pet_store\" -> \"PetStore\", \"get-user\" -> \"GetUser\"\n" ). -spec to_pascal_case(binary()) -> binary(). to_pascal_case(Input) -> _pipe = Input, _pipe@1 = split_words(_pipe), _pipe@2 = gleam@list:map(_pipe@1, fun capitalize/1), gleam@string:join(_pipe@2, <<""/utf8>>). -file("src/oaspec/util/naming.gleam", 58). ?DOC(" Convert an OpenAPI schema name to a valid Gleam type name.\n"). -spec schema_to_type_name(binary()) -> binary(). schema_to_type_name(Schema_name) -> _pipe = Schema_name, to_pascal_case(_pipe). -file("src/oaspec/util/naming.gleam", 102). ?DOC(" Insert underscores before capital letters in camelCase strings.\n"). -spec insert_underscores_before_caps(binary()) -> binary(). insert_underscores_before_caps(Input) -> Re@1 = case gleam@regexp:from_string(<<"([a-z0-9])([A-Z])"/utf8>>) of {ok, Re} -> Re; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, file => <>, module => <<"oaspec/util/naming"/utf8>>, function => <<"insert_underscores_before_caps"/utf8>>, line => 103, value => _assert_fail, start => 2599, 'end' => 2658, pattern_start => 2610, pattern_end => 2616}) end, gleam_regexp_ffi:replace(Re@1, Input, <<"\\1_\\2"/utf8>>). -file("src/oaspec/util/naming.gleam", 17). ?DOC( " Convert a string to snake_case for Gleam function/variable names.\n" " Examples: \"PetStore\" -> \"pet_store\", \"getUserById\" -> \"get_user_by_id\"\n" " Gleam keywords are suffixed with _ to avoid syntax errors.\n" ). -spec to_snake_case(binary()) -> binary(). to_snake_case(Input) -> Result = begin _pipe = Input, _pipe@1 = insert_underscores_before_caps(_pipe), _pipe@2 = split_words(_pipe@1), _pipe@3 = gleam@list:map(_pipe@2, fun string:lowercase/1), gleam@string:join(_pipe@3, <<"_"/utf8>>) end, escape_keyword(Result). -file("src/oaspec/util/naming.gleam", 52). ?DOC(" Convert an OpenAPI operation ID to a valid Gleam function name.\n"). -spec operation_to_function_name(binary()) -> binary(). operation_to_function_name(Operation_id) -> _pipe = Operation_id, to_snake_case(_pipe). -file("src/oaspec/util/naming.gleam", 64). ?DOC(" Convert an OpenAPI schema name to a valid Gleam module name.\n"). -spec to_module_name(binary()) -> binary(). to_module_name(Name) -> _pipe = Name, to_snake_case(_pipe).