Current section

Files

Jump to
oaspec src oaspec@util@http.erl
Raw

src/oaspec@util@http.erl

-module(oaspec@util@http).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/oaspec/util/http.gleam").
-export([status_code_suffix/1, status_code_to_int_pattern/1, status_code_to_int/1, sort_response_entries/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/http.gleam", 7).
?DOC(
" Shared HTTP status code utilities for code generation.\n"
" Get a human-readable suffix for a given HTTP status code.\n"
" Used to build anonymous type names like \"ListPetsResponseOk\".\n"
).
-spec status_code_suffix(binary()) -> binary().
status_code_suffix(Code) ->
case Code of
<<"200"/utf8>> ->
<<"Ok"/utf8>>;
<<"201"/utf8>> ->
<<"Created"/utf8>>;
<<"204"/utf8>> ->
<<"NoContent"/utf8>>;
<<"400"/utf8>> ->
<<"BadRequest"/utf8>>;
<<"401"/utf8>> ->
<<"Unauthorized"/utf8>>;
<<"403"/utf8>> ->
<<"Forbidden"/utf8>>;
<<"404"/utf8>> ->
<<"NotFound"/utf8>>;
<<"409"/utf8>> ->
<<"Conflict"/utf8>>;
<<"422"/utf8>> ->
<<"UnprocessableEntity"/utf8>>;
<<"500"/utf8>> ->
<<"InternalServerError"/utf8>>;
<<"default"/utf8>> ->
<<"Default"/utf8>>;
<<"1XX"/utf8>> ->
<<"Status1xx"/utf8>>;
<<"1xx"/utf8>> ->
<<"Status1xx"/utf8>>;
<<"2XX"/utf8>> ->
<<"Status2xx"/utf8>>;
<<"2xx"/utf8>> ->
<<"Status2xx"/utf8>>;
<<"3XX"/utf8>> ->
<<"Status3xx"/utf8>>;
<<"3xx"/utf8>> ->
<<"Status3xx"/utf8>>;
<<"4XX"/utf8>> ->
<<"Status4xx"/utf8>>;
<<"4xx"/utf8>> ->
<<"Status4xx"/utf8>>;
<<"5XX"/utf8>> ->
<<"Status5xx"/utf8>>;
<<"5xx"/utf8>> ->
<<"Status5xx"/utf8>>;
Other ->
<<"Status"/utf8, Other/binary>>
end.
-file("src/oaspec/util/http.gleam", 32).
?DOC(
" Convert a status code string to a valid Gleam case pattern.\n"
" Exact codes become integer literals, range codes become guard expressions,\n"
" and \"default\" becomes \"_\" (catch-all).\n"
).
-spec status_code_to_int_pattern(binary()) -> binary().
status_code_to_int_pattern(Code) ->
case Code of
<<"default"/utf8>> ->
<<"_"/utf8>>;
<<"1XX"/utf8>> ->
<<"status if status >= 100 && status <= 199"/utf8>>;
<<"1xx"/utf8>> ->
<<"status if status >= 100 && status <= 199"/utf8>>;
<<"2XX"/utf8>> ->
<<"status if status >= 200 && status <= 299"/utf8>>;
<<"2xx"/utf8>> ->
<<"status if status >= 200 && status <= 299"/utf8>>;
<<"3XX"/utf8>> ->
<<"status if status >= 300 && status <= 399"/utf8>>;
<<"3xx"/utf8>> ->
<<"status if status >= 300 && status <= 399"/utf8>>;
<<"4XX"/utf8>> ->
<<"status if status >= 400 && status <= 499"/utf8>>;
<<"4xx"/utf8>> ->
<<"status if status >= 400 && status <= 499"/utf8>>;
<<"5XX"/utf8>> ->
<<"status if status >= 500 && status <= 599"/utf8>>;
<<"5xx"/utf8>> ->
<<"status if status >= 500 && status <= 599"/utf8>>;
_ ->
Code
end.
-file("src/oaspec/util/http.gleam", 55).
?DOC(
" Convert a status code string to an integer string for use in ServerResponse.\n"
" Range codes and \"default\" map to a representative status code.\n"
).
-spec status_code_to_int(binary()) -> binary().
status_code_to_int(Code) ->
case Code of
<<"default"/utf8>> ->
<<"500"/utf8>>;
<<"1XX"/utf8>> ->
<<"100"/utf8>>;
<<"1xx"/utf8>> ->
<<"100"/utf8>>;
<<"2XX"/utf8>> ->
<<"200"/utf8>>;
<<"2xx"/utf8>> ->
<<"200"/utf8>>;
<<"3XX"/utf8>> ->
<<"300"/utf8>>;
<<"3xx"/utf8>> ->
<<"300"/utf8>>;
<<"4XX"/utf8>> ->
<<"400"/utf8>>;
<<"4xx"/utf8>> ->
<<"400"/utf8>>;
<<"5XX"/utf8>> ->
<<"500"/utf8>>;
<<"5xx"/utf8>> ->
<<"500"/utf8>>;
_ ->
Code
end.
-file("src/oaspec/util/http.gleam", 67).
-spec status_sort_priority(binary()) -> integer().
status_sort_priority(Code) ->
case Code of
<<"default"/utf8>> ->
9999;
<<"1XX"/utf8>> ->
1100;
<<"1xx"/utf8>> ->
1100;
<<"2XX"/utf8>> ->
1200;
<<"2xx"/utf8>> ->
1200;
<<"3XX"/utf8>> ->
1300;
<<"3xx"/utf8>> ->
1300;
<<"4XX"/utf8>> ->
1400;
<<"4xx"/utf8>> ->
1400;
<<"5XX"/utf8>> ->
1500;
<<"5xx"/utf8>> ->
1500;
_ ->
case gleam_stdlib:parse_int(Code) of
{ok, N} ->
N;
{error, _} ->
9998
end
end.
-file("src/oaspec/util/http.gleam", 47).
?DOC(
" Sort response entries so that exact codes come before ranges,\n"
" and ranges come before the default catch-all. This ensures\n"
" correct pattern matching order in generated case expressions.\n"
).
-spec sort_response_entries(list({binary(), HXG})) -> list({binary(), HXG}).
sort_response_entries(Entries) ->
gleam@list:sort(
Entries,
fun(A, B) ->
gleam@int:compare(
status_sort_priority(erlang:element(1, A)),
status_sort_priority(erlang:element(1, B))
)
end
).