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
use std::convert::TryFrom;
use crate::{
immutable::{object::decode, parse::SPACE},
tree,
};
use bstr::{BStr, ByteSlice};
use nom::{
bytes::complete::{tag, take, take_while1, take_while_m_n},
character::is_digit,
combinator::all_consuming,
multi::many1,
sequence::terminated,
IResult,
};
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct Tree<'a> {
#[cfg_attr(feature = "serde1", serde(borrow))]
pub entries: Vec<Entry<'a>>,
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct TreeIter<'a> {
#[cfg_attr(feature = "serde1", serde(borrow))]
data: &'a [u8],
}
impl<'a> TreeIter<'a> {
pub fn from_bytes(data: &'a [u8]) -> TreeIter<'a> {
TreeIter { data }
}
}
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct Entry<'a> {
pub mode: tree::EntryMode,
pub filename: &'a BStr,
#[cfg_attr(feature = "serde1", serde(borrow))]
pub oid: &'a git_hash::oid,
}
impl<'a> Tree<'a> {
pub fn from_bytes(data: &'a [u8]) -> Result<Tree<'a>, decode::Error> {
parse(data).map(|(_, t)| t).map_err(decode::Error::from)
}
pub const fn empty() -> Tree<'static> {
Tree { entries: Vec::new() }
}
}
impl<'a> TreeIter<'a> {
pub fn entries(self) -> Result<Vec<Entry<'a>>, decode::Error> {
self.collect()
}
}
impl<'a> Default for TreeIter<'a> {
fn default() -> Self {
TreeIter { data: &[] }
}
}
impl<'a> Iterator for TreeIter<'a> {
type Item = Result<Entry<'a>, decode::Error>;
fn next(&mut self) -> Option<Self::Item> {
if self.data.is_empty() {
return None;
}
match parse_entry(self.data) {
Ok((data_left, entry)) => {
self.data = data_left;
Some(Ok(entry))
}
Err(err) => {
self.data = &[];
Some(Err(err.into()))
}
}
}
}
impl TryFrom<&[u8]> for tree::EntryMode {
type Error = decode::Error;
fn try_from(mode: &[u8]) -> Result<Self, Self::Error> {
Ok(match mode {
b"40000" => tree::EntryMode::Tree,
b"100644" => tree::EntryMode::Blob,
b"100664" => tree::EntryMode::Blob,
b"100640" => tree::EntryMode::Blob,
b"100755" => tree::EntryMode::BlobExecutable,
b"120000" => tree::EntryMode::Link,
b"160000" => tree::EntryMode::Commit,
_ => return Err(decode::Error::NomDetail(mode.into(), "unknown tree mode")),
})
}
}
const NULL: &[u8] = b"\0";
fn parse_entry(i: &[u8]) -> IResult<&[u8], Entry<'_>, decode::Error> {
let (i, mode) = terminated(take_while_m_n(5, 6, is_digit), tag(SPACE))(i)?;
let mode = tree::EntryMode::try_from(mode).map_err(nom::Err::Error)?;
let (i, filename) = terminated(take_while1(|b| b != NULL[0]), tag(NULL))(i)?;
let (i, oid) = take(20u8)(i)?;
Ok((
i,
Entry {
mode,
filename: filename.as_bstr(),
oid: git_hash::oid::try_from(oid).expect("we counted exactly 20 bytes"),
},
))
}
fn parse(i: &[u8]) -> IResult<&[u8], Tree<'_>, decode::Error> {
let (i, entries) = all_consuming(many1(parse_entry))(i)?;
Ok((i, Tree { entries }))
}