commitlint_rs/rule/
scope_max_length.rs1use crate::{message::Message, result::Violation, rule::Rule};
2use serde::{Deserialize, Serialize};
3
4use super::Level;
5
6#[derive(Clone, Debug, Deserialize, Serialize)]
8#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
9pub struct ScopeMaxLength {
10 level: Option<Level>,
15
16 length: usize,
18}
19
20impl Rule for ScopeMaxLength {
22 const NAME: &'static str = "scope-max-length";
23 const LEVEL: Level = Level::Error;
24
25 fn message(&self, _message: &Message) -> String {
26 format!("scope is longer than {} characters", self.length)
27 }
28
29 fn validate(&self, message: &Message) -> Option<Violation> {
30 match &message.scope {
31 Some(scope) => {
32 if scope.len() >= self.length {
33 return Some(Violation {
34 level: self.level.unwrap_or(Self::LEVEL),
35 message: self.message(message),
36 });
37 }
38 }
39 None => {
40 return Some(Violation {
41 level: self.level.unwrap_or(Self::LEVEL),
42 message: self.message(message),
43 })
44 }
45 }
46
47 None
48 }
49}
50
51impl Default for ScopeMaxLength {
53 fn default() -> Self {
54 Self {
55 level: Some(Self::LEVEL),
56 length: 72,
57 }
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn test_long_scope() {
67 let rule = ScopeMaxLength {
68 length: usize::MAX, ..Default::default()
70 };
71 let message = Message {
72 body: None,
73 description: Some("desc".to_string()),
74 footers: None,
75 r#type: Some("feat".to_string()),
76 raw: "feat(scope): desc".to_string(),
77 scope: Some("scope".to_string()),
78 subject: Some("feat(scope): desc".to_string()),
79 };
80
81 assert!(rule.validate(&message).is_none());
82 }
83
84 #[test]
85 fn test_short_scope() {
86 let rule = ScopeMaxLength {
87 length: 3, ..Default::default()
89 };
90 let message = Message {
91 body: None,
92 description: Some("feat(scope): I'm long description".to_string()),
93 footers: None,
94 r#type: Some("feat".to_string()),
95 raw: "feat(scope): I'm long description".to_string(),
96 scope: Some("scope".to_string()),
97 subject: None,
98 };
99
100 let violation = rule.validate(&message);
101 assert!(violation.is_some());
102 assert_eq!(violation.clone().unwrap().level, Level::Error);
103 assert_eq!(
104 violation.unwrap().message,
105 format!("scope is longer than {} characters", rule.length)
106 );
107 }
108}