commitlint_rs/
message.rs

1use crate::{
2    config::Config,
3    git::{parse_commit_message, parse_subject},
4    result::Result as LintResult,
5};
6use std::{collections::HashMap, fmt::Error};
7
8/// Message represents a single commit message.
9///
10///
11/// ```code
12/// <type>[optional scope]: <description>
13///
14/// [optional body]
15///
16/// [optional footer(s)]
17/// ```
18///
19#[derive(Clone, Debug)]
20pub struct Message {
21    /// Body part of the commit message.
22    pub body: Option<String>,
23
24    /// Description part of the commit message.
25    pub description: Option<String>,
26    /// Footers part of the commit message.
27    pub footers: Option<HashMap<String, String>>,
28
29    #[allow(dead_code)]
30    /// Raw commit message (or any input from stdin) including the body and footers.
31    pub raw: String,
32
33    /// Type part of the commit message.
34    pub r#type: Option<String>,
35
36    /// Scope part of the commit message.
37    pub scope: Option<String>,
38
39    /// Subject part of the commit message.
40    pub subject: Option<String>,
41}
42
43/// Message represents a commit message.
44impl Message {
45    /// Create a new Message.
46    pub fn new(raw: String) -> Self {
47        let (subject, body, footers) = parse_commit_message(&raw);
48        let (r#type, scope, description) = parse_subject(&subject);
49        Self {
50            body,
51            description,
52            footers,
53            raw,
54            r#type,
55            scope,
56            subject: Some(subject),
57        }
58    }
59}
60
61/// validate the raw commit message.
62pub async fn validate(msg: &Message, config: &Config) -> Result<LintResult, Error> {
63    let violations = config.rules.validate(msg);
64    Ok(LintResult { violations })
65}