mit_lint/model/lint.rs
1use std::{
2 convert::{TryFrom, TryInto},
3 str::FromStr,
4 sync::LazyLock,
5};
6
7use miette::Diagnostic;
8use mit_commit::CommitMessage;
9use quickcheck::{Arbitrary, Gen};
10use strum_macros::EnumIter;
11use thiserror::Error;
12
13use crate::{
14 checks, model,
15 model::{Lints, Problem},
16};
17
18/// The lints that are supported
19#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash, Ord, PartialOrd, EnumIter)]
20pub enum Lint {
21 /// Check for duplicated trailers
22 ///
23 /// # Examples
24 ///
25 /// Passing
26 ///
27 /// ```rust
28 /// use mit_commit::CommitMessage;
29 /// use mit_lint::Lint;
30 ///
31 /// let message: &str = "An example commit
32 ///
33 /// This is an example commit without any duplicate trailers
34 /// "
35 /// .into();
36 /// let actual = Lint::DuplicatedTrailers.lint(&CommitMessage::from(message));
37 /// assert!(actual.is_none(), "Expected None, found {:?}", actual);
38 /// ```
39 ///
40 /// Erring
41 ///
42 /// ```rust
43 /// use mit_commit::CommitMessage;
44 /// use mit_lint::{Code, Lint, Problem};
45 ///
46 /// let message: &str = "An example commit
47 ///
48 /// This is an example commit without any duplicate trailers
49 ///
50 /// Signed-off-by: Billie Thompson <email@example.com>
51 /// Signed-off-by: Billie Thompson <email@example.com>
52 /// Co-authored-by: Billie Thompson <email@example.com>
53 /// Co-authored-by: Billie Thompson <email@example.com>
54 /// "
55 /// .into();
56 /// let expected = Some(Problem::new(
57 /// "Your commit message has duplicated trailers".into(),
58 /// "These are normally added accidentally when you\'re rebasing or amending to a \
59 /// commit, sometimes in the text editor, but often by git hooks.\n\nYou can fix \
60 /// this by deleting the duplicated \"Co-authored-by\", \"Signed-off-by\" fields"
61 /// .into(),
62 /// Code::DuplicatedTrailers,
63 /// &message.into(),
64 /// Some(vec![
65 /// ("Duplicated `Co-authored-by`".to_string(), 231, 51),
66 /// ("Duplicated `Signed-off-by`".to_string(), 128, 50),
67 /// ]),
68 /// Some(
69 /// "https://git-scm.com/docs/githooks#_commit_msg"
70 /// .parse()
71 /// .unwrap(),
72 /// ),
73 /// ));
74 /// let actual = Lint::DuplicatedTrailers.lint(&CommitMessage::from(message));
75 /// assert_eq!(
76 /// actual, expected,
77 /// "Expected {:?}, found {:?}",
78 /// expected, actual
79 /// );
80 /// ```
81 DuplicatedTrailers,
82 /// Check for a missing pivotal tracker id
83 ///
84 /// # Examples
85 ///
86 /// Passing
87 ///
88 /// ```rust
89 /// use mit_commit::CommitMessage;
90 /// use mit_lint::Lint;
91 ///
92 /// let message: &str = "An example commit [fixes #12345678]
93 /// "
94 /// .into();
95 /// let actual = Lint::PivotalTrackerIdMissing.lint(&CommitMessage::from(message));
96 /// assert!(actual.is_none(), "Expected None, found {:?}", actual);
97 /// ```
98 ///
99 /// Erring
100 ///
101 /// ```rust
102 /// use mit_commit::CommitMessage;
103 /// use mit_lint::{Code, Lint, Problem};
104 ///
105 /// let message: &str = "An example commit
106 ///
107 /// This is an example commit
108 /// "
109 ///
110 /// .into();
111 /// let expected = Some(Problem::new(
112 /// "Your commit message is missing a Pivotal Tracker ID".into(),
113 /// "It's important to add the ID because it allows code to be linked back to the stories it was done for, it can provide a chain of custody for code for audit purposes, and it can give future explorers of the codebase insight into the wider organisational need behind the change. We may also use it for automation purposes, like generating changelogs or notification emails.\n\nYou can fix this by adding the Id in one of the styles below to the commit message\n[Delivers #12345678]\n[fixes #12345678]\n[finishes #12345678]\n[#12345884 #12345678]\n[#12345884,#12345678]\n[#12345678],[#12345884]\nThis will address [#12345884]"
114 /// .into(),
115 /// Code::PivotalTrackerIdMissing,
116 /// &message.into(),
117 /// Some(vec![("No Pivotal Tracker ID".to_string(), 19, 25)]),
118 /// Some("https://www.pivotaltracker.com/help/api?version=v5#Tracker_Updates_in_SCM_Post_Commit_Hooks".parse().unwrap()),
119 /// ));
120 /// let actual = Lint::PivotalTrackerIdMissing.lint(&CommitMessage::from(message));
121 /// assert_eq!(
122 /// actual, expected,
123 /// "Expected {:?}, found {:?}",
124 /// expected, actual
125 /// );
126 /// ```
127 PivotalTrackerIdMissing,
128 /// Check for a missing jira issue key
129 ///
130 /// # Examples
131 ///
132 /// Passing
133 ///
134 /// ```rust
135 /// use mit_commit::CommitMessage;
136 /// use mit_lint::Lint;
137 ///
138 /// let message: &str = "An example commit
139 ///
140 /// Relates-to: JRA-123
141 /// "
142 /// .into();
143 /// let actual = Lint::JiraIssueKeyMissing.lint(&CommitMessage::from(message));
144 /// assert!(actual.is_none(), "Expected None, found {:?}", actual);
145 /// ```
146 ///
147 /// Erring
148 ///
149 /// ```rust
150 /// use mit_commit::CommitMessage;
151 /// use mit_lint::{Code, Lint, Problem};
152 ///
153 /// let message: &str = "An example commit
154 ///
155 /// This is an example commit
156 /// "
157 ///
158 /// .into();
159 /// let expected = Some(Problem::new(
160 /// "Your commit message is missing a JIRA Issue Key".into(),
161 /// "It's important to add the issue key because it allows us to link code back to the motivations for doing it, and in some cases provide an audit trail for compliance purposes.\n\nYou can fix this by adding a key like `JRA-123` to the commit message"
162 /// .into(),
163 /// Code::JiraIssueKeyMissing,&message.into(),
164 /// Some(vec![("No JIRA Issue Key".to_string(), 19, 25)]),
165 /// Some("https://support.atlassian.com/jira-software-cloud/docs/what-is-an-issue/#Workingwithissues-Projectkeys".parse().unwrap()),
166 /// ));
167 /// let actual = Lint::JiraIssueKeyMissing.lint(&CommitMessage::from(message));
168 /// assert_eq!(
169 /// actual, expected,
170 /// "Expected {:?}, found {:?}",
171 /// expected, actual
172 /// );
173 /// ```
174 JiraIssueKeyMissing,
175 /// Check for a missing github id
176 ///
177 /// # Examples
178 ///
179 /// Passing
180 ///
181 /// ```rust
182 /// use mit_commit::CommitMessage;
183 /// use mit_lint::Lint;
184 ///
185 /// let message: &str = "An example commit
186 ///
187 /// Relates-to: AnOrganisation/git-mit#642
188 /// "
189 /// .into();
190 /// let actual = Lint::GitHubIdMissing.lint(&CommitMessage::from(message));
191 /// assert!(actual.is_none(), "Expected None, found {:?}", actual);
192 /// ```
193 ///
194 /// Erring
195 ///
196 /// ```rust
197 /// use mit_commit::CommitMessage;
198 /// use mit_lint::{Code, Lint, Problem};
199 ///
200 /// let message: &str = "An example commit
201 ///
202 /// This is an example commit
203 /// "
204 ///
205 /// .into();
206 /// let expected = Some(Problem::new(
207 /// "Your commit message is missing a GitHub ID".into(),
208 /// "It's important to add the issue ID because it allows us to link code back to the motivations for doing it, and because we can help people exploring the repository link their issues to specific bits of code.\n\nYou can fix this by adding a ID like the following examples:\n\n#642\nGH-642\nAnUser/git-mit#642\nAnOrganisation/git-mit#642\nfixes #642\n\nBe careful just putting '#642' on a line by itself, as '#' is the default comment character"
209 /// .into(),
210 /// Code::GitHubIdMissing,&message.into(),Some(vec![("No GitHub ID".to_string(), 19, 25)]),
211 /// Some("https://docs.github.com/en/github/writing-on-github/working-with-advanced-formatting/autolinked-references-and-urls#issues-and-pull-requests".parse().unwrap()),
212 /// ));
213 /// let actual = Lint::GitHubIdMissing.lint(&CommitMessage::from(message));
214 /// assert_eq!(
215 /// actual, expected,
216 /// "Expected {:?}, found {:?}",
217 /// expected, actual
218 /// );
219 /// ```
220 GitHubIdMissing,
221 /// Subject being not being seperated from the body
222 ///
223 /// # Examples
224 ///
225 /// Passing
226 ///
227 /// ```rust
228 /// use mit_commit::CommitMessage;
229 /// use mit_lint::Lint;
230 ///
231 /// let message: &str = "An example commit
232 ///
233 /// Some Body Content
234 /// "
235 /// .into();
236 /// let actual = Lint::SubjectNotSeparateFromBody.lint(&CommitMessage::from(message));
237 /// assert!(actual.is_none(), "Expected None, found {:?}", actual);
238 /// ```
239 ///
240 /// Erring
241 ///
242 /// ```rust
243 /// use mit_commit::CommitMessage;
244 /// use mit_lint::{Code, Lint, Problem};
245 ///
246 /// let message: &str = "An example commit
247 /// This is an example commit
248 /// "
249 /// .into();
250 /// let expected = Some(Problem::new(
251 /// "Your commit message is missing a blank line between the subject and the body".into(),
252 /// "Most tools that render and parse commit messages, expect commit messages to be in the form of subject and body. This includes git itself in tools like git-format-patch. If you don't include this you may see strange behaviour from git and any related tools.\n\nTo fix this separate subject from body with a blank line"
253 /// .into(),
254 /// Code::SubjectNotSeparateFromBody,&message.into(),
255 /// Some(vec![("Missing blank line".to_string(), 18, 25)]),
256 /// Some("https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines".parse().unwrap()),
257 /// ));
258 /// let actual = Lint::SubjectNotSeparateFromBody.lint(&CommitMessage::from(message));
259 /// assert_eq!(
260 /// actual, expected,
261 /// "Expected {:?}, found {:?}",
262 /// expected, actual
263 /// );
264 /// ```
265 SubjectNotSeparateFromBody,
266 /// Check for a long subject line
267 ///
268 /// # Examples
269 ///
270 /// Passing
271 ///
272 /// ```rust
273 /// use mit_commit::CommitMessage;
274 /// use mit_lint::Lint;
275 ///
276 /// let message: &str = "An example commit
277 ///
278 /// Some Body Content
279 /// "
280 /// .into();
281 /// let actual = Lint::SubjectLongerThan72Characters.lint(&CommitMessage::from(message));
282 /// assert!(actual.is_none(), "Expected None, found {:?}", actual);
283 /// ```
284 ///
285 /// Erring
286 ///
287 /// ```
288 /// use mit_commit::CommitMessage;
289 /// use mit_lint::{Code, Lint, Problem};
290 ///
291 /// let message:String = "x".repeat(73).into();
292 /// let expected = Some(Problem::new(
293 /// "Your subject is longer than 72 characters".into(),
294 /// "It's important to keep the subject of the commit less than 72 characters because when you look at the git log, that's where it truncates the message. This means that people won't get the entirety of the information in your commit.\n\nPlease keep the subject line 72 characters or under"
295 /// .into(),
296 /// Code::SubjectLongerThan72Characters,&message.clone().into(),
297 /// Some(vec![("Too long".to_string(), 72, 1)]),
298 /// Some("https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines".parse().unwrap()),
299 /// ));
300 /// let actual = Lint::SubjectLongerThan72Characters.lint(&CommitMessage::from(message));
301 /// assert_eq!(
302 /// actual, expected,
303 /// "Expected {:?}, found {:?}",
304 /// expected, actual
305 /// );
306 /// ```
307 SubjectLongerThan72Characters,
308 /// Check for a non-capitalised subject
309 ///
310 /// # Examples
311 ///
312 /// Passing
313 ///
314 /// ```rust
315 /// use mit_commit::CommitMessage;
316 /// use mit_lint::Lint;
317 ///
318 /// let message: &str = "An example commit\n".into();
319 /// let actual = Lint::SubjectNotCapitalized.lint(&CommitMessage::from(message));
320 /// assert!(actual.is_none(), "Expected None, found {:?}", actual);
321 /// ```
322 ///
323 /// Erring
324 ///
325 /// ```rust
326 /// use mit_commit::CommitMessage;
327 /// use mit_lint::{Code, Lint, Problem};
328 ///
329 /// let message: &str =
330 /// "an example commit\n"
331 /// .into();
332 /// let expected = Some(
333 /// Problem::new(
334 /// "Your commit message is missing a capital letter".into(),
335 /// "The subject line is a title, and as such should be capitalised.\n\nYou can fix this by capitalising the first character in the subject".into(),
336 /// Code::SubjectNotCapitalized,&message.into(),
337 /// Some(vec![("Not capitalised".to_string(), 0, 1)]),
338 /// Some("https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines".parse().unwrap()),
339 /// )
340 /// );
341 /// let actual = Lint::SubjectNotCapitalized.lint(&CommitMessage::from(message));
342 /// assert_eq!(
343 /// actual, expected,
344 /// "Expected {:?}, found {:?}",
345 /// expected, actual
346 /// );
347 /// ```
348 SubjectNotCapitalized,
349 /// Check for period at the end of the subject
350 ///
351 /// # Examples
352 ///
353 /// Passing
354 ///
355 /// ```rust
356 /// use mit_commit::CommitMessage;
357 /// use mit_lint::Lint;
358 ///
359 /// let message: &str = "An example commit\n".into();
360 /// let actual = Lint::SubjectEndsWithPeriod.lint(&CommitMessage::from(message));
361 /// assert!(actual.is_none(), "Expected None, found {:?}", actual);
362 /// ```
363 ///
364 /// Erring
365 ///
366 /// ```rust
367 /// use mit_commit::CommitMessage;
368 /// use mit_lint::{Code, Lint, Problem};
369 ///
370 /// let message: &str =
371 /// "An example commit.\n".into();
372 /// let expected = Some(
373 /// Problem::new(
374 /// "Your commit message ends with a period".into(),
375 /// "It's important to keep your commits short, because we only have a limited number of characters to use (72) before the subject line is truncated. Full stops aren't normally in subject lines, and take up an extra character, so we shouldn't use them in commit message subjects.\n\nYou can fix this by removing the period"
376 /// .into(),
377 /// Code::SubjectEndsWithPeriod,&message.into(),
378 /// Some(vec![("Unneeded period".to_string(), 17, 1)]),
379 /// Some("https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines".parse().unwrap()),
380 /// )
381 /// );
382 /// let actual = Lint::SubjectEndsWithPeriod.lint(&CommitMessage::from(message));
383 /// assert_eq!(
384 /// actual, expected,
385 /// "Expected {:?}, found {:?}",
386 /// expected, actual
387 /// );
388 /// ```
389 SubjectEndsWithPeriod,
390 /// Check for a long body line
391 ///
392 /// # Examples
393 ///
394 /// Passing
395 ///
396 /// ```rust
397 /// use mit_commit::CommitMessage;
398 /// use mit_lint::Lint;
399 ///
400 /// let message: &str = "An example commit\n\nSome Body Content\n".into();
401 /// let actual = Lint::BodyWiderThan72Characters.lint(&CommitMessage::from(message));
402 /// assert!(actual.is_none(), "Expected None, found {:?}", actual);
403 /// ```
404 ///
405 /// Erring
406 ///
407 /// ```rust
408 /// use mit_commit::CommitMessage;
409 /// use mit_lint::{Code, Lint, Problem};
410 ///
411 /// let message:String = ["Subject".to_string(), "x".repeat(73).into()].join("\n\n");
412 /// let expected = Some(Problem::new(
413 /// "Your commit has a body wider than 72 characters".into(),
414 /// "It's important to keep the body of the commit narrower than 72 characters because when you look at the git log, that's where it truncates the message. This means that people won't get the entirety of the information in your commit.\n\nYou can fix this by making the lines in your body no more than 72 characters"
415 /// .into(),
416 /// Code::BodyWiderThan72Characters,&message.clone().into(),
417 /// Some(vec![("Too long".parse().unwrap(), 81, 1)]),
418 /// Some("https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines".parse().unwrap())
419 /// ));
420 /// let actual = Lint::BodyWiderThan72Characters.lint(&CommitMessage::from(message));
421 /// assert_eq!(
422 /// actual, expected,
423 /// "Expected {:?}, found {:?}",
424 /// expected, actual
425 /// );
426 /// ```
427 BodyWiderThan72Characters,
428 /// Check for commits following the conventional standard
429 ///
430 /// # Examples
431 ///
432 /// Passing
433 ///
434 /// ```rust
435 /// use mit_commit::CommitMessage;
436 /// use mit_lint::Lint;
437 ///
438 /// let message: &str = "refactor: An example commit\n\nSome Body Content\n".into();
439 /// let actual = Lint::NotConventionalCommit.lint(&CommitMessage::from(message));
440 /// assert!(actual.is_none(), "Expected None, found {:?}", actual);
441 /// ```
442 ///
443 /// Erring
444 ///
445 /// ```rust
446 /// use mit_commit::CommitMessage;
447 /// use mit_lint::{Code, Lint, Problem};
448 ///
449 /// let message: &str =
450 /// "An example commit\n\nSome Body Content\n"
451 /// .into();
452 /// let expected = Some(Problem::new(
453 /// "Your commit message isn't in conventional style".into(),
454 /// "It's important to follow the conventional commit style when creating your commit message. By using this style we can automatically calculate the version of software using deployment pipelines, and also generate changelogs and other useful information without human interaction.\n\nYou can fix it by following style\n\n<type>[optional scope]: <description>\n\n[optional body]\n\n[optional footer(s)]"
455 /// .into(),
456 /// Code::NotConventionalCommit,&message.into(),Some(vec![("Not conventional".to_string(), 0, 17)]),Some("https://www.conventionalcommits.org/".to_string()),
457 /// ));
458 /// let actual = Lint::NotConventionalCommit.lint(&CommitMessage::from(message));
459 /// assert_eq!(
460 /// actual, expected,
461 /// "Expected {:?}, found {:?}",
462 /// expected, actual
463 /// );
464 /// ```
465 NotConventionalCommit,
466 /// Check for commits following the emoji log standard
467 ///
468 /// # Examples
469 ///
470 /// Passing
471 ///
472 /// ```rust
473 /// use mit_commit::CommitMessage;
474 /// use mit_lint::Lint;
475 ///
476 /// let message: &str = "š DOC: An example commit\n\nSome Body Content\n".into();
477 /// let actual = Lint::NotEmojiLog.lint(&CommitMessage::from(message));
478 /// assert!(actual.is_none(), "Expected None, found {:?}", actual);
479 /// ```
480 ///
481 /// Erring
482 ///
483 /// ```rust
484 /// use mit_commit::CommitMessage;
485 /// use mit_lint::{Code, Lint, Problem};
486 ///
487 /// let message: &str =
488 /// "An example commit\n\nSome Body Content\n"
489 /// .into();
490 /// let expected = Some(
491 /// Problem::new(
492 /// "Your commit message isn't in emoji log style".into(),
493 /// "It's important to follow the emoji log style when creating your commit message. By using this style we can automatically generate changelogs.\n\nYou can fix it using one of the prefixes:\n\n\nš¦ NEW:\nš IMPROVE:\nš FIX:\nš DOC:\nš RELEASE:\nš¤ TEST:\nā¼\u{fe0f} BREAKING:"
494 /// .into(),
495 /// Code::NotEmojiLog,&message.into(),Some(vec![("Not emoji log".to_string(), 0, 17)]),Some("https://github.com/ahmadawais/Emoji-Log".to_string()),
496 /// ));
497 /// let actual = Lint::NotEmojiLog.lint(&CommitMessage::from(message));
498 /// assert_eq!(
499 /// actual, expected,
500 /// "Expected {:?}, found {:?}",
501 /// expected, actual
502 /// );
503 /// ```
504 NotEmojiLog,
505}
506
507/// The prefix we put in front of the lint when serialising
508pub const CONFIG_KEY_PREFIX: &str = "mit.lint";
509
510impl TryFrom<&str> for Lint {
511 type Error = Error;
512
513 fn try_from(from: &str) -> Result<Self, Self::Error> {
514 Self::all_lints()
515 .zip(Self::all_lints().map(|lint| format!("{lint}")))
516 .filter_map(|(lint, name): (Self, String)| if name == from { Some(lint) } else { None })
517 .collect::<Vec<Self>>()
518 .first()
519 .copied()
520 .ok_or_else(|| Error::new_lint_not_found(from))
521 }
522}
523
524impl From<Lint> for String {
525 fn from(from: Lint) -> Self {
526 format!("{from}")
527 }
528}
529
530impl From<Lint> for &str {
531 /// Get an lint's unique name
532 ///
533 /// # Examples
534 ///
535 /// ```
536 /// use mit_lint::Lint;
537 /// let actual: &str = Lint::NotConventionalCommit.into();
538 /// assert_eq!(actual, Lint::NotConventionalCommit.name());
539 /// ```
540 fn from(lint: Lint) -> Self {
541 lint.name()
542 }
543}
544
545impl Lint {
546 /// Get an lint's unique name
547 #[must_use]
548 pub const fn name(self) -> &'static str {
549 match self {
550 Self::DuplicatedTrailers => checks::duplicate_trailers::CONFIG,
551 Self::PivotalTrackerIdMissing => checks::missing_pivotal_tracker_id::CONFIG,
552 Self::JiraIssueKeyMissing => checks::missing_jira_issue_key::CONFIG,
553 Self::GitHubIdMissing => checks::missing_github_id::CONFIG,
554 Self::SubjectNotSeparateFromBody => checks::subject_not_separate_from_body::CONFIG,
555 Self::SubjectLongerThan72Characters => {
556 checks::subject_longer_than_72_characters::CONFIG
557 }
558 Self::SubjectNotCapitalized => checks::subject_not_capitalized::CONFIG,
559 Self::SubjectEndsWithPeriod => checks::subject_line_ends_with_period::CONFIG,
560 Self::BodyWiderThan72Characters => checks::body_wider_than_72_characters::CONFIG,
561 Self::NotConventionalCommit => checks::not_conventional_commit::CONFIG,
562 Self::NotEmojiLog => checks::not_emoji_log::CONFIG,
563 }
564 }
565}
566
567/// All the available lints
568static ALL_LINTS: LazyLock<[Lint; 11]> = LazyLock::new(|| {
569 [
570 Lint::DuplicatedTrailers,
571 Lint::PivotalTrackerIdMissing,
572 Lint::JiraIssueKeyMissing,
573 Lint::SubjectNotSeparateFromBody,
574 Lint::GitHubIdMissing,
575 Lint::SubjectLongerThan72Characters,
576 Lint::SubjectNotCapitalized,
577 Lint::SubjectEndsWithPeriod,
578 Lint::BodyWiderThan72Characters,
579 Lint::NotConventionalCommit,
580 Lint::NotEmojiLog,
581 ]
582});
583
584/// The ones that are enabled by default
585static DEFAULT_ENABLED_LINTS: LazyLock<[Lint; 4]> = LazyLock::new(|| {
586 [
587 Lint::DuplicatedTrailers,
588 Lint::SubjectNotSeparateFromBody,
589 Lint::SubjectLongerThan72Characters,
590 Lint::BodyWiderThan72Characters,
591 ]
592});
593
594impl Lint {
595 /// Iterator over all the lints
596 ///
597 /// # Examples
598 ///
599 /// ```rust
600 /// use mit_lint::Lint;
601 /// assert!(Lint::all_lints().next().is_some())
602 /// ```
603 pub fn all_lints() -> impl Iterator<Item = Self> {
604 ALL_LINTS.iter().copied()
605 }
606
607 /// Iterator over all the lints
608 ///
609 /// # Examples
610 ///
611 /// ```rust
612 /// use mit_lint::Lint;
613 /// assert!(Lint::iterator().next().is_some())
614 /// ```
615 #[deprecated(since = "0.1.5", note = "iterator was an unusual name. Use all_lints")]
616 pub fn iterator() -> impl Iterator<Item = Self> {
617 Self::all_lints()
618 }
619
620 /// Check if a lint is enabled by default
621 ///
622 /// # Examples
623 ///
624 /// ```rust
625 /// use mit_lint::Lint;
626 /// assert!(Lint::SubjectNotSeparateFromBody.enabled_by_default());
627 /// assert!(!Lint::NotConventionalCommit.enabled_by_default());
628 /// ```
629 #[must_use]
630 pub fn enabled_by_default(self) -> bool {
631 DEFAULT_ENABLED_LINTS.contains(&self)
632 }
633
634 /// Get a key suitable for a configuration document
635 ///
636 /// # Examples
637 ///
638 /// ```rust
639 /// use mit_lint::Lint;
640 /// assert_eq!(
641 /// Lint::SubjectNotSeparateFromBody.config_key(),
642 /// "mit.lint.subject-not-separated-from-body"
643 /// );
644 /// ```
645 #[must_use]
646 pub fn config_key(self) -> String {
647 format!("{CONFIG_KEY_PREFIX}.{self}")
648 }
649
650 /// Run this lint on a commit message
651 ///
652 /// # Examples
653 ///
654 /// ```rust
655 /// use mit_commit::CommitMessage;
656 /// use mit_lint::Lint;
657 /// let actual =
658 /// Lint::NotConventionalCommit.lint(&CommitMessage::from("An example commit message"));
659 /// assert!(actual.is_some());
660 /// ```
661 #[must_use]
662 pub fn lint(self, commit_message: &CommitMessage<'_>) -> Option<Problem> {
663 match self {
664 Self::DuplicatedTrailers => checks::duplicate_trailers::lint(commit_message),
665 Self::PivotalTrackerIdMissing => {
666 checks::missing_pivotal_tracker_id::lint(commit_message)
667 }
668 Self::JiraIssueKeyMissing => checks::missing_jira_issue_key::lint(commit_message),
669 Self::GitHubIdMissing => checks::missing_github_id::lint(commit_message),
670 Self::SubjectNotSeparateFromBody => {
671 checks::subject_not_separate_from_body::lint(commit_message)
672 }
673 Self::SubjectLongerThan72Characters => {
674 checks::subject_longer_than_72_characters::lint(commit_message)
675 }
676 Self::SubjectNotCapitalized => checks::subject_not_capitalized::lint(commit_message),
677 Self::SubjectEndsWithPeriod => {
678 checks::subject_line_ends_with_period::lint(commit_message)
679 }
680 Self::BodyWiderThan72Characters => {
681 checks::body_wider_than_72_characters::lint(commit_message)
682 }
683 Self::NotConventionalCommit => checks::not_conventional_commit::lint(commit_message),
684 Self::NotEmojiLog => checks::not_emoji_log::lint(commit_message),
685 }
686 }
687
688 /// Try and convert a list of names into lints
689 ///
690 /// # Examples
691 ///
692 /// ```rust
693 /// use mit_lint::Lint;
694 /// let actual = Lint::from_names(vec!["not-emoji-log", "body-wider-than-72-characters"]);
695 /// assert_eq!(
696 /// actual.unwrap(),
697 /// vec![Lint::BodyWiderThan72Characters, Lint::NotEmojiLog]
698 /// );
699 /// ```
700 ///
701 /// # Errors
702 /// If the lint does not exist
703 pub fn from_names(names: Vec<&str>) -> Result<Vec<Self>, model::lints::Error> {
704 let lints: Lints = names.try_into()?;
705 Ok(lints.into_iter().collect())
706 }
707}
708
709impl Arbitrary for Lint {
710 fn arbitrary(g: &mut Gen) -> Self {
711 *g.choose(&ALL_LINTS.iter().copied().collect::<Vec<_>>())
712 .unwrap()
713 }
714
715 fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
716 quickcheck::empty_shrinker()
717 }
718}
719
720impl std::fmt::Display for Lint {
721 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
722 write!(f, "{}", self.name())
723 }
724}
725
726impl FromStr for Lint {
727 type Err = Error;
728
729 fn from_str(s: &str) -> Result<Self, Self::Err> {
730 Self::try_from(s)
731 }
732}
733
734/// Errors
735#[derive(Error, Debug, Diagnostic)]
736pub enum Error {
737 /// Lint not found
738 #[error("Lint not found: {0}")]
739 #[diagnostic(
740 code(mit_lint::model::lint::error::LintNotFound),
741 url(docsrs),
742 help("check the list of available lints")
743 )]
744 LintNotFound(#[source_code] String, #[label("Not found")] (usize, usize)),
745}
746
747impl Error {
748 fn new_lint_not_found(missing_lint: &str) -> Self {
749 Self::LintNotFound(missing_lint.to_string(), (0, missing_lint.len()))
750 }
751}
752
753#[cfg(test)]
754mod tests {
755 use super::*;
756
757 use std::convert::TryInto;
758
759 #[quickcheck]
760 fn it_is_creatable_from_string(expected: Lint) -> bool {
761 let lint: String = expected.into();
762 expected == lint.parse().unwrap()
763 }
764
765 #[quickcheck]
766 fn it_is_convertible_to_string(expected: Lint) -> bool {
767 let lint: String = expected.into();
768 expected.name() == lint
769 }
770
771 #[quickcheck]
772 fn it_can_be_created_from_string(expected: Lint) -> bool {
773 let lint: Lint = expected.name().try_into().unwrap();
774 expected == lint
775 }
776
777 #[quickcheck]
778 fn it_is_printable(lint: Lint) -> bool {
779 lint.name() == format!("{lint}")
780 }
781
782 #[quickcheck]
783 fn i_can_get_all_the_lints(lint: Lint) -> bool {
784 Lint::all_lints().any(|x| x == lint)
785 }
786
787 #[test]
788 fn example_it_is_convertible_to_string() {
789 let string: String = Lint::PivotalTrackerIdMissing.into();
790 assert_eq!("pivotal-tracker-id-missing".to_string(), string);
791 }
792
793 #[test]
794 fn example_it_can_be_created_from_string() {
795 let lint: Lint = "pivotal-tracker-id-missing".try_into().unwrap();
796 assert_eq!(Lint::PivotalTrackerIdMissing, lint);
797 }
798
799 #[test]
800 fn example_it_is_printable() {
801 assert_eq!(
802 "pivotal-tracker-id-missing",
803 &format!("{}", Lint::PivotalTrackerIdMissing)
804 );
805 }
806
807 #[test]
808 fn example_i_can_get_all_the_lints() {
809 let all: Vec<Lint> = Lint::all_lints().collect();
810 assert_eq!(
811 all,
812 vec![
813 Lint::DuplicatedTrailers,
814 Lint::PivotalTrackerIdMissing,
815 Lint::JiraIssueKeyMissing,
816 Lint::SubjectNotSeparateFromBody,
817 Lint::GitHubIdMissing,
818 Lint::SubjectLongerThan72Characters,
819 Lint::SubjectNotCapitalized,
820 Lint::SubjectEndsWithPeriod,
821 Lint::BodyWiderThan72Characters,
822 Lint::NotConventionalCommit,
823 Lint::NotEmojiLog,
824 ]
825 );
826 }
827
828 #[test]
829 fn example_i_can_get_if_a_lint_is_enabled_by_default() {
830 assert!(Lint::DuplicatedTrailers.enabled_by_default());
831 assert!(!Lint::PivotalTrackerIdMissing.enabled_by_default());
832 assert!(!Lint::JiraIssueKeyMissing.enabled_by_default());
833 assert!(Lint::SubjectNotSeparateFromBody.enabled_by_default());
834 assert!(!Lint::GitHubIdMissing.enabled_by_default());
835 }
836}