Skip to main content

domainforge_core/application/
diagnostic.rs

1//! Stable APP diagnostic wire model (reference §5).
2//!
3//! Diagnostics are machine-readable `APPNNN code_slug` pairs. Every APP
4//! diagnostic is `Error` severity and blocks contract emission. Inapplicable
5//! context values are omitted, never serialized as empty strings.
6
7use serde::{Deserialize, Serialize};
8
9/// The closed set of APP014 closure-resolution reasons (reference §5).
10pub const APP014_SYMBOL_COLLISION: &str = "symbol_collision";
11pub const APP014_IMPORT_CYCLE: &str = "import_cycle";
12pub const APP014_UNRESOLVED_SPECIFIER: &str = "unresolved_specifier";
13pub const APP014_UNRESOLVED_ALIAS: &str = "unresolved_alias";
14pub const APP014_NOT_EXPORTED: &str = "not_exported";
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
17pub enum ApplicationDiagnosticCode {
18    #[serde(rename = "APP001")]
19    App001,
20    #[serde(rename = "APP002")]
21    App002,
22    #[serde(rename = "APP003")]
23    App003,
24    #[serde(rename = "APP004")]
25    App004,
26    #[serde(rename = "APP005")]
27    App005,
28    #[serde(rename = "APP006")]
29    App006,
30    #[serde(rename = "APP007")]
31    App007,
32    #[serde(rename = "APP008")]
33    App008,
34    #[serde(rename = "APP009")]
35    App009,
36    #[serde(rename = "APP010")]
37    App010,
38    #[serde(rename = "APP011")]
39    App011,
40    #[serde(rename = "APP012")]
41    App012,
42    #[serde(rename = "APP013")]
43    App013,
44    #[serde(rename = "APP014")]
45    App014,
46    #[serde(rename = "APP015")]
47    App015,
48}
49
50impl ApplicationDiagnosticCode {
51    /// Every registered APP code in numeric order.
52    pub fn all() -> [ApplicationDiagnosticCode; 15] {
53        use ApplicationDiagnosticCode::*;
54        [
55            App001, App002, App003, App004, App005, App006, App007, App008, App009, App010, App011,
56            App012, App013, App014, App015,
57        ]
58    }
59
60    /// The stable `("APPNNN", "code_slug")` registry.
61    pub fn all_code_slugs() -> [(&'static str, &'static str); 15] {
62        Self::all().map(|c| (c.code(), c.slug()))
63    }
64
65    /// Stable wire code, e.g. `"APP001"`.
66    pub fn code(&self) -> &'static str {
67        use ApplicationDiagnosticCode::*;
68        match self {
69            App001 => "APP001",
70            App002 => "APP002",
71            App003 => "APP003",
72            App004 => "APP004",
73            App005 => "APP005",
74            App006 => "APP006",
75            App007 => "APP007",
76            App008 => "APP008",
77            App009 => "APP009",
78            App010 => "APP010",
79            App011 => "APP011",
80            App012 => "APP012",
81            App013 => "APP013",
82            App014 => "APP014",
83            App015 => "APP015",
84        }
85    }
86
87    /// Every APP diagnostic blocks contract emission.
88    pub fn severity(&self) -> &'static str {
89        "error"
90    }
91
92    pub fn slug(&self) -> &'static str {
93        use ApplicationDiagnosticCode::*;
94        match self {
95            App001 => "missing_application_semantics",
96            App002 => "unknown_contract_reference",
97            App003 => "invalid_record_shape",
98            App004 => "duplicate_field",
99            App005 => "invalid_aggregate_key",
100            App006 => "operation_type_mismatch",
101            App007 => "policy_scope_error",
102            App008 => "invalid_failure_declaration",
103            App009 => "invalid_strategy_reference",
104            App010 => "invalid_not_applicable",
105            App011 => "effect_state_mismatch",
106            App012 => "constraint_error",
107            App013 => "enum_error",
108            App014 => "closure_resolution_error",
109            App015 => "artifact_schema_violation",
110        }
111    }
112}
113
114/// Structured diagnostic context (reference §5). Inapplicable values are
115/// omitted from the wire form.
116#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
117#[serde(deny_unknown_fields)]
118pub struct ApplicationDiagnosticContext {
119    #[serde(skip_serializing_if = "Option::is_none")]
120    pub reason: Option<String>,
121    #[serde(skip_serializing_if = "Option::is_none")]
122    pub expected: Option<String>,
123    #[serde(skip_serializing_if = "Option::is_none")]
124    pub actual: Option<String>,
125    #[serde(skip_serializing_if = "Option::is_none")]
126    pub remediation: Option<String>,
127    /// Source module logical ID (APP001–APP014).
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub logical_module_id: Option<String>,
130    #[serde(skip_serializing_if = "Option::is_none")]
131    pub line: Option<usize>,
132    #[serde(skip_serializing_if = "Option::is_none")]
133    pub column: Option<usize>,
134    /// Stable semantic ID when one has been assigned.
135    #[serde(skip_serializing_if = "Option::is_none")]
136    pub symbol_id: Option<String>,
137    /// Import or declaration field path for pre-identity resolver failures,
138    /// or the failing JSON Pointer for APP015.
139    #[serde(skip_serializing_if = "Option::is_none")]
140    pub field_path: Option<String>,
141    /// APP015 only.
142    #[serde(skip_serializing_if = "Option::is_none")]
143    pub document_kind: Option<String>,
144    #[serde(skip_serializing_if = "Option::is_none")]
145    pub schema_version: Option<String>,
146}
147
148#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
149#[serde(deny_unknown_fields)]
150pub struct ApplicationDiagnostic {
151    pub code: ApplicationDiagnosticCode,
152    pub slug: String,
153    pub severity: String, // always "error" in interactive and strict modes
154    pub message: String,
155    pub context: ApplicationDiagnosticContext,
156}
157
158impl ApplicationDiagnostic {
159    pub fn new(code: ApplicationDiagnosticCode, message: impl Into<String>) -> Self {
160        Self {
161            code,
162            slug: code.slug().to_string(),
163            severity: "error".to_string(),
164            message: message.into(),
165            context: ApplicationDiagnosticContext::default(),
166        }
167    }
168
169    /// APP014 with one closed reason value.
170    pub fn closure_error(reason: &str, message: impl Into<String>) -> Self {
171        let mut d = Self::new(ApplicationDiagnosticCode::App014, message);
172        d.context.reason = Some(reason.to_string());
173        d
174    }
175
176    pub fn at(mut self, logical_module_id: &str, line: usize, column: usize) -> Self {
177        self.context.logical_module_id = Some(logical_module_id.to_string());
178        self.context.line = Some(line);
179        self.context.column = Some(column);
180        self
181    }
182
183    /// Mark this APP015 as originating from an authored source map (parse,
184    /// graph build, pack-set input) rather than from persisted-artifact
185    /// validation. The two APP015 paths are distinguishable in context.
186    pub fn with_document_kind(mut self, kind: &str) -> Self {
187        self.context.document_kind = Some(kind.to_string());
188        self
189    }
190}
191
192/// Deterministic diagnostic order: logical ID, line, column, then APP code.
193pub fn sort_diagnostics(diags: &mut [ApplicationDiagnostic]) {
194    diags.sort_by(|a, b| {
195        (
196            &a.context.logical_module_id,
197            a.context.line,
198            a.context.column,
199            a.code,
200        )
201            .cmp(&(
202                &b.context.logical_module_id,
203                b.context.line,
204                b.context.column,
205                b.code,
206            ))
207    });
208}