gix_object/commit/message/
body.rs

1use std::ops::Deref;
2
3use winnow::{
4    combinator::{eof, rest, separated_pair, terminated},
5    error::{ErrorKind, ParserError},
6    prelude::*,
7    token::take_until,
8};
9
10use crate::{
11    bstr::{BStr, ByteSlice},
12    commit::message::BodyRef,
13};
14
15/// An iterator over trailers as parsed from a commit message body.
16///
17/// lines with parsing failures will be skipped
18pub struct Trailers<'a> {
19    pub(crate) cursor: &'a [u8],
20}
21
22/// A trailer as parsed from the commit message body.
23#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
24#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
25pub struct TrailerRef<'a> {
26    /// The name of the trailer, like "Signed-off-by", up to the separator ": "
27    #[cfg_attr(feature = "serde", serde(borrow))]
28    pub token: &'a BStr,
29    /// The value right after the separator ": ", with leading and trailing whitespace trimmed.
30    /// Note that multi-line values aren't currently supported.
31    pub value: &'a BStr,
32}
33
34fn parse_single_line_trailer<'a, E: ParserError<&'a [u8]>>(i: &mut &'a [u8]) -> PResult<(&'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_error_kind(i, ErrorKind::Fail).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    /// Parse `body` bytes into the trailer and the actual body.
71    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    /// Returns the body with the trailers stripped.
90    ///
91    /// You can iterate trailers with the [`trailers()`][BodyRef::trailers()] method.
92    pub fn without_trailer(&self) -> &'a BStr {
93        self.body_without_trailer
94    }
95
96    /// Return an iterator over the trailers parsed from the last paragraph of the body. May be empty.
97    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#[cfg(test)]
118mod test_parse_trailer {
119    use super::*;
120
121    fn parse(input: &str) -> (&BStr, &BStr) {
122        parse_single_line_trailer::<()>.parse_peek(input.as_bytes()).unwrap().1
123    }
124
125    #[test]
126    fn simple_newline() {
127        assert_eq!(parse("foo: bar\n"), ("foo".into(), "bar".into()));
128    }
129
130    #[test]
131    fn simple_non_ascii_no_newline() {
132        assert_eq!(parse("🤗: 🎉"), ("🤗".into(), "🎉".into()));
133    }
134
135    #[test]
136    fn with_lots_of_whitespace_newline() {
137        assert_eq!(
138            parse("hello foo: bar there   \n"),
139            ("hello foo".into(), "bar there".into())
140        );
141    }
142
143    #[test]
144    fn extra_whitespace_before_token_or_value_is_error() {
145        assert!(parse_single_line_trailer::<()>.parse_peek(b"foo : bar").is_err());
146        assert!(parse_single_line_trailer::<()>.parse_peek(b"foo:  bar").is_err());
147    }
148
149    #[test]
150    fn simple_newline_windows() {
151        assert_eq!(parse("foo: bar\r\n"), ("foo".into(), "bar".into()));
152    }
153}