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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//! Coding standards templates
use super::{Template, TemplateCategory};
use crate::{Result, format::RuleCategory, parser::UnifiedRule};
/// Standards template definition
#[derive(Debug, Clone)]
pub struct StandardsTemplate {
name: String,
description: String,
rules: Vec<(RuleCategory, u8, String)>,
tags: Vec<String>,
}
impl StandardsTemplate {
/// Rust idiomatic patterns
pub fn rust_idioms() -> Self {
Self {
name: "rust-idioms".to_string(),
description: "Idiomatic Rust coding patterns".to_string(),
rules: vec![
(
RuleCategory::Naming,
0,
"Use snake_case for functions and variables".to_string(),
),
(
RuleCategory::Naming,
1,
"Use PascalCase for types and traits".to_string(),
),
(
RuleCategory::Naming,
2,
"Use SCREAMING_SNAKE_CASE for constants".to_string(),
),
(
RuleCategory::Style,
0,
"Prefer &str over String for function parameters".to_string(),
),
(
RuleCategory::Style,
1,
"Use iterators instead of manual loops when possible".to_string(),
),
(
RuleCategory::Style,
2,
"Prefer Option and Result over nulls and exceptions".to_string(),
),
(
RuleCategory::ErrorHandling,
0,
"Use ? operator for error propagation".to_string(),
),
(
RuleCategory::ErrorHandling,
1,
"Create custom error types for libraries".to_string(),
),
(
RuleCategory::Performance,
0,
"Prefer references over cloning when possible".to_string(),
),
(
RuleCategory::Performance,
1,
"Use Cow<str> for potentially owned strings".to_string(),
),
],
tags: vec![
"rust".to_string(),
"idioms".to_string(),
"patterns".to_string(),
],
}
}
/// Error handling standards
pub fn error_handling() -> Self {
Self {
name: "error-handling".to_string(),
description: "Error handling best practices".to_string(),
rules: vec![
(
RuleCategory::ErrorHandling,
0,
"Never silently swallow errors".to_string(),
),
(
RuleCategory::ErrorHandling,
1,
"Provide context with error messages".to_string(),
),
(
RuleCategory::ErrorHandling,
2,
"Use structured error types, not strings".to_string(),
),
(
RuleCategory::ErrorHandling,
3,
"Handle errors at the appropriate level".to_string(),
),
(
RuleCategory::ErrorHandling,
4,
"Log errors with sufficient context for debugging".to_string(),
),
(
RuleCategory::ErrorHandling,
5,
"Consider user experience when displaying errors".to_string(),
),
],
tags: vec![
"errors".to_string(),
"handling".to_string(),
"exceptions".to_string(),
],
}
}
/// Testing standards
pub fn testing() -> Self {
Self {
name: "testing".to_string(),
description: "Testing best practices".to_string(),
rules: vec![
(
RuleCategory::Testing,
0,
"Write tests for all public functions".to_string(),
),
(
RuleCategory::Testing,
1,
"Use descriptive test names that explain the scenario".to_string(),
),
(
RuleCategory::Testing,
2,
"Follow Arrange-Act-Assert pattern".to_string(),
),
(
RuleCategory::Testing,
3,
"Test edge cases and error conditions".to_string(),
),
(
RuleCategory::Testing,
4,
"Keep tests independent and isolated".to_string(),
),
(
RuleCategory::Testing,
5,
"Prefer integration tests for complex workflows".to_string(),
),
(
RuleCategory::Testing,
6,
"Mock external dependencies".to_string(),
),
],
tags: vec![
"testing".to_string(),
"tests".to_string(),
"quality".to_string(),
],
}
}
/// Documentation standards
pub fn documentation() -> Self {
Self {
name: "documentation".to_string(),
description: "Documentation best practices".to_string(),
rules: vec![
(
RuleCategory::Documentation,
0,
"Document all public APIs".to_string(),
),
(
RuleCategory::Documentation,
1,
"Include examples in documentation".to_string(),
),
(
RuleCategory::Documentation,
2,
"Document error conditions and panics".to_string(),
),
(
RuleCategory::Documentation,
3,
"Keep documentation up to date with code".to_string(),
),
(
RuleCategory::Documentation,
4,
"Use proper markdown formatting".to_string(),
),
(
RuleCategory::Documentation,
5,
"Link to related documentation".to_string(),
),
],
tags: vec![
"documentation".to_string(),
"docs".to_string(),
"comments".to_string(),
],
}
}
/// Git conventions
pub fn git_conventions() -> Self {
Self {
name: "git-conventions".to_string(),
description: "Git commit and branching conventions".to_string(),
rules: vec![
(
RuleCategory::Git,
0,
"Use conventional commit messages (feat:, fix:, docs:, etc.)".to_string(),
),
(
RuleCategory::Git,
1,
"Keep commits atomic and focused".to_string(),
),
(
RuleCategory::Git,
2,
"Write meaningful commit messages".to_string(),
),
(
RuleCategory::Git,
3,
"Reference issues in commits when applicable".to_string(),
),
(
RuleCategory::Git,
4,
"Use feature branches for development".to_string(),
),
(
RuleCategory::Git,
5,
"Rebase to keep history clean".to_string(),
),
],
tags: vec![
"git".to_string(),
"commits".to_string(),
"branching".to_string(),
],
}
}
}
impl Template for StandardsTemplate {
fn name(&self) -> &str {
&self.name
}
fn description(&self) -> &str {
&self.description
}
fn category(&self) -> TemplateCategory {
TemplateCategory::Standards
}
fn expand(&self) -> Result<Vec<UnifiedRule>> {
Ok(self
.rules
.iter()
.map(|(category, priority, description)| UnifiedRule::Standard {
category: *category,
priority: *priority,
description: description.clone(),
pattern: None,
})
.collect())
}
fn tags(&self) -> Vec<&str> {
self.tags.iter().map(|s| s.as_str()).collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_rust_idioms() {
let template = StandardsTemplate::rust_idioms();
assert_eq!(template.name(), "rust-idioms");
assert_eq!(template.category(), TemplateCategory::Standards);
let rules = template.expand().unwrap();
assert!(!rules.is_empty());
}
#[test]
fn test_all_standards() {
let standards = vec![
StandardsTemplate::rust_idioms(),
StandardsTemplate::error_handling(),
StandardsTemplate::testing(),
StandardsTemplate::documentation(),
StandardsTemplate::git_conventions(),
];
for standard in standards {
let rules = standard.expand().unwrap();
assert!(
!rules.is_empty(),
"Standard {} should have rules",
standard.name()
);
}
}
}