commitlint_rs/rule/
body_empty.rs

1use crate::{message::Message, result::Violation, rule::Rule};
2use serde::{Deserialize, Serialize};
3
4use super::Level;
5
6/// BodyEmpty represents the body-empty rule.
7#[derive(Clone, Debug, Deserialize, Serialize)]
8#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
9pub struct BodyEmpty {
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/// BodyEmpty represents the body-empty rule.
18impl Rule for BodyEmpty {
19    const NAME: &'static str = "body-empty";
20    const LEVEL: Level = Level::Error;
21
22    fn message(&self, _message: &Message) -> String {
23        "body is empty".to_string()
24    }
25
26    fn validate(&self, message: &Message) -> Option<Violation> {
27        if message.body.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 BodyEmpty.
39impl Default for BodyEmpty {
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_body() {
53        let rule = BodyEmpty::default();
54        let message = Message {
55            body: Some("Hello world".to_string()),
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_body() {
72        let rule = BodyEmpty::default();
73        let message = Message {
74            body: None,
75            description: None,
76            footers: None,
77            r#type: Some("feat".to_string()),
78            raw: "feat(scope): broadcast $destroy event on scope destruction".to_string(),
79            scope: Some("scope".to_string()),
80            subject: None,
81        };
82
83        let violation = rule.validate(&message);
84        assert!(violation.is_some());
85        assert_eq!(violation.clone().unwrap().level, Level::Error);
86        assert_eq!(violation.unwrap().message, "body is empty".to_string());
87    }
88}