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
use std::{
collections::{BTreeMap, BTreeSet},
convert::TryFrom,
vec::IntoIter,
};
use miette::Diagnostic;
use thiserror::Error;
use crate::model::{lint, Lint};
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Lints {
lints: BTreeSet<Lint>,
}
lazy_static! {
static ref AVAILABLE: Lints = {
let set = Lint::all_lints().collect();
Lints::new(set)
};
}
impl Lints {
#[must_use]
pub const fn new(lints: BTreeSet<Lint>) -> Self {
Self { lints }
}
#[must_use]
pub fn available() -> &'static Self {
&AVAILABLE
}
#[must_use]
pub fn names(self) -> Vec<&'static str> {
self.lints.iter().map(|lint| lint.name()).collect()
}
#[must_use]
pub fn config_keys(self) -> Vec<String> {
self.lints.iter().map(|lint| lint.config_key()).collect()
}
#[must_use]
pub fn merge(&self, other: &Self) -> Self {
Self::new(self.lints.union(&other.lints).copied().collect())
}
#[must_use]
pub fn subtract(&self, other: &Self) -> Self {
Self::new(self.lints.difference(&other.lints).copied().collect())
}
}
impl std::iter::IntoIterator for Lints {
type IntoIter = IntoIter<Lint>;
type Item = Lint;
fn into_iter(self) -> Self::IntoIter {
self.lints.into_iter().collect::<Vec<_>>().into_iter()
}
}
impl TryFrom<Lints> for String {
type Error = Error;
fn try_from(lints: Lints) -> Result<Self, Self::Error> {
let enabled: Vec<_> = lints.into();
let config: BTreeMap<Self, bool> = Lint::all_lints()
.map(|x| (x, enabled.contains(&x)))
.fold(BTreeMap::new(), |mut acc, (lint, state)| {
acc.insert(lint.to_string(), state);
acc
});
let mut inner: BTreeMap<Self, BTreeMap<Self, bool>> = BTreeMap::new();
inner.insert("lint".into(), config);
let mut output: BTreeMap<Self, BTreeMap<Self, BTreeMap<Self, bool>>> = BTreeMap::new();
output.insert("mit".into(), inner);
Ok(toml::to_string(&output)?)
}
}
impl From<Vec<Lint>> for Lints {
fn from(lints: Vec<Lint>) -> Self {
Self::new(lints.into_iter().collect())
}
}
impl From<Lints> for Vec<Lint> {
fn from(lints: Lints) -> Self {
lints.into_iter().collect()
}
}
impl TryFrom<Vec<&str>> for Lints {
type Error = Error;
fn try_from(value: Vec<&str>) -> Result<Self, Self::Error> {
let lints = value
.into_iter()
.try_fold(
vec![],
|lints: Vec<Lint>, item_name| -> Result<Vec<Lint>, Error> {
let lint = Lint::try_from(item_name)?;
Ok(vec![lints, vec![lint]].concat())
},
)
.map(Vec::into_iter)?;
Ok(Self::new(lints.collect()))
}
}
#[derive(Error, Debug, Diagnostic)]
pub enum Error {
#[error(transparent)]
#[diagnostic(transparent)]
LintNameUnknown(#[from] lint::Error),
#[error("Failed to parse lint config file: {0}")]
#[diagnostic(
code(mit_lint::model::lints::error::toml_parse),
url(docsrs),
help("is it valid toml?")
)]
TomlParse(#[from] toml::de::Error),
#[error("Failed to convert config to toml: {0}")]
#[diagnostic(code(mit_lint::model::lints::error::toml_serialize), url(docsrs))]
TomlSerialize(#[from] toml::ser::Error),
}