domainforge_core/application/
diagnostic.rs1use serde::{Deserialize, Serialize};
8
9pub 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 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 pub fn all_code_slugs() -> [(&'static str, &'static str); 15] {
62 Self::all().map(|c| (c.code(), c.slug()))
63 }
64
65 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 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#[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 #[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 #[serde(skip_serializing_if = "Option::is_none")]
136 pub symbol_id: Option<String>,
137 #[serde(skip_serializing_if = "Option::is_none")]
140 pub field_path: Option<String>,
141 #[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, 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 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 pub fn with_document_kind(mut self, kind: &str) -> Self {
187 self.context.document_kind = Some(kind.to_string());
188 self
189 }
190}
191
192pub 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}