summaryrefslogtreecommitdiffstats
path: root/vendor/gix-object/src/commit/message/decode.rs
blob: 6224909bdf1da9036541c20b2ad6bc97b7eb08de (plain)
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
use nom::{
    branch::alt,
    bytes::complete::{tag, take_till1},
    combinator::all_consuming,
    error::ParseError,
    sequence::pair,
    IResult,
};

use crate::bstr::{BStr, ByteSlice};

pub(crate) fn newline<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], &'a [u8], E> {
    alt((tag(b"\r\n"), tag(b"\n")))(i)
}

fn subject_and_body<'a, E: ParseError<&'a [u8]>>(i: &'a [u8]) -> IResult<&'a [u8], (&'a BStr, Option<&'a BStr>), E> {
    let mut c = i;
    let mut consumed_bytes = 0;
    while !c.is_empty() {
        c = match take_till1::<_, _, E>(|c| c == b'\n' || c == b'\r')(c) {
            Ok((i1, segment)) => {
                consumed_bytes += segment.len();
                match pair::<_, _, _, E, _, _>(newline, newline)(i1) {
                    Ok((body, _)) => {
                        return Ok((
                            &[],
                            (
                                i[0usize..consumed_bytes].as_bstr(),
                                (!body.is_empty()).then(|| body.as_bstr()),
                            ),
                        ));
                    }
                    Err(_) => match i1.get(1..) {
                        Some(next) => {
                            consumed_bytes += 1;
                            next
                        }
                        None => break,
                    },
                }
            }
            Err(_) => match c.get(1..) {
                Some(next) => {
                    consumed_bytes += 1;
                    next
                }
                None => break,
            },
        };
    }
    Ok((&[], (i.as_bstr(), None)))
}

/// Returns title and body, without separator
pub fn message(input: &[u8]) -> (&BStr, Option<&BStr>) {
    all_consuming(subject_and_body::<()>)(input).expect("cannot fail").1
}