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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
#[derive(Clone, PartialEq, Eq, Hash)] pub struct Message<'c> { pub raw_subject: &'c str, pub commit_type: unicase::UniCase<&'c str>, pub scope: Option<unicase::UniCase<&'c str>>, pub important: bool, pub subject: &'c str, pub body: Option<&'c str>, pub trailer: Option<Vec<&'c str>>, #[doc(hidden)] __do_not_match_exhaustively: (), } impl<'c> Message<'c> { pub fn parse(commit: &'c str) -> Result<Self, failure::Error> { let commit = commit.trim(); let mut sections = split_sections(commit); let raw_subject = sections .next() .ok_or_else(|| failure::Context::new("Commit is empty"))?; let (commit_type, scope, important, subject) = parse_subject(raw_subject)?; let commit_type = unicase::UniCase::new(commit_type); let scope = scope.map(|s| unicase::UniCase::new(s)); let body = sections.next(); let trailer = sections.next().map(|s| s.lines().collect()); if let Some(section) = sections.next() { failure::bail!("Cannot have sections past body+trailer: ```{}```", section); } let c = Message { raw_subject, commit_type, scope, important, subject, body, trailer, __do_not_match_exhaustively: (), }; Ok(c) } } impl<'c> crate::style::Style for Message<'c> { fn subject(&self) -> &str { self.subject } } static SECTION_RE: once_cell::sync::Lazy<regex::Regex> = once_cell::sync::Lazy::new(|| regex::Regex::new("\r?\n\r?\n").unwrap()); fn split_sections(commit: &str) -> impl Iterator<Item = &str> { SECTION_RE.split(commit).map(|s| s.trim()) } #[cfg(test)] mod test_split_sections { use super::*; #[test] fn subject() { let actual: Vec<_> = split_sections("feat(parser): Parse bad greetings").collect(); let expected = ["feat(parser): Parse bad greetings"]; assert_eq!(actual, expected); } #[test] fn body() { let actual: Vec<_> = split_sections( r#"feat(parser): Parse bad greetings Hello World Foo Bar"#, ) .collect(); let expected = [ "feat(parser): Parse bad greetings", "Hello\nWorld", "Foo\nBar", ]; assert_eq!(actual, expected); } } static META_RE: once_cell::sync::Lazy<regex::Regex> = once_cell::sync::Lazy::new(|| regex::Regex::new(r#"^(.*?)(\(.*?\))?(!)?$"#).unwrap()); fn parse_subject(raw_subject: &str) -> Result<(&str, Option<&str>, bool, &str), failure::Error> { if raw_subject.contains("\n") { failure::bail!("Subject must be a single line"); } let mut parts = raw_subject.splitn(2, ":"); let meta = parts.next().unwrap(); let subject = parts .next() .ok_or_else(|| failure::Context::new("No commit metadata provided"))? .trim(); let captures = META_RE .captures(meta) .expect("Regex should match against everything"); let commit_type = captures .get(1) .expect("commit_type should match against everything") .as_str(); let scope = captures .get(2) .map(|m| m.as_str().trim_start_matches('(').trim_end_matches(')')); let important = captures.get(3).is_some(); if scope.is_none() { if commit_type.contains('(') { failure::bail!("Scope has unclosed '('"); } else if commit_type.contains(')') { failure::bail!("Scope is closed but never opened"); } } Ok((commit_type, scope, important, subject)) } #[cfg(test)] mod test_parse_subject { use super::*; #[test] fn basic() { let actual = parse_subject("feat: Parse bad greetings").unwrap(); let expected = ("feat", None, false, "Parse bad greetings"); assert_eq!(actual, expected); } #[test] fn with_scope() { let actual = parse_subject("feat(parser): Parse bad greetings").unwrap(); let expected = ("feat", Some("parser"), false, "Parse bad greetings"); assert_eq!(actual, expected); } #[test] fn with_important() { let actual = parse_subject("feat!: Parse bad greetings").unwrap(); let expected = ("feat", None, true, "Parse bad greetings"); assert_eq!(actual, expected); } #[test] fn with_scope_and_important() { let actual = parse_subject("feat(parser)!: Parse bad greetings").unwrap(); let expected = ("feat", Some("parser"), true, "Parse bad greetings"); assert_eq!(actual, expected); } #[test] fn error_without_metadata() { parse_subject("Parse bad greetings").unwrap_err(); } #[test] fn error_on_unclosed() { parse_subject("feat(parser: Parse bad greetings").unwrap_err(); } #[test] fn error_on_unopened() { parse_subject("featparser): Parse bad greetings").unwrap_err(); } }