commitlint_rs/
message.rs

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
use crate::{
    config::Config,
    git::{parse_commit_message, parse_subject},
    result::Result as LintResult,
};
use std::{collections::HashMap, fmt::Error};

/// Message represents a single commit message.
///
///
/// ```code
/// <type>[optional scope]: <description>
///
/// [optional body]
///
/// [optional footer(s)]
/// ```
///
#[derive(Clone, Debug)]
pub struct Message {
    /// Body part of the commit message.
    pub body: Option<String>,

    /// Description part of the commit message.
    pub description: Option<String>,
    /// Footers part of the commit message.
    pub footers: Option<HashMap<String, String>>,

    #[allow(dead_code)]
    /// Raw commit message (or any input from stdin) including the body and footers.
    pub raw: String,

    /// Type part of the commit message.
    pub r#type: Option<String>,

    /// Scope part of the commit message.
    pub scope: Option<String>,

    /// Subject part of the commit message.
    pub subject: Option<String>,
}

/// Message represents a commit message.
impl Message {
    /// Create a new Message.
    pub fn new(raw: String) -> Self {
        let (subject, body, footers) = parse_commit_message(&raw);
        let (r#type, scope, description) = parse_subject(&subject);
        Self {
            body,
            description,
            footers,
            raw,
            r#type,
            scope,
            subject: Some(subject),
        }
    }
}

/// validate the raw commit message.
pub async fn validate(msg: &Message, config: &Config) -> Result<LintResult, Error> {
    let violations = config.rules.validate(msg);
    Ok(LintResult { violations })
}