forked from crate-ci/typos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.rs
207 lines (183 loc) · 4.83 KB
/
report.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#![allow(clippy::needless_update)]
use std::borrow::Cow;
pub trait Report: Send + Sync {
fn report(&self, msg: Message) -> Result<(), std::io::Error>;
}
#[derive(Clone, Debug, serde::Serialize, derive_more::From)]
#[serde(rename_all = "snake_case")]
#[serde(tag = "type")]
#[non_exhaustive]
pub enum Message<'m> {
BinaryFile(BinaryFile<'m>),
Typo(Typo<'m>),
File(File<'m>),
Parse(Parse<'m>),
Error(Error<'m>),
}
impl<'m> Message<'m> {
pub fn is_correction(&self) -> bool {
match self {
Message::BinaryFile(_) => false,
Message::Typo(c) => c.corrections.is_correction(),
Message::File(_) => false,
Message::Parse(_) => false,
Message::Error(_) => false,
}
}
pub fn is_error(&self) -> bool {
match self {
Message::BinaryFile(_) => false,
Message::Typo(_) => false,
Message::File(_) => false,
Message::Parse(_) => false,
Message::Error(_) => true,
}
}
pub fn context(self, context: Option<Context<'m>>) -> Self {
match self {
Message::Typo(typo) => {
let typo = typo.context(context);
Message::Typo(typo)
}
Message::Parse(parse) => {
let parse = parse.context(context);
Message::Parse(parse)
}
Message::Error(error) => {
let error = error.context(context);
Message::Error(error)
}
_ => self,
}
}
}
#[derive(Clone, Debug, serde::Serialize, derive_more::Display, derive_setters::Setters)]
#[display(fmt = "Skipping binary file {}", "path.display()")]
#[non_exhaustive]
pub struct BinaryFile<'m> {
pub path: &'m std::path::Path,
}
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
#[non_exhaustive]
pub struct Typo<'m> {
#[serde(flatten)]
pub context: Option<Context<'m>>,
#[serde(skip)]
pub buffer: Cow<'m, [u8]>,
pub byte_offset: usize,
pub typo: &'m str,
pub corrections: typos::Status<'m>,
}
impl<'m> Default for Typo<'m> {
fn default() -> Self {
Self {
context: None,
buffer: Cow::Borrowed(&[]),
byte_offset: 0,
typo: "",
corrections: typos::Status::Invalid,
}
}
}
#[derive(Clone, Debug, serde::Serialize, derive_more::From)]
#[serde(untagged)]
#[non_exhaustive]
pub enum Context<'m> {
File(FileContext<'m>),
Path(PathContext<'m>),
}
impl<'m> std::fmt::Display for Context<'m> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
match self {
Context::File(c) => write!(f, "{}:{}", c.path.display(), c.line_num),
Context::Path(c) => write!(f, "{}", c.path.display()),
}
}
}
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
#[non_exhaustive]
pub struct FileContext<'m> {
pub path: &'m std::path::Path,
pub line_num: usize,
}
impl<'m> Default for FileContext<'m> {
fn default() -> Self {
Self {
path: std::path::Path::new("-"),
line_num: 0,
}
}
}
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
#[non_exhaustive]
pub struct PathContext<'m> {
pub path: &'m std::path::Path,
}
impl<'m> Default for PathContext<'m> {
fn default() -> Self {
Self {
path: std::path::Path::new("-"),
}
}
}
#[derive(Copy, Clone, Debug, serde::Serialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ParseKind {
Identifier,
Word,
}
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
#[non_exhaustive]
pub struct File<'m> {
pub path: &'m std::path::Path,
}
impl<'m> File<'m> {
pub fn new(path: &'m std::path::Path) -> Self {
Self { path }
}
}
impl<'m> Default for File<'m> {
fn default() -> Self {
Self {
path: std::path::Path::new("-"),
}
}
}
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
#[non_exhaustive]
pub struct Parse<'m> {
#[serde(flatten)]
pub context: Option<Context<'m>>,
pub kind: ParseKind,
pub data: &'m str,
}
impl<'m> Default for Parse<'m> {
fn default() -> Self {
Self {
context: None,
kind: ParseKind::Identifier,
data: "",
}
}
}
#[derive(Clone, Debug, serde::Serialize, derive_setters::Setters)]
#[non_exhaustive]
pub struct Error<'m> {
#[serde(flatten)]
pub context: Option<Context<'m>>,
pub msg: String,
}
impl<'m> Error<'m> {
pub fn new(msg: String) -> Self {
Self { context: None, msg }
}
}
impl<'m> Default for Error<'m> {
fn default() -> Self {
Self {
context: None,
msg: "".to_owned(),
}
}
}