-module(oaspec@openapi@diagnostic). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/oaspec/openapi/diagnostic.gleam"). -export([file_error/1, yaml_error/2, missing_field/2, invalid_value/2, resolve_error/3, capability/5, validation/5, errors_only/1, warnings_only/1, filter_by_mode/2, to_string/1, to_short_string/1]). -export_type([phase/0, severity/0, target/0, source_loc/0, diagnostic/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 phase() :: phase_parse | phase_normalize | phase_resolve | phase_capability_check | phase_validate | phase_codegen. -type severity() :: severity_error | severity_warning. -type target() :: target_both | target_client | target_server. -type source_loc() :: {source_loc, integer(), integer()} | no_source_loc. -type diagnostic() :: {diagnostic, binary(), phase(), severity(), target(), binary(), source_loc(), binary(), gleam@option:option(binary())}. -file("src/oaspec/openapi/diagnostic.gleam", 54). -spec file_error(binary()) -> diagnostic(). file_error(Detail) -> {diagnostic, <<"file_error"/utf8>>, phase_parse, severity_error, target_both, <<""/utf8>>, no_source_loc, Detail, none}. -file("src/oaspec/openapi/diagnostic.gleam", 67). -spec yaml_error(binary(), source_loc()) -> diagnostic(). yaml_error(Detail, Loc) -> {diagnostic, <<"yaml_error"/utf8>>, phase_parse, severity_error, target_both, <<""/utf8>>, Loc, Detail, none}. -file("src/oaspec/openapi/diagnostic.gleam", 80). -spec missing_field(binary(), binary()) -> diagnostic(). missing_field(Path, Field) -> {diagnostic, <<"missing_field"/utf8>>, phase_parse, severity_error, target_both, Path, no_source_loc, <<"Missing required field: "/utf8, Field/binary>>, {some, <<"Check your OpenAPI spec structure."/utf8>>}}. -file("src/oaspec/openapi/diagnostic.gleam", 93). -spec invalid_value(binary(), binary()) -> diagnostic(). invalid_value(Path, Detail) -> {diagnostic, <<"invalid_value"/utf8>>, phase_parse, severity_error, target_both, Path, no_source_loc, Detail, none}. -file("src/oaspec/openapi/diagnostic.gleam", 110). -spec resolve_error(binary(), binary(), gleam@option:option(binary())) -> diagnostic(). resolve_error(Path, Detail, Hint) -> {diagnostic, <<"resolve_error"/utf8>>, phase_resolve, severity_error, target_both, Path, no_source_loc, Detail, Hint}. -file("src/oaspec/openapi/diagnostic.gleam", 131). -spec capability( binary(), binary(), severity(), target(), gleam@option:option(binary()) ) -> diagnostic(). capability(Path, Detail, Severity, Target, Hint) -> {diagnostic, <<"capability"/utf8>>, phase_capability_check, Severity, Target, Path, no_source_loc, Detail, Hint}. -file("src/oaspec/openapi/diagnostic.gleam", 154). -spec validation( binary(), binary(), severity(), target(), gleam@option:option(binary()) ) -> diagnostic(). validation(Path, Detail, Severity, Target, Hint) -> {diagnostic, <<"validation"/utf8>>, phase_validate, Severity, Target, Path, no_source_loc, Detail, Hint}. -file("src/oaspec/openapi/diagnostic.gleam", 178). ?DOC(" Filter to only errors (not warnings).\n"). -spec errors_only(list(diagnostic())) -> list(diagnostic()). errors_only(Issues) -> gleam@list:filter( Issues, fun(E) -> erlang:element(4, E) =:= severity_error end ). -file("src/oaspec/openapi/diagnostic.gleam", 183). ?DOC(" Filter to only warnings (not errors).\n"). -spec warnings_only(list(diagnostic())) -> list(diagnostic()). warnings_only(Issues) -> gleam@list:filter( Issues, fun(E) -> erlang:element(4, E) =:= severity_warning end ). -file("src/oaspec/openapi/diagnostic.gleam", 188). ?DOC(" Filter diagnostics to those relevant for the selected generation mode.\n"). -spec filter_by_mode(list(diagnostic()), oaspec@config:generate_mode()) -> list(diagnostic()). filter_by_mode(Issues, Mode) -> case Mode of client -> gleam@list:filter( Issues, fun(E) -> erlang:element(5, E) /= target_server end ); server -> gleam@list:filter( Issues, fun(E@1) -> erlang:element(5, E@1) /= target_client end ); both -> Issues end. -file("src/oaspec/openapi/diagnostic.gleam", 204). ?DOC(" Convert a diagnostic to a human-readable string.\n"). -spec to_string(diagnostic()) -> binary(). to_string(D) -> Phase_str = case erlang:element(3, D) of phase_parse -> <<"Parse"/utf8>>; phase_normalize -> <<"Normalize"/utf8>>; phase_resolve -> <<"Resolve"/utf8>>; phase_capability_check -> <<"CapabilityCheck"/utf8>>; phase_validate -> <<"Validate"/utf8>>; phase_codegen -> <<"Codegen"/utf8>> end, Severity_str = case erlang:element(4, D) of severity_error -> <<"Error"/utf8>>; severity_warning -> <<"Warning"/utf8>> end, Loc_str = case erlang:element(7, D) of {source_loc, Line, Column} -> <<<<<<<<" (line "/utf8, (erlang:integer_to_binary(Line))/binary>>/binary, ", column "/utf8>>/binary, (erlang:integer_to_binary(Column))/binary>>/binary, ")"/utf8>>; no_source_loc -> <<""/utf8>> end, Pointer_str = case erlang:element(6, D) of <<""/utf8>> -> <<""/utf8>>; P -> <<" at "/utf8, P/binary>> end, Hint_str = case erlang:element(9, D) of {some, H} -> <<" "/utf8, H/binary>>; none -> <<""/utf8>> end, <<<<<<<<<<<<<<<<"["/utf8, Phase_str/binary>>/binary, "] "/utf8>>/binary, Severity_str/binary>>/binary, Pointer_str/binary>>/binary, ": "/utf8>>/binary, (erlang:element(8, D))/binary>>/binary, Loc_str/binary>>/binary, Hint_str/binary>>. -file("src/oaspec/openapi/diagnostic.gleam", 246). ?DOC(" Convert a diagnostic to a short string (for backward-compatible display).\n"). -spec to_short_string(diagnostic()) -> binary(). to_short_string(D) -> Prefix = case erlang:element(4, D) of severity_error -> <<"Error"/utf8>>; severity_warning -> <<"Warning"/utf8>> end, Loc_str = case erlang:element(7, D) of {source_loc, Line, Column} -> <<<<<<<<" (line "/utf8, (erlang:integer_to_binary(Line))/binary>>/binary, ", column "/utf8>>/binary, (erlang:integer_to_binary(Column))/binary>>/binary, ")"/utf8>>; no_source_loc -> <<""/utf8>> end, case erlang:element(2, D) of <<"file_error"/utf8>> -> erlang:element(8, D); <<"yaml_error"/utf8>> -> <<(erlang:element(8, D))/binary, Loc_str/binary>>; <<"missing_field"/utf8>> -> Location = case erlang:element(6, D) of <<""/utf8>> -> <<"root"/utf8>>; P -> P end, Field = gleam@string:replace( erlang:element(8, D), <<"Missing required field: "/utf8>>, <<""/utf8>> ), <<<<<<<<"Missing required field '"/utf8, Field/binary>>/binary, "' at "/utf8>>/binary, Location/binary>>/binary, (case erlang:element(9, D) of {some, H} -> <<". "/utf8, H/binary>>; none -> <<""/utf8>> end)/binary>>; <<"invalid_value"/utf8>> -> Location@1 = case erlang:element(6, D) of <<""/utf8>> -> <<"root"/utf8>>; P@1 -> P@1 end, <<<<<<"Invalid value at "/utf8, Location@1/binary>>/binary, ": "/utf8>>/binary, (erlang:element(8, D))/binary>>; _ -> <<<<<> -> <<""/utf8>>; P@2 -> <<" at "/utf8, P@2/binary>> end)/binary>>/binary, ": "/utf8>>/binary, (erlang:element( 8, D ))/binary>> end.