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