1use crate::checksum::Checksum;
8use crate::error::{Error, Result};
9use crate::objtype::ObjectType;
10use crate::varint;
11
12#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct SizeEntry {
25 pub checksum: Checksum,
27 pub compressed: u64,
29 pub unpacked: u64,
31 pub objtype: Option<ObjectType>,
35}
36
37pub fn pack_entry(entry: &SizeEntry, out: &mut Vec<u8>) {
39 out.extend_from_slice(entry.checksum.as_bytes());
40 varint::encode(entry.compressed, out);
41 varint::encode(entry.unpacked, out);
42 if let Some(ty) = entry.objtype {
43 out.push(ty.as_u32() as u8);
44 }
45}
46
47pub fn unpack_entry(buf: &[u8]) -> Result<SizeEntry> {
61 if buf.len() < 32 {
62 return Err(Error::InvalidSizeEntry("entry is shorter than a checksum"));
63 }
64 let checksum = Checksum::from_ay(&buf[..32])?;
65 let mut pos = 32;
66 let (compressed, n) = varint::decode(&buf[pos..])?;
67 pos += n;
68 let (unpacked, m) = varint::decode(&buf[pos..])?;
69 pos += m;
70 let objtype = match buf.len() - pos {
71 0 => None,
72 1 => Some(ObjectType::from_u32(u32::from(buf[pos]))?),
73 _ => return Err(Error::InvalidSizeEntry("entry has trailing bytes")),
74 };
75 Ok(SizeEntry {
76 checksum,
77 compressed,
78 unpacked,
79 objtype,
80 })
81}
82
83pub fn pack_sizes(mut entries: Vec<SizeEntry>) -> Vec<Vec<u8>> {
89 entries.sort_by_key(|a| a.checksum);
90 entries
91 .iter()
92 .map(|e| {
93 let mut buf = Vec::with_capacity(56);
95 pack_entry(e, &mut buf);
96 buf
97 })
98 .collect()
99}
100
101#[cfg(test)]
102mod tests {
103 use super::*;
104
105 fn csum(byte: u8) -> Checksum {
106 Checksum::from_bytes([byte; 32])
107 }
108
109 #[test]
110 fn entry_round_trips_with_objtype() {
111 let entry = SizeEntry {
112 checksum: csum(0xab),
113 compressed: 300,
114 unpacked: 16384,
115 objtype: Some(ObjectType::File),
116 };
117 let mut buf = Vec::new();
118 pack_entry(&entry, &mut buf);
119 assert_eq!(buf.len(), 32 + 2 + 3 + 1);
121 assert_eq!(unpack_entry(&buf).unwrap(), entry);
122 }
123
124 #[test]
125 fn tolerates_absent_objtype_byte() {
126 let entry = SizeEntry {
127 checksum: csum(0x01),
128 compressed: 1,
129 unpacked: 2,
130 objtype: None,
131 };
132 let mut buf = Vec::new();
133 pack_entry(&entry, &mut buf);
134 assert_eq!(buf.len(), 32 + 1 + 1);
135 assert_eq!(unpack_entry(&buf).unwrap().objtype, None);
136 }
137
138 #[test]
139 fn rejects_trailing_garbage() {
140 let mut buf = Vec::new();
141 pack_entry(
142 &SizeEntry {
143 checksum: csum(0x02),
144 compressed: 5,
145 unpacked: 6,
146 objtype: Some(ObjectType::File),
147 },
148 &mut buf,
149 );
150 buf.push(0x00);
151 assert!(matches!(
152 unpack_entry(&buf),
153 Err(Error::InvalidSizeEntry("entry has trailing bytes"))
154 ));
155 }
156
157 #[test]
158 fn pack_sizes_sorts_by_checksum() {
159 let entries = vec![
160 SizeEntry {
161 checksum: csum(0xff),
162 compressed: 1,
163 unpacked: 1,
164 objtype: Some(ObjectType::File),
165 },
166 SizeEntry {
167 checksum: csum(0x00),
168 compressed: 1,
169 unpacked: 1,
170 objtype: Some(ObjectType::File),
171 },
172 ];
173 let packed = pack_sizes(entries);
174 assert_eq!(&packed[0][..32], &[0x00u8; 32]);
176 assert_eq!(&packed[1][..32], &[0xffu8; 32]);
177 }
178}