1use std::fmt;
14use std::ops::Range;
15
16use crate::EntityId;
17
18mod damage;
19
20pub use damage::{RecordDamage, UnclassifiedDiagnostic};
21
22#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
24#[non_exhaustive]
25pub enum Severity {
26 #[default]
28 Warning,
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
34#[non_exhaustive]
35pub enum DiagnosticKind {
36 SkippedRecord,
38 DuplicateId,
40 DanglingReference,
42 RealWithoutPoint,
44 Other,
46}
47
48#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
52pub struct SourcePosition {
53 line: usize,
54 column: usize,
55}
56
57impl SourcePosition {
58 pub const fn new(line: usize, column: usize) -> Self {
60 Self { line, column }
61 }
62
63 pub const fn line(&self) -> usize {
65 self.line
66 }
67
68 pub const fn column(&self) -> usize {
70 self.column
71 }
72}
73
74impl fmt::Display for SourcePosition {
75 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
76 write!(formatter, "line {}, column {}", self.line, self.column)
77 }
78}
79
80#[derive(Debug, Clone, PartialEq, Eq)]
82pub struct Diagnostic {
83 severity: Severity,
84 kind: DiagnosticKind,
85 byte_range: Option<Range<usize>>,
86 position: Option<SourcePosition>,
87 instance: Option<EntityId>,
88 referrer: Option<EntityId>,
89 excerpt: Option<String>,
90 detail: String,
91}
92
93impl Diagnostic {
94 pub fn new(kind: DiagnosticKind, detail: impl Into<String>) -> Self {
97 Self {
98 severity: Severity::Warning,
99 kind,
100 byte_range: None,
101 position: None,
102 instance: None,
103 referrer: None,
104 excerpt: None,
105 detail: detail.into(),
106 }
107 }
108
109 pub fn warning(byte_range: Range<usize>, detail: impl Into<String>) -> Self {
112 Self::new(DiagnosticKind::Other, detail).with_byte_range(byte_range)
113 }
114
115 pub fn unlocated(detail: impl Into<String>) -> Self {
117 Self::new(DiagnosticKind::Other, detail)
118 }
119
120 #[must_use]
122 pub fn with_byte_range(mut self, range: Range<usize>) -> Self {
123 self.byte_range = Some(range);
124 self
125 }
126
127 #[must_use]
129 pub fn with_position(mut self, position: SourcePosition) -> Self {
130 self.position = Some(position);
131 self
132 }
133
134 #[must_use]
137 pub fn with_instance(mut self, id: EntityId) -> Self {
138 self.instance = Some(id);
139 self
140 }
141
142 #[must_use]
144 pub fn with_referrer(mut self, id: EntityId) -> Self {
145 self.referrer = Some(id);
146 self
147 }
148
149 #[must_use]
151 pub fn with_excerpt(mut self, excerpt: impl Into<String>) -> Self {
152 self.excerpt = Some(excerpt.into());
153 self
154 }
155
156 pub const fn severity(&self) -> Severity {
158 self.severity
159 }
160
161 pub const fn kind(&self) -> DiagnosticKind {
163 self.kind
164 }
165
166 pub const fn byte_range(&self) -> Option<&Range<usize>> {
168 self.byte_range.as_ref()
169 }
170
171 pub const fn instance(&self) -> Option<EntityId> {
175 self.instance
176 }
177
178 pub const fn referrer(&self) -> Option<EntityId> {
181 self.referrer
182 }
183
184 pub const fn position(&self) -> Option<SourcePosition> {
186 self.position
187 }
188
189 pub fn excerpt(&self) -> Option<&str> {
191 self.excerpt.as_deref()
192 }
193
194 pub fn detail(&self) -> &str {
196 &self.detail
197 }
198}
199
200impl fmt::Display for Diagnostic {
201 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
202 match (&self.position, &self.byte_range) {
203 (Some(position), Some(range)) => write!(
204 formatter,
205 "warning at {position} (bytes {}..{}): {}",
206 range.start, range.end, self.detail
207 ),
208 (Some(position), None) => {
209 write!(formatter, "warning at {position}: {}", self.detail)
210 }
211 (None, Some(range)) => write!(
212 formatter,
213 "warning at bytes {}..{}: {}",
214 range.start, range.end, self.detail
215 ),
216 (None, None) => write!(formatter, "warning: {}", self.detail),
217 }
218 }
219}