|
1 |
| -use std::fmt::Display; |
2 |
| - |
3 |
| -use is_macro::Is; |
4 | 1 | use serde::{Deserialize, Serialize};
|
5 | 2 |
|
6 | 3 | use crate::Span;
|
7 | 4 |
|
| 5 | +use super::{LintKind, Suggestion}; |
| 6 | + |
8 | 7 | /// An error found in text.
|
9 | 8 | #[derive(Debug, Clone, Serialize, Deserialize)]
|
10 | 9 | pub struct Lint {
|
@@ -37,150 +36,3 @@ impl Default for Lint {
|
37 | 36 | }
|
38 | 37 | }
|
39 | 38 | }
|
40 |
| - |
41 |
| -/// The general category a [`Lint`] falls into. |
42 |
| -/// There's no reason not to add a new item here if you are adding a new rule that doesn't fit |
43 |
| -/// the existing categories. |
44 |
| -#[derive(Debug, Clone, Copy, Serialize, Deserialize, Is, Default)] |
45 |
| -pub enum LintKind { |
46 |
| - Spelling, |
47 |
| - Capitalization, |
48 |
| - Style, |
49 |
| - Formatting, |
50 |
| - Repetition, |
51 |
| - Enhancement, |
52 |
| - Readability, |
53 |
| - WordChoice, |
54 |
| - #[default] |
55 |
| - Miscellaneous, |
56 |
| -} |
57 |
| - |
58 |
| -impl Display for LintKind { |
59 |
| - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
60 |
| - let s = match self { |
61 |
| - LintKind::Spelling => "Spelling", |
62 |
| - LintKind::Capitalization => "Capitalization", |
63 |
| - LintKind::Formatting => "Formatting", |
64 |
| - LintKind::Repetition => "Repetition", |
65 |
| - LintKind::Readability => "Readability", |
66 |
| - LintKind::Miscellaneous => "Miscellaneous", |
67 |
| - LintKind::Enhancement => "Enhancement", |
68 |
| - LintKind::WordChoice => "Word Choice", |
69 |
| - LintKind::Style => "Style", |
70 |
| - }; |
71 |
| - |
72 |
| - write!(f, "{}", s) |
73 |
| - } |
74 |
| -} |
75 |
| - |
76 |
| -/// A suggested edit that could resolve a [`Lint`]. |
77 |
| -#[derive(Debug, Clone, Serialize, Deserialize, Is, PartialEq, Eq)] |
78 |
| -pub enum Suggestion { |
79 |
| - /// Replace the offending text with a specific character sequence. |
80 |
| - ReplaceWith(Vec<char>), |
81 |
| - /// Insert the provided characters _after_ the offending text. |
82 |
| - InsertAfter(Vec<char>), |
83 |
| - /// Remove the offending text. |
84 |
| - Remove, |
85 |
| -} |
86 |
| - |
87 |
| -impl Suggestion { |
88 |
| - /// Variant of [`Self::replace_with_match_case`] that accepts a static string. |
89 |
| - pub fn replace_with_match_case_str(value: &'static str, template: &[char]) -> Self { |
90 |
| - Self::replace_with_match_case(value.chars().collect(), template) |
91 |
| - } |
92 |
| - |
93 |
| - /// Construct an instance of [`Self::ReplaceWith`], but make the content match the case of the |
94 |
| - /// provided template. |
95 |
| - /// |
96 |
| - /// For example, if we want to replace "You're" with "You are", we can provide "you are" and |
97 |
| - /// "You're". |
98 |
| - pub fn replace_with_match_case(mut value: Vec<char>, template: &[char]) -> Self { |
99 |
| - for (v, t) in value.iter_mut().zip(template.iter()) { |
100 |
| - if v.is_ascii_uppercase() != t.is_ascii_uppercase() { |
101 |
| - if t.is_uppercase() { |
102 |
| - *v = v.to_ascii_uppercase(); |
103 |
| - } else { |
104 |
| - *v = v.to_ascii_lowercase(); |
105 |
| - } |
106 |
| - } |
107 |
| - } |
108 |
| - |
109 |
| - Self::ReplaceWith(value) |
110 |
| - } |
111 |
| - |
112 |
| - /// Apply a suggestion to a given text. |
113 |
| - pub fn apply(&self, span: Span, source: &mut Vec<char>) { |
114 |
| - match self { |
115 |
| - Self::ReplaceWith(chars) => { |
116 |
| - // Avoid allocation if possible |
117 |
| - if chars.len() == span.len() { |
118 |
| - for (index, c) in chars.iter().enumerate() { |
119 |
| - source[index + span.start] = *c |
120 |
| - } |
121 |
| - } else { |
122 |
| - let popped = source.split_off(span.start); |
123 |
| - |
124 |
| - source.extend(chars); |
125 |
| - source.extend(popped.into_iter().skip(span.len())); |
126 |
| - } |
127 |
| - } |
128 |
| - Self::Remove => { |
129 |
| - for i in span.end..source.len() { |
130 |
| - source[i - span.len()] = source[i]; |
131 |
| - } |
132 |
| - |
133 |
| - source.truncate(source.len() - span.len()); |
134 |
| - } |
135 |
| - Self::InsertAfter(chars) => { |
136 |
| - let popped = source.split_off(span.end); |
137 |
| - source.extend(chars); |
138 |
| - source.extend(popped); |
139 |
| - } |
140 |
| - } |
141 |
| - } |
142 |
| -} |
143 |
| - |
144 |
| -impl Display for Suggestion { |
145 |
| - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
146 |
| - match self { |
147 |
| - Suggestion::ReplaceWith(with) => { |
148 |
| - write!(f, "Replace with: “{}”", with.iter().collect::<String>()) |
149 |
| - } |
150 |
| - Suggestion::InsertAfter(with) => { |
151 |
| - write!(f, "Insert “{}”", with.iter().collect::<String>()) |
152 |
| - } |
153 |
| - Suggestion::Remove => write!(f, "Remove error"), |
154 |
| - } |
155 |
| - } |
156 |
| -} |
157 |
| - |
158 |
| -#[cfg(test)] |
159 |
| -mod tests { |
160 |
| - use crate::Span; |
161 |
| - |
162 |
| - use super::Suggestion; |
163 |
| - |
164 |
| - #[test] |
165 |
| - fn insert_comma_after() { |
166 |
| - let source = "This is a test"; |
167 |
| - let mut source_chars = source.chars().collect(); |
168 |
| - let sug = Suggestion::InsertAfter(vec![',']); |
169 |
| - sug.apply(Span::new(0, 4), &mut source_chars); |
170 |
| - |
171 |
| - assert_eq!(source_chars, "This, is a test".chars().collect::<Vec<_>>()); |
172 |
| - } |
173 |
| - |
174 |
| - #[test] |
175 |
| - fn suggestion_your_match_case() { |
176 |
| - let template: Vec<_> = "You're".chars().collect(); |
177 |
| - let value: Vec<_> = "you are".chars().collect(); |
178 |
| - |
179 |
| - let correct = "You are".chars().collect(); |
180 |
| - |
181 |
| - assert_eq!( |
182 |
| - Suggestion::replace_with_match_case(value, &template), |
183 |
| - Suggestion::ReplaceWith(correct) |
184 |
| - ) |
185 |
| - } |
186 |
| -} |
0 commit comments