show_my_errors/
annotation.rs1use super::{Error, Result};
2use std::{
3 fmt::{self, Display},
4 ops::Range,
5};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum Severity {
10 Info,
11 Warning,
12 Error,
13}
14
15impl Display for Severity {
16 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
17 match self {
18 Self::Info => f.write_str("info"),
19 Self::Warning => f.write_str("warning"),
20 Self::Error => f.write_str("error"),
21 }
22 }
23}
24
25#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct Annotation {
30 range: Range<usize>,
31 pub header: Option<String>,
33 pub text: Option<String>,
37 pub severity: Severity,
38}
39
40pub trait AnnotationText {
44 fn into_option_string(self) -> Option<String>;
45}
46
47impl AnnotationText for String {
48 fn into_option_string(self) -> Option<String> {
49 Some(self)
50 }
51}
52
53impl AnnotationText for &'_ str {
54 fn into_option_string(self) -> Option<String> {
55 Some(self.into())
56 }
57}
58
59impl AnnotationText for Option<String> {
60 fn into_option_string(self) -> Option<String> {
61 self
62 }
63}
64
65impl Annotation {
66 pub fn new(
83 range: Range<usize>,
84 severity: Severity,
85 header: impl AnnotationText,
86 text: impl AnnotationText,
87 ) -> Result<Self> {
88 if range.end < range.start {
89 Err(Error::InvalidRange(range.start, range.end))
90 } else {
91 Ok(Self {
92 range,
93 severity,
94 header: header.into_option_string(),
95 text: text.into_option_string(),
96 })
97 }
98 }
99
100 pub fn info(
102 range: Range<usize>,
103 header: impl AnnotationText,
104 text: impl AnnotationText,
105 ) -> Result<Self> {
106 Self::new(range, Severity::Info, header, text)
107 }
108
109 pub fn warning(
111 range: Range<usize>,
112 header: impl AnnotationText,
113 text: impl AnnotationText,
114 ) -> Result<Self> {
115 Self::new(range, Severity::Warning, header, text)
116 }
117
118 pub fn error(
120 range: Range<usize>,
121 header: impl AnnotationText,
122 text: impl AnnotationText,
123 ) -> Result<Self> {
124 Self::new(range, Severity::Error, header, text)
125 }
126
127 pub fn range(&self) -> &Range<usize> {
129 &self.range
130 }
131}