-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepo.rs
More file actions
230 lines (198 loc) · 6.37 KB
/
Copy pathrepo.rs
File metadata and controls
230 lines (198 loc) · 6.37 KB
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
use std::fs::File;
use std::io::{self, BufReader, BufWriter, Read, Write};
use std::path::Path;
use super::repo_header::RepoHeader;
use super::repo_record::RepoRecord;
/// Repository walker for CAR files.
pub struct Repo;
impl Repo {
/// Walks through a repository stream, calling callbacks for header and each record.
///
/// The header callback receives the RepoHeader and should return `true` to continue
/// processing or `false` to stop.
///
/// The record callback receives each RepoRecord and should return `true` to continue
/// processing or `false` to stop.
///
pub fn walk_repo<R, FH, FR>(
reader: R,
header_callback: FH,
record_callback: FR,
) -> io::Result<()>
where
R: Read,
FH: FnOnce(&RepoHeader) -> bool,
FR: FnMut(&RepoRecord) -> bool,
{
Self::walk_repo_inner(reader, header_callback, record_callback)
}
fn walk_repo_inner<R, FH, FR>(
reader: R,
header_callback: FH,
mut record_callback: FR,
) -> io::Result<()>
where
R: Read,
FH: FnOnce(&RepoHeader) -> bool,
FR: FnMut(&RepoRecord) -> bool,
{
let mut buf_reader = BufReader::new(reader);
// Read header
let repo_header = RepoHeader::read_from_stream(&mut buf_reader)?;
let keep_going = header_callback(&repo_header);
if !keep_going {
return Ok(());
}
// Read records until EOF
loop {
match RepoRecord::read_from_stream(&mut buf_reader) {
Ok(record) => {
let keep_going = record_callback(&record);
if !keep_going {
break;
}
}
Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => {
// Normal end of file
break;
}
Err(e) => {
// Propagate other errors
return Err(e);
}
}
}
Ok(())
}
/// Walks through a repository file, calling callbacks for header and each record.
pub fn walk_repo_file<P, FH, FR>(
path: P,
header_callback: FH,
record_callback: FR,
) -> io::Result<()>
where
P: AsRef<Path>,
FH: FnOnce(&RepoHeader) -> bool,
FR: FnMut(&RepoRecord) -> bool,
{
let file = File::open(path)?;
Self::walk_repo(file, header_callback, record_callback)
}
/// Returns an iterator over records in a repository file.
/// This is useful when you want to process records lazily.
pub fn iter_records<R: Read>(reader: R) -> io::Result<RepoIterator<R>> {
let mut buf_reader = BufReader::new(reader);
// Read header first
let header = RepoHeader::read_from_stream(&mut buf_reader)?;
Ok(RepoIterator {
reader: buf_reader,
header,
done: false,
})
}
///
/// Writes a repository to a stream.
///
pub fn write_repo<W: Write>(
writer: W,
header: &RepoHeader,
records: &[RepoRecord],
) -> io::Result<()> {
let mut buf_writer = BufWriter::new(writer);
// Write header
header.write_to_stream(&mut buf_writer)?;
// Write records
for record in records {
record.write_to_stream(&mut buf_writer)?;
}
buf_writer.flush()?;
Ok(())
}
/// Writes a repository to a file.
pub fn write_repo_file<P: AsRef<Path>>(
path: P,
header: &RepoHeader,
records: &[RepoRecord],
) -> io::Result<()> {
let file = File::create(path)?;
Self::write_repo(file, header, records)
}
/// Reads a repository from a stream into memory.
///
/// Returns the header and all records.
pub fn read_repo<R: Read>(reader: R) -> io::Result<(RepoHeader, Vec<RepoRecord>)> {
let mut buf_reader = BufReader::new(reader);
let header = RepoHeader::read_from_stream(&mut buf_reader)?;
let mut records = Vec::new();
loop {
match RepoRecord::read_from_stream(&mut buf_reader) {
Ok(record) => records.push(record),
Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => break,
Err(e) => return Err(e),
}
}
Ok((header, records))
}
/// Reads a repository from a file into memory.
pub fn read_repo_file<P: AsRef<Path>>(path: P) -> io::Result<(RepoHeader, Vec<RepoRecord>)> {
let file = File::open(path)?;
Self::read_repo(file)
}
/// Copies a repository from one stream to another.
/// This reads the repository, then writes it back out.
pub fn copy_repo<R: Read, W: Write>(reader: R, writer: W) -> io::Result<()> {
let (header, records) = Self::read_repo(reader)?;
Self::write_repo(writer, &header, &records)
}
/// Copies a repository file to another file.
pub fn copy_repo_file<P1: AsRef<Path>, P2: AsRef<Path>>(
input_path: P1,
output_path: P2,
) -> io::Result<()> {
let input_file = File::open(input_path)?;
let output_file = File::create(output_path)?;
Self::copy_repo(input_file, output_file)
}
}
/// An iterator over records in a repository.
pub struct RepoIterator<R: Read> {
reader: BufReader<R>,
header: RepoHeader,
done: bool,
}
impl<R: Read> RepoIterator<R> {
/// Returns a reference to the header.
pub fn header(&self) -> &RepoHeader {
&self.header
}
}
impl<R: Read> Iterator for RepoIterator<R> {
type Item = io::Result<RepoRecord>;
fn next(&mut self) -> Option<Self::Item> {
if self.done {
return None;
}
match RepoRecord::read_from_stream(&mut self.reader) {
Ok(record) => Some(Ok(record)),
Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => {
self.done = true;
None
}
Err(e) => {
self.done = true;
Some(Err(e))
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
// Note: Full integration tests would require a real CAR file
// These are placeholder tests for the API design
#[test]
fn test_repo_struct_exists() {
// Just verify the API compiles
let _repo = Repo;
}
}