solar_interface/diagnostics/
message.rs1use crate::Span;
4use std::{borrow::Cow, ops::Deref};
5
6#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub struct DiagMsg {
8 inner: Cow<'static, str>,
9}
10
11impl Deref for DiagMsg {
12 type Target = str;
13
14 #[inline]
15 fn deref(&self) -> &Self::Target {
16 &self.inner
17 }
18}
19
20impl From<&'static str> for DiagMsg {
21 fn from(value: &'static str) -> Self {
22 Self { inner: Cow::Borrowed(value) }
23 }
24}
25
26impl From<String> for DiagMsg {
27 fn from(value: String) -> Self {
28 Self { inner: Cow::Owned(value) }
29 }
30}
31
32impl From<Cow<'static, str>> for DiagMsg {
33 fn from(value: Cow<'static, str>) -> Self {
34 Self { inner: value }
35 }
36}
37
38impl DiagMsg {
39 #[inline]
41 pub fn as_str(&self) -> &str {
42 &self.inner
43 }
44
45 pub fn into_inner(self) -> Cow<'static, str> {
46 self.inner
47 }
48}
49
50#[derive(Clone, Debug)]
52pub struct SpanLabel {
53 pub span: Span,
55
56 pub is_primary: bool,
59
60 pub label: Option<DiagMsg>,
62}
63
64#[derive(Clone, Debug, PartialEq, Eq, Hash)]
72pub struct MultiSpan {
73 primary_spans: Vec<Span>,
74 span_labels: Vec<(Span, DiagMsg)>,
75}
76
77impl MultiSpan {
78 #[inline]
79 pub fn new() -> Self {
80 Self { primary_spans: vec![], span_labels: vec![] }
81 }
82
83 pub fn from_span(primary_span: Span) -> Self {
84 Self { primary_spans: vec![primary_span], span_labels: vec![] }
85 }
86
87 pub fn from_spans(mut vec: Vec<Span>) -> Self {
88 vec.sort_unstable();
89 Self { primary_spans: vec, span_labels: vec![] }
90 }
91
92 pub fn push_primary_span(&mut self, primary_span: Span) {
94 self.primary_spans.push(primary_span);
95 }
96
97 pub fn push_span_label(&mut self, span: Span, label: impl Into<DiagMsg>) {
98 self.span_labels.push((span, label.into()));
99 }
100
101 pub fn push_span_diag(&mut self, span: Span, diag: DiagMsg) {
103 self.span_labels.push((span, diag));
104 }
105
106 pub fn primary_span(&self) -> Option<Span> {
108 self.primary_spans.first().copied()
109 }
110
111 pub fn primary_spans(&self) -> &[Span] {
113 &self.primary_spans
114 }
115
116 pub fn has_primary_spans(&self) -> bool {
118 !self.is_dummy()
119 }
120
121 pub fn is_dummy(&self) -> bool {
123 self.primary_spans.iter().all(|sp| sp.is_dummy())
124 }
125
126 pub fn replace(&mut self, before: Span, after: Span) -> bool {
129 let mut replacements_occurred = false;
130 for primary_span in &mut self.primary_spans {
131 if *primary_span == before {
132 *primary_span = after;
133 replacements_occurred = true;
134 }
135 }
136 for span_label in &mut self.span_labels {
137 if span_label.0 == before {
138 span_label.0 = after;
139 replacements_occurred = true;
140 }
141 }
142 replacements_occurred
143 }
144
145 pub fn span_labels(&self) -> Vec<SpanLabel> {
151 let is_primary = |span| self.primary_spans.contains(&span);
152
153 let mut span_labels = self
154 .span_labels
155 .iter()
156 .map(|&(span, ref label)| SpanLabel {
157 span,
158 is_primary: is_primary(span),
159 label: Some(label.clone()),
160 })
161 .collect::<Vec<_>>();
162
163 for &span in &self.primary_spans {
164 if !span_labels.iter().any(|sl| sl.span == span) {
165 span_labels.push(SpanLabel { span, is_primary: true, label: None });
166 }
167 }
168
169 span_labels
170 }
171
172 pub fn span_labels_raw(&self) -> &[(Span, DiagMsg)] {
174 &self.span_labels
175 }
176
177 pub fn has_span_labels(&self) -> bool {
179 self.span_labels.iter().any(|(sp, _)| !sp.is_dummy())
180 }
181
182 pub fn clone_ignoring_labels(&self) -> Self {
184 Self { primary_spans: self.primary_spans.clone(), ..Self::new() }
185 }
186}
187
188impl Default for MultiSpan {
189 fn default() -> Self {
190 Self::new()
191 }
192}
193
194impl From<Span> for MultiSpan {
195 fn from(span: Span) -> Self {
196 Self::from_span(span)
197 }
198}
199
200impl From<Option<Span>> for MultiSpan {
201 fn from(span: Option<Span>) -> Self {
202 if let Some(span) = span { Self::from_span(span) } else { Self::new() }
203 }
204}
205
206impl From<Vec<Span>> for MultiSpan {
207 fn from(spans: Vec<Span>) -> Self {
208 Self::from_spans(spans)
209 }
210}