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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//! Pack file reader/writer traits.
//!
//! # Architecture
//! Packfiles are Git's highly compressed archive format for storing multiple objects.
//! This module defines the contracts for both writing and reading packfiles, isolating
//! the complex delta-compression and indexing logic from the standard object store.
//!
//! # Design Rationale: Streaming I/O
//! Packfiles can contain thousands of objects and span gigabytes. The reader trait
//! returns a `Box<dyn Read>` rather than a `Vec<u8>`. This is a critical architectural
//! decision: it forces streaming deserialization. It allows the engine to resolve
//! deltas and decompress zlib streams on the fly, maintaining a constant memory
//! footprint regardless of the packfile's total size.
use crateVctrlError;
use Read;
/// Trait for writing Git pack files.
///
/// # Why this exists
/// Provides the contract for building a packfile. Packfiles are essential for
/// network transfers and repository garbage collection, as they compress objects
/// using delta encoding to save space. Abstracting this into a trait allows the
/// crate to support different compression levels or custom delta algorithms.
///
/// # How it works
/// The writer maintains internal state, tracking the offsets of each written object
/// to build a final index. As objects are written via `write_object`, the implementor
/// compresses the data and appends it to the underlying stream. The `finish` method
/// is required to flush any remaining buffers, write the packfile trailer, and
/// finalize the corresponding index file.
///
/// # Examples
///
/// Implementing the trait for a mock in-memory writer:
///
/// ```
/// # use libvctrl_handler::traits::core::pack::PackWriter;
/// # use libvctrl_handler::VctrlError;
/// # use std::collections::HashMap;
/// #
/// struct MockPackWriter {
/// objects: HashMap<Vec<u8>, Vec<u8>>,
/// }
///
/// impl PackWriter for MockPackWriter {
/// type ObjectId = Vec<u8>;
///
/// fn write_object(&mut self, id: &Self::ObjectId, data: &[u8]) -> Result<(), VctrlError> {
/// self.objects.insert(id.clone(), data.to_vec());
/// Ok(())
/// }
///
/// fn finish(&mut self) -> Result<(), VctrlError> {
/// // In a real impl, this would write the checksum and flush the stream.
/// Ok(())
/// }
/// }
///
/// let mut writer = MockPackWriter { objects: HashMap::new() };
/// writer.write_object(&vec![1, 2, 3], b"blob data")?;
/// writer.finish()?;
/// assert_eq!(writer.objects.len(), 1);
/// # Ok::<(), VctrlError>(())
/// ```
/// Trait for reading Git pack files.
///
/// # Why this exists
/// Provides the contract for random access reading of objects within a packfile.
/// By abstracting this, the crate allows backends to use memory-mapped files,
/// direct file I/O, or entirely in-memory representations for testing.
///
/// # Design Rationale: `&self` and Thread Safety
/// The trait requires `&self` for `read_object` (not `&mut self`). This is crucial
/// for concurrency. Packfiles are immutable once written. By taking an immutable
/// reference, multiple threads can safely read different objects from the same
/// packfile concurrently without requiring external locking.
///
/// # Examples
///
/// Implementing the trait for a mock in-memory reader:
///
/// ```
/// # use libvctrl_handler::traits::core::pack::PackReader;
/// # use libvctrl_handler::VctrlError;
/// # use std::collections::HashMap;
/// # use std::io::{Cursor, Read};
/// #
/// struct MockPackReader {
/// objects: HashMap<Vec<u8>, Vec<u8>>,
/// }
///
/// impl PackReader for MockPackReader {
/// type ObjectId = Vec<u8>;
///
/// fn read_object(&self, id: &Self::ObjectId) -> Result<Box<dyn Read + Send + '_>, VctrlError> {
/// let data = self.objects.get(id).cloned().unwrap_or_default();
/// Ok(Box::new(Cursor::new(data)))
/// }
/// }
///
/// let reader = MockPackReader { objects: HashMap::from([(vec![1], b"data".to_vec())]) };
/// let mut r = reader.read_object(&vec![1])?;
/// let mut buf = String::new();
/// r.read_to_string(&mut buf)?;
/// assert_eq!(buf, "data");
/// # Ok::<(), VctrlError>(())
/// ```