git_object/commit/message/
body.rs1use std::ops::Deref;
2
3use nom::{
4 bytes::complete::{tag, take_until1},
5 combinator::all_consuming,
6 error::{ErrorKind, ParseError},
7 sequence::terminated,
8 IResult,
9};
10
11use crate::{
12 bstr::{BStr, ByteSlice},
13 commit::message::BodyRef,
14};
15
16pub struct Trailers<'a> {
20 pub(crate) cursor: &'a [u8],
21}
22
23#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
25#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
26pub struct TrailerRef<'a> {
27 #[cfg_attr(feature = "serde1", serde(borrow))]
29 pub token: &'a BStr,
30 pub value: &'a BStr,
33}
34
35fn parse_single_line_trailer<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&'a BStr, &'a BStr), E> {
36 let (value, token) = terminated(take_until1(b":".as_ref()), tag(b": "))(i.trim_end())?;
37 if token.trim_end().len() != token.len() || value.trim_start().len() != value.len() {
38 Err(nom::Err::Failure(E::from_error_kind(i, ErrorKind::Fail)))
39 } else {
40 Ok((&[], (token.as_bstr(), value.as_bstr())))
41 }
42}
43
44impl<'a> Iterator for Trailers<'a> {
45 type Item = TrailerRef<'a>;
46
47 fn next(&mut self) -> Option<Self::Item> {
48 if self.cursor.is_empty() {
49 return None;
50 }
51 for line in self.cursor.lines_with_terminator() {
52 self.cursor = &self.cursor[line.len()..];
53 if let Some(trailer) =
54 all_consuming(parse_single_line_trailer::<()>)(line)
55 .ok()
56 .map(|(_, (token, value))| TrailerRef {
57 token: token.trim().as_bstr(),
58 value: value.trim().as_bstr(),
59 })
60 {
61 return Some(trailer);
62 }
63 }
64 None
65 }
66}
67
68impl<'a> BodyRef<'a> {
69 pub fn from_bytes(body: &'a [u8]) -> Self {
71 body.rfind(b"\n\n")
72 .map(|pos| (2, pos))
73 .or_else(|| body.rfind(b"\r\n\r\n").map(|pos| (4, pos)))
74 .and_then(|(sep_len, pos)| {
75 let trailer = &body[pos + sep_len..];
76 let body = &body[..pos];
77 Trailers { cursor: trailer }.next().map(|_| BodyRef {
78 body_without_trailer: body.as_bstr(),
79 start_of_trailer: trailer,
80 })
81 })
82 .unwrap_or_else(|| BodyRef {
83 body_without_trailer: body.as_bstr(),
84 start_of_trailer: &[],
85 })
86 }
87
88 pub fn without_trailer(&self) -> &'a BStr {
92 self.body_without_trailer
93 }
94
95 pub fn trailers(&self) -> Trailers<'a> {
97 Trailers {
98 cursor: self.start_of_trailer,
99 }
100 }
101}
102
103impl<'a> AsRef<BStr> for BodyRef<'a> {
104 fn as_ref(&self) -> &BStr {
105 self.body_without_trailer
106 }
107}
108
109impl<'a> Deref for BodyRef<'a> {
110 type Target = BStr;
111
112 fn deref(&self) -> &Self::Target {
113 self.body_without_trailer
114 }
115}
116#[cfg(test)]
117mod test_parse_trailer {
118 use super::*;
119
120 fn parse(input: &str) -> (&BStr, &BStr) {
121 parse_single_line_trailer::<()>(input.as_bytes()).unwrap().1
122 }
123
124 #[test]
125 fn simple_newline() {
126 assert_eq!(parse("foo: bar\n"), ("foo".into(), "bar".into()));
127 }
128
129 #[test]
130 fn simple_non_ascii_no_newline() {
131 assert_eq!(parse("🤗: 🎉"), ("🤗".into(), "🎉".into()));
132 }
133
134 #[test]
135 fn with_lots_of_whitespace_newline() {
136 assert_eq!(
137 parse("hello foo: bar there \n"),
138 ("hello foo".into(), "bar there".into())
139 );
140 }
141
142 #[test]
143 fn extra_whitespace_before_token_or_value_is_error() {
144 assert!(parse_single_line_trailer::<()>(b"foo : bar").is_err());
145 assert!(parse_single_line_trailer::<()>(b"foo: bar").is_err())
146 }
147
148 #[test]
149 fn simple_newline_windows() {
150 assert_eq!(parse("foo: bar\r\n"), ("foo".into(), "bar".into()));
151 }
152}