commitlint_rs/rule/
subject_empty.rs

1use crate::{message::Message, result::Violation, rule::Rule};
2use serde::{Deserialize, Serialize};
3
4use super::Level;
5
6/// SubjectEmpty represents the subject-empty rule.
7#[derive(Clone, Debug, Deserialize, Serialize)]
8#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
9pub struct SubjectEmpty {
10    /// Level represents the level of the rule.
11    ///
12    // Note that currently the default literal is not supported.
13    // See: https://github.com/serde-rs/serde/issues/368
14    level: Option<Level>,
15}
16
17/// SubjectEmpty represents the subject-empty rule.
18impl Rule for SubjectEmpty {
19    const NAME: &'static str = "subject-empty";
20    const LEVEL: Level = Level::Error;
21
22    fn message(&self, _message: &Message) -> String {
23        "subject is empty".to_string()
24    }
25
26    fn validate(&self, message: &Message) -> Option<Violation> {
27        if message.subject.is_none() {
28            return Some(Violation {
29                level: self.level.unwrap_or(Self::LEVEL),
30                message: self.message(message),
31            });
32        }
33
34        None
35    }
36}
37
38/// Default implementation of SubjectEmpty.
39impl Default for SubjectEmpty {
40    fn default() -> Self {
41        Self {
42            level: Some(Self::LEVEL),
43        }
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_non_empty_subject() {
53        let rule = SubjectEmpty::default();
54        let message = Message {
55            body: None,
56            description: Some("broadcast $destroy event on scope destruction".to_string()),
57            footers: None,
58            r#type: Some("feat".to_string()),
59            raw: "feat(scope): broadcast $destroy event on scope destruction
60
61Hello world"
62                .to_string(),
63            scope: Some("scope".to_string()),
64            subject: Some("feat(scope): broadcast $destroy event on scope destruction".to_string()),
65        };
66
67        assert!(rule.validate(&message).is_none());
68    }
69
70    #[test]
71    fn test_empty_description() {
72        let rule = SubjectEmpty::default();
73        let message = Message {
74            body: None,
75            description: None,
76            footers: None,
77            r#type: Some("feat".to_string()),
78            raw: "
79
80Hello world"
81                .to_string(),
82            scope: Some("scope".to_string()),
83            subject: None,
84        };
85
86        let violation = rule.validate(&message);
87        assert!(violation.is_some());
88        assert_eq!(violation.clone().unwrap().level, Level::Error);
89        assert_eq!(violation.unwrap().message, "subject is empty".to_string());
90    }
91}