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
use std::ops::Deref;
use nom::{
bytes::complete::{tag, take_until1},
combinator::all_consuming,
error::{ErrorKind, ParseError},
sequence::terminated,
IResult,
};
use crate::{
bstr::{BStr, ByteSlice},
commit::message::BodyRef,
};
pub struct Trailers<'a> {
pub(crate) cursor: &'a [u8],
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct TrailerRef<'a> {
#[cfg_attr(feature = "serde1", serde(borrow))]
pub token: &'a BStr,
pub value: &'a BStr,
}
fn parse_single_line_trailer<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&'a BStr, &'a BStr), E> {
let (value, token) = terminated(take_until1(b":".as_ref()), tag(b": "))(i.trim_end())?;
if token.trim_end().len() != token.len() || value.trim_start().len() != value.len() {
Err(nom::Err::Failure(E::from_error_kind(i, ErrorKind::Fail)))
} else {
Ok((&[], (token.as_bstr(), value.as_bstr())))
}
}
impl<'a> Iterator for Trailers<'a> {
type Item = TrailerRef<'a>;
fn next(&mut self) -> Option<Self::Item> {
if self.cursor.is_empty() {
return None;
}
for line in self.cursor.lines_with_terminator() {
self.cursor = &self.cursor[line.len()..];
if let Some(trailer) =
all_consuming(parse_single_line_trailer::<()>)(line)
.ok()
.map(|(_, (token, value))| TrailerRef {
token: token.trim().as_bstr(),
value: value.trim().as_bstr(),
})
{
return Some(trailer);
}
}
None
}
}
impl<'a> BodyRef<'a> {
pub fn from_bytes(body: &'a [u8]) -> Self {
body.rfind(b"\n\n")
.map(|pos| (2, pos))
.or_else(|| body.rfind(b"\r\n\r\n").map(|pos| (4, pos)))
.and_then(|(sep_len, pos)| {
let trailer = &body[pos + sep_len..];
let body = &body[..pos];
Trailers { cursor: trailer }.next().map(|_| BodyRef {
body_without_trailer: body.as_bstr(),
start_of_trailer: trailer,
})
})
.unwrap_or_else(|| BodyRef {
body_without_trailer: body.as_bstr(),
start_of_trailer: &[],
})
}
pub fn without_trailer(&self) -> &'a BStr {
self.body_without_trailer
}
pub fn trailers(&self) -> Trailers<'a> {
Trailers {
cursor: self.start_of_trailer,
}
}
}
impl<'a> AsRef<BStr> for BodyRef<'a> {
fn as_ref(&self) -> &BStr {
self.body_without_trailer
}
}
impl<'a> Deref for BodyRef<'a> {
type Target = BStr;
fn deref(&self) -> &Self::Target {
self.body_without_trailer
}
}
#[cfg(test)]
mod test_parse_trailer {
use super::*;
fn parse(input: &str) -> (&BStr, &BStr) {
parse_single_line_trailer::<()>(input.as_bytes()).unwrap().1
}
#[test]
fn simple_newline() {
assert_eq!(parse("foo: bar\n"), ("foo".into(), "bar".into()));
}
#[test]
fn simple_non_ascii_no_newline() {
assert_eq!(parse("🤗: 🎉"), ("🤗".into(), "🎉".into()));
}
#[test]
fn with_lots_of_whitespace_newline() {
assert_eq!(
parse("hello foo: bar there \n"),
("hello foo".into(), "bar there".into())
);
}
#[test]
fn extra_whitespace_before_token_or_value_is_error() {
assert!(parse_single_line_trailer::<()>(b"foo : bar").is_err());
assert!(parse_single_line_trailer::<()>(b"foo: bar").is_err())
}
#[test]
fn simple_newline_windows() {
assert_eq!(parse("foo: bar\r\n"), ("foo".into(), "bar".into()));
}
}