-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([parse_status_code/1, status_code_to_string/1, status_code_suffix/1, status_code_to_int_pattern/1, status_code_to_int/1, sort_response_entries/1]). -export_type([http_status_code/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -type http_status_code() :: {status, integer()} | {status_range, integer()} | default_status. -file("src/oaspec/util/http.gleam", 17). ?DOC( " Parse a raw status code string from an OpenAPI spec into an HttpStatusCode.\n" " Returns Error(Nil) for unrecognisable strings.\n" ). -spec parse_status_code(binary()) -> {ok, http_status_code()} | {error, nil}. parse_status_code(Code) -> case Code of <<"default"/utf8>> -> {ok, default_status}; <<"1XX"/utf8>> -> {ok, {status_range, 1}}; <<"1xx"/utf8>> -> {ok, {status_range, 1}}; <<"2XX"/utf8>> -> {ok, {status_range, 2}}; <<"2xx"/utf8>> -> {ok, {status_range, 2}}; <<"3XX"/utf8>> -> {ok, {status_range, 3}}; <<"3xx"/utf8>> -> {ok, {status_range, 3}}; <<"4XX"/utf8>> -> {ok, {status_range, 4}}; <<"4xx"/utf8>> -> {ok, {status_range, 4}}; <<"5XX"/utf8>> -> {ok, {status_range, 5}}; <<"5xx"/utf8>> -> {ok, {status_range, 5}}; _ -> case gleam_stdlib:parse_int(Code) of {ok, N} -> {ok, {status, N}}; {error, _} -> {error, nil} end end. -file("src/oaspec/util/http.gleam", 34). ?DOC(" Convert an HttpStatusCode back to its canonical string form.\n"). -spec status_code_to_string(http_status_code()) -> binary(). status_code_to_string(Code) -> case Code of {status, N} -> erlang:integer_to_binary(N); {status_range, N@1} -> <<(erlang:integer_to_binary(N@1))/binary, "XX"/utf8>>; default_status -> <<"default"/utf8>> end. -file("src/oaspec/util/http.gleam", 45). ?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(http_status_code()) -> binary(). status_code_suffix(Code) -> case Code of {status, 200} -> <<"Ok"/utf8>>; {status, 201} -> <<"Created"/utf8>>; {status, 204} -> <<"NoContent"/utf8>>; {status, 400} -> <<"BadRequest"/utf8>>; {status, 401} -> <<"Unauthorized"/utf8>>; {status, 403} -> <<"Forbidden"/utf8>>; {status, 404} -> <<"NotFound"/utf8>>; {status, 409} -> <<"Conflict"/utf8>>; {status, 422} -> <<"UnprocessableEntity"/utf8>>; {status, 500} -> <<"InternalServerError"/utf8>>; {status, N} -> <<"Status"/utf8, (erlang:integer_to_binary(N))/binary>>; {status_range, 1} -> <<"Status1xx"/utf8>>; {status_range, 2} -> <<"Status2xx"/utf8>>; {status_range, 3} -> <<"Status3xx"/utf8>>; {status_range, 4} -> <<"Status4xx"/utf8>>; {status_range, 5} -> <<"Status5xx"/utf8>>; {status_range, N@1} -> <<<<"Status"/utf8, (erlang:integer_to_binary(N@1))/binary>>/binary, "xx"/utf8>>; default_status -> <<"Default"/utf8>> end. -file("src/oaspec/util/http.gleam", 71). ?DOC( " Convert an HttpStatusCode to a valid Gleam case pattern.\n" " Exact codes become integer literals, range codes become guard expressions,\n" " and DefaultStatus becomes \"_\" (catch-all).\n" ). -spec status_code_to_int_pattern(http_status_code()) -> binary(). status_code_to_int_pattern(Code) -> case Code of default_status -> <<"_"/utf8>>; {status_range, N} -> Low = erlang:integer_to_binary(N * 100), High = erlang:integer_to_binary((N * 100) + 99), <<<<<<"status if status >= "/utf8, Low/binary>>/binary, " && status <= "/utf8>>/binary, High/binary>>; {status, N@1} -> erlang:integer_to_binary(N@1) end. -file("src/oaspec/util/http.gleam", 96). ?DOC( " Convert an HttpStatusCode to an integer string for use in ServerResponse.\n" " Range codes and DefaultStatus map to a representative status code.\n" ). -spec status_code_to_int(http_status_code()) -> binary(). status_code_to_int(Code) -> case Code of default_status -> <<"500"/utf8>>; {status_range, N} -> erlang:integer_to_binary(N * 100); {status, N@1} -> erlang:integer_to_binary(N@1) end. -file("src/oaspec/util/http.gleam", 104). -spec status_sort_priority(http_status_code()) -> integer(). status_sort_priority(Code) -> case Code of default_status -> 9999; {status_range, N} -> (N * 100) + 1000; {status, N@1} -> N@1 end. -file("src/oaspec/util/http.gleam", 86). ?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({http_status_code(), HKK})) -> list({http_status_code(), HKK}). 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 ).