|
| 1 | +//! Diagnostic message types and related structures. |
| 2 | +
|
| 3 | +use rowan::TextRange; |
| 4 | + |
| 5 | +/// Severity level of a diagnostic. |
| 6 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] |
| 7 | +pub enum Severity { |
| 8 | + #[default] |
| 9 | + Error, |
| 10 | + Warning, |
| 11 | +} |
| 12 | + |
| 13 | +impl std::fmt::Display for Severity { |
| 14 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 15 | + match self { |
| 16 | + Severity::Error => write!(f, "error"), |
| 17 | + Severity::Warning => write!(f, "warning"), |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/// A suggested fix for a diagnostic. |
| 23 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 24 | +pub struct Fix { |
| 25 | + pub(crate) replacement: String, |
| 26 | + pub(crate) description: String, |
| 27 | +} |
| 28 | + |
| 29 | +impl Fix { |
| 30 | + pub fn new(replacement: impl Into<String>, description: impl Into<String>) -> Self { |
| 31 | + Self { |
| 32 | + replacement: replacement.into(), |
| 33 | + description: description.into(), |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/// Related location information for a diagnostic. |
| 39 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 40 | +pub struct RelatedInfo { |
| 41 | + pub(crate) range: TextRange, |
| 42 | + pub(crate) message: String, |
| 43 | +} |
| 44 | + |
| 45 | +impl RelatedInfo { |
| 46 | + pub fn new(range: TextRange, message: impl Into<String>) -> Self { |
| 47 | + Self { |
| 48 | + range, |
| 49 | + message: message.into(), |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/// A diagnostic message with location, message, severity, and optional fix. |
| 55 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 56 | +pub(crate) struct DiagnosticMessage { |
| 57 | + pub(crate) severity: Severity, |
| 58 | + pub(crate) range: TextRange, |
| 59 | + pub(crate) message: String, |
| 60 | + pub(crate) fix: Option<Fix>, |
| 61 | + pub(crate) related: Vec<RelatedInfo>, |
| 62 | +} |
| 63 | + |
| 64 | +impl DiagnosticMessage { |
| 65 | + pub(crate) fn error(range: TextRange, message: impl Into<String>) -> Self { |
| 66 | + Self { |
| 67 | + severity: Severity::Error, |
| 68 | + range, |
| 69 | + message: message.into(), |
| 70 | + fix: None, |
| 71 | + related: Vec::new(), |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + pub(crate) fn warning(range: TextRange, message: impl Into<String>) -> Self { |
| 76 | + Self { |
| 77 | + severity: Severity::Warning, |
| 78 | + range, |
| 79 | + message: message.into(), |
| 80 | + fix: None, |
| 81 | + related: Vec::new(), |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + pub(crate) fn is_error(&self) -> bool { |
| 86 | + self.severity == Severity::Error |
| 87 | + } |
| 88 | + |
| 89 | + pub(crate) fn is_warning(&self) -> bool { |
| 90 | + self.severity == Severity::Warning |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl std::fmt::Display for DiagnosticMessage { |
| 95 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 96 | + write!( |
| 97 | + f, |
| 98 | + "{} at {}..{}: {}", |
| 99 | + self.severity, |
| 100 | + u32::from(self.range.start()), |
| 101 | + u32::from(self.range.end()), |
| 102 | + self.message |
| 103 | + )?; |
| 104 | + if let Some(fix) = &self.fix { |
| 105 | + write!(f, " (fix: {})", fix.description)?; |
| 106 | + } |
| 107 | + for related in &self.related { |
| 108 | + write!( |
| 109 | + f, |
| 110 | + " (related: {} at {}..{})", |
| 111 | + related.message, |
| 112 | + u32::from(related.range.start()), |
| 113 | + u32::from(related.range.end()) |
| 114 | + )?; |
| 115 | + } |
| 116 | + Ok(()) |
| 117 | + } |
| 118 | +} |
0 commit comments