// Diagnostics: severity, code, JSON-pointer location, message. Strict mode
// (the default) promotes unsupported constructs to errors; lenient mode
// keeps them warnings.
///|
/// Severity level of a spec-loading or IR-lowering diagnostic.
#derive(Debug, Eq)
pub(all) enum Severity {
/// Fatal in `Strict` mode; recorded as an error in `Lenient` mode.
Error
/// Non-fatal advisory diagnostic.
Warning
}
///|
/// A single diagnostic emitted during OpenAPI parsing, bundling, merging, or IR lowering.
#derive(Debug, Eq)
pub(all) struct Diagnostic {
severity : Severity
code : String
pointer : String
message : String
}
///|
/// Severity of `unsupported` reports in the current mode.
#derive(Debug, Eq)
pub(all) enum Mode {
/// Unsupported constructs are errors (default).
Strict
/// Unsupported constructs are warnings.
Lenient
}
///|
/// Mutable diagnostic accumulator threaded through spec parsing and IR lowering.
#derive(Debug, Eq)
pub struct Diagnostics {
priv items : Array[Diagnostic]
mode : Mode
priv mut oas_31 : Bool
}
///|
/// Create an empty diagnostic collector in `mode` (default `Strict`).
pub fn Diagnostics::new(mode~ : Mode = Strict) -> Diagnostics {
{ items: [], mode, oas_31: false, }
}
///|
/// Record whether the loaded document declares OpenAPI 3.1.
pub fn Diagnostics::set_oas_31(self : Diagnostics, enabled : Bool) -> Unit {
self.oas_31 = enabled
}
///|
/// True when the loaded document is OpenAPI 3.1.
pub fn Diagnostics::is_oas_31(self : Diagnostics) -> Bool {
self.oas_31
}
///|
/// True when the collector is running in `Strict` mode.
pub fn Diagnostics::is_strict(self : Diagnostics) -> Bool {
self.mode is Strict
}
///|
/// Return a defensive copy of all recorded diagnostics in emission order.
pub fn Diagnostics::copy_items(self : Diagnostics) -> Array[Diagnostic] {
self.items.copy()
}
///|
/// Record an `Error`-severity diagnostic at JSON pointer `pointer`.
pub fn Diagnostics::error(
self : Diagnostics,
code : String,
pointer : String,
message : String,
) -> Unit {
self.items.push({ severity: Error, code, pointer, message, })
}
///|
/// Record a `Warning`-severity diagnostic at JSON pointer `pointer`.
pub fn Diagnostics::warning(
self : Diagnostics,
code : String,
pointer : String,
message : String,
) -> Unit {
self.items.push({ severity: Warning, code, pointer, message, })
}
///|
/// An unsupported construct: Error in strict mode, Warning in lenient mode.
pub fn Diagnostics::unsupported(
self : Diagnostics,
code : String,
pointer : String,
message : String,
) -> Unit {
let severity = match self.mode {
Strict => Error
Lenient => Warning
}
self.items.push({ severity, code, pointer, message, })
}
///|
/// True when any Error-severity diagnostic was recorded.
pub fn Diagnostics::has_errors(self : Diagnostics) -> Bool {
self.items.iter().find_first(fn(d) { d.severity is Error }) is Some(_)
}
///|
pub fn Diagnostics::length(self : Diagnostics) -> Int32 {
self.items.length()
}
///|
/// Formatted messages (`ERROR [code] /pointer: message`), in order.
pub fn Diagnostics::messages(self : Diagnostics) -> Array[String] {
let out : Array[String] = []
for d in self.items {
let level = match d.severity {
Error => "ERROR"
Warning => "WARNING"
}
let pointer = if d.pointer == "" { "" } else { " " + d.pointer }
out.push("\{level} [\{d.code}]\{pointer}: \{d.message}")
}
out
}