gix_object/commit/message/
body.rs1use std::ops::Deref;
2
3use winnow::{
4 combinator::{eof, separated_pair, terminated},
5 error::ParserError,
6 prelude::*,
7 token::{rest, take_until},
8};
9
10use crate::{
11 bstr::{BStr, ByteSlice},
12 commit::message::BodyRef,
13};
14
15pub struct Trailers<'a> {
19 pub(crate) cursor: &'a [u8],
20}
21
22#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
24#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
25pub struct TrailerRef<'a> {
26 #[cfg_attr(feature = "serde", serde(borrow))]
28 pub token: &'a BStr,
29 pub value: &'a BStr,
32}
33
34fn parse_single_line_trailer<'a, E: ParserError<&'a [u8]>>(i: &mut &'a [u8]) -> ModalResult<(&'a BStr, &'a BStr), E> {
35 *i = i.trim_end();
36 let (token, value) = separated_pair(take_until(1.., b":".as_ref()), b": ", rest).parse_next(i)?;
37
38 if token.trim_end().len() != token.len() || value.trim_start().len() != value.len() {
39 Err(winnow::error::ErrMode::from_input(i).cut())
40 } else {
41 Ok((token.as_bstr(), value.as_bstr()))
42 }
43}
44
45impl<'a> Iterator for Trailers<'a> {
46 type Item = TrailerRef<'a>;
47
48 fn next(&mut self) -> Option<Self::Item> {
49 if self.cursor.is_empty() {
50 return None;
51 }
52 for mut line in self.cursor.lines_with_terminator() {
53 self.cursor = &self.cursor[line.len()..];
54 if let Some(trailer) = terminated(parse_single_line_trailer::<()>, eof)
55 .parse_next(&mut line)
56 .ok()
57 .map(|(token, value)| TrailerRef {
58 token: token.trim().as_bstr(),
59 value: value.trim().as_bstr(),
60 })
61 {
62 return Some(trailer);
63 }
64 }
65 None
66 }
67}
68
69impl<'a> BodyRef<'a> {
70 pub fn from_bytes(body: &'a [u8]) -> Self {
72 body.rfind(b"\n\n")
73 .map(|pos| (2, pos))
74 .or_else(|| body.rfind(b"\r\n\r\n").map(|pos| (4, pos)))
75 .and_then(|(sep_len, pos)| {
76 let trailer = &body[pos + sep_len..];
77 let body = &body[..pos];
78 Trailers { cursor: trailer }.next().map(|_| BodyRef {
79 body_without_trailer: body.as_bstr(),
80 start_of_trailer: trailer,
81 })
82 })
83 .unwrap_or_else(|| BodyRef {
84 body_without_trailer: body.as_bstr(),
85 start_of_trailer: &[],
86 })
87 }
88
89 pub fn without_trailer(&self) -> &'a BStr {
93 self.body_without_trailer
94 }
95
96 pub fn trailers(&self) -> Trailers<'a> {
98 Trailers {
99 cursor: self.start_of_trailer,
100 }
101 }
102}
103
104impl AsRef<BStr> for BodyRef<'_> {
105 fn as_ref(&self) -> &BStr {
106 self.body_without_trailer
107 }
108}
109
110impl Deref for BodyRef<'_> {
111 type Target = BStr;
112
113 fn deref(&self) -> &Self::Target {
114 self.body_without_trailer
115 }
116}
117
118impl TrailerRef<'_> {
120 pub fn is_signed_off_by(&self) -> bool {
122 self.token.eq_ignore_ascii_case(b"Signed-off-by")
123 }
124
125 pub fn is_co_authored_by(&self) -> bool {
127 self.token.eq_ignore_ascii_case(b"Co-authored-by")
128 }
129
130 pub fn is_acked_by(&self) -> bool {
132 self.token.eq_ignore_ascii_case(b"Acked-by")
133 }
134
135 pub fn is_reviewed_by(&self) -> bool {
137 self.token.eq_ignore_ascii_case(b"Reviewed-by")
138 }
139
140 pub fn is_tested_by(&self) -> bool {
142 self.token.eq_ignore_ascii_case(b"Tested-by")
143 }
144
145 pub fn is_attribution(&self) -> bool {
148 self.is_signed_off_by()
149 || self.is_co_authored_by()
150 || self.is_acked_by()
151 || self.is_reviewed_by()
152 || self.is_tested_by()
153 }
154}
155
156impl<'a> Trailers<'a> {
158 pub fn signed_off_by(self) -> impl Iterator<Item = TrailerRef<'a>> {
160 self.filter(TrailerRef::is_signed_off_by)
161 }
162
163 pub fn co_authored_by(self) -> impl Iterator<Item = TrailerRef<'a>> {
165 self.filter(TrailerRef::is_co_authored_by)
166 }
167
168 pub fn attributions(self) -> impl Iterator<Item = TrailerRef<'a>> {
171 self.filter(TrailerRef::is_attribution)
172 }
173
174 pub fn authors(self) -> impl Iterator<Item = TrailerRef<'a>> {
176 self.filter(|trailer| trailer.is_signed_off_by() || trailer.is_co_authored_by())
177 }
178}
179
180#[cfg(test)]
181mod test_parse_trailer {
182 use super::*;
183
184 fn parse(input: &str) -> (&BStr, &BStr) {
185 parse_single_line_trailer::<()>.parse_peek(input.as_bytes()).unwrap().1
186 }
187
188 #[test]
189 fn simple_newline() {
190 assert_eq!(parse("foo: bar\n"), ("foo".into(), "bar".into()));
191 }
192
193 #[test]
194 fn simple_non_ascii_no_newline() {
195 assert_eq!(parse("🤗: 🎉"), ("🤗".into(), "🎉".into()));
196 }
197
198 #[test]
199 fn with_lots_of_whitespace_newline() {
200 assert_eq!(
201 parse("hello foo: bar there \n"),
202 ("hello foo".into(), "bar there".into())
203 );
204 }
205
206 #[test]
207 fn extra_whitespace_before_token_or_value_is_error() {
208 assert!(parse_single_line_trailer::<()>.parse_peek(b"foo : bar").is_err());
209 assert!(parse_single_line_trailer::<()>.parse_peek(b"foo: bar").is_err());
210 }
211
212 #[test]
213 fn simple_newline_windows() {
214 assert_eq!(parse("foo: bar\r\n"), ("foo".into(), "bar".into()));
215 }
216}