forked from rust-lang/annotate-snippets-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippet.rs
186 lines (159 loc) · 4.39 KB
/
snippet.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//! Structures used as an input for the library.
//!
//! Example:
//!
//! ```
//! use annotate_snippets::*;
//!
//! Snippet::error("mismatched types")
//! .slice(Slice::new("Foo", 51).origin("src/format.rs"))
//! .slice(Slice::new("Faa", 129).origin("src/display.rs"));
//! ```
use std::ops::Range;
/// Primary structure provided for formatting
pub struct Snippet<'a> {
pub(crate) title: Label<'a>,
pub(crate) id: Option<&'a str>,
pub(crate) slices: Vec<Slice<'a>>,
pub(crate) footer: Vec<Label<'a>>,
}
impl<'a> Snippet<'a> {
pub fn title(title: Label<'a>) -> Self {
Self {
title,
id: None,
slices: vec![],
footer: vec![],
}
}
pub fn error(title: &'a str) -> Self {
Self::title(Label::error(title))
}
pub fn warning(title: &'a str) -> Self {
Self::title(Label::warning(title))
}
pub fn info(title: &'a str) -> Self {
Self::title(Label::info(title))
}
pub fn note(title: &'a str) -> Self {
Self::title(Label::note(title))
}
pub fn help(title: &'a str) -> Self {
Self::title(Label::help(title))
}
pub fn id(mut self, id: &'a str) -> Self {
self.id = Some(id);
self
}
pub fn slice(mut self, slice: Slice<'a>) -> Self {
self.slices.push(slice);
self
}
pub fn footer(mut self, footer: Label<'a>) -> Self {
self.footer.push(footer);
self
}
}
pub struct Label<'a> {
pub(crate) annotation_type: AnnotationType,
pub(crate) label: &'a str,
}
impl<'a> Label<'a> {
pub fn new(annotation_type: AnnotationType, label: &'a str) -> Self {
Self {
annotation_type,
label,
}
}
pub fn error(label: &'a str) -> Self {
Self::new(AnnotationType::Error, label)
}
pub fn warning(label: &'a str) -> Self {
Self::new(AnnotationType::Warning, label)
}
pub fn info(label: &'a str) -> Self {
Self::new(AnnotationType::Info, label)
}
pub fn note(label: &'a str) -> Self {
Self::new(AnnotationType::Note, label)
}
pub fn help(label: &'a str) -> Self {
Self::new(AnnotationType::Help, label)
}
pub fn label(mut self, label: &'a str) -> Self {
self.label = label;
self
}
/// Create a [`SourceAnnotation`] with the given span for a [`Slice`]
pub fn span(&self, span: Range<usize>) -> SourceAnnotation<'a> {
SourceAnnotation {
range: span,
label: self.label,
annotation_type: self.annotation_type,
}
}
}
impl From<AnnotationType> for Label<'_> {
fn from(annotation_type: AnnotationType) -> Self {
Label {
annotation_type,
label: "",
}
}
}
/// Structure containing the slice of text to be annotated and
/// basic information about the location of the slice.
///
/// One `Slice` is meant to represent a single, continuous,
/// slice of source code that you want to annotate.
pub struct Slice<'a> {
pub(crate) source: &'a str,
pub(crate) line_start: usize,
pub(crate) origin: Option<&'a str>,
pub(crate) annotations: Vec<SourceAnnotation<'a>>,
pub(crate) fold: bool,
}
impl<'a> Slice<'a> {
pub fn new(source: &'a str, line_start: usize) -> Self {
Self {
source,
line_start,
origin: None,
annotations: vec![],
fold: false,
}
}
pub fn origin(mut self, origin: &'a str) -> Self {
self.origin = Some(origin);
self
}
pub fn annotation(mut self, annotation: SourceAnnotation<'a>) -> Self {
self.annotations.push(annotation);
self
}
pub fn fold(mut self, fold: bool) -> Self {
self.fold = fold;
self
}
}
/// Types of annotations.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AnnotationType {
/// Error annotations are displayed using red color and "^" character.
Error,
/// Warning annotations are displayed using blue color and "-" character.
Warning,
Info,
Note,
Help,
}
/// An annotation for a [`Slice`].
///
/// This gets created by [`Label::span`].
#[derive(Debug)]
pub struct SourceAnnotation<'a> {
/// The byte range of the annotation in the `source` string
pub(crate) range: Range<usize>,
pub(crate) label: &'a str,
pub(crate) annotation_type: AnnotationType,
}