mit_lint/cmd/
lint.rs

1use mit_commit::CommitMessage;
2
3use crate::model::{Lints, Problem};
4
5/// Lint a commit message
6///
7/// # Examples
8///
9/// ```rust
10/// use mit_commit::CommitMessage;
11/// use mit_lint::{lint, Lints};
12/// let actual = lint(
13///     &CommitMessage::from("An example commit message"),
14///     Lints::available(),
15/// );
16/// assert!(!actual.is_empty());
17/// ```
18///
19/// ```rust
20/// use mit_commit::CommitMessage;
21/// use mit_lint::{Code, lint, Problem, Lints, Lint};
22///
23/// let message:String = "x".repeat(73).into();
24/// let expected = vec![Problem::new(
25///     "Your subject is longer than 72 characters".into(),
26///     "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"
27///         .into(),
28///     Code::SubjectLongerThan72Characters,&message.clone().into(),Some(vec![("Too long".to_string(), 72, 1)]),
29///     Some("https://git-scm.com/book/en/v2/Distributed-Git-Contributing-to-a-Project#_commit_guidelines".parse().unwrap()),
30/// )];
31/// let lints = Lints::new(vec![Lint::SubjectLongerThan72Characters].into_iter().collect());
32/// let actual = lint(&CommitMessage::from(message), &lints);
33/// assert_eq!(
34///     actual, expected,
35///     "Expected {:?}, found {:?}",
36///     expected, actual
37/// );
38/// ```
39#[must_use]
40pub fn lint(commit_message: &CommitMessage<'_>, lints: &Lints) -> Vec<Problem> {
41    lints
42        .clone()
43        .into_iter()
44        .collect::<Vec<_>>()
45        .into_iter()
46        .filter_map(|lint| lint.lint(commit_message))
47        .collect::<Vec<Problem>>()
48}