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
use std::path::Path;
use std::fs::File;
use std::io::{Read, self};

// Use 1MB buffer for reads
const CHUNK_SIZE: usize = 1024 *1024;

#[non_exhaustive]
pub struct Metadata{

}

pub struct ChunkIter{
    f: File,
    buffer: [u8; CHUNK_SIZE],
}

impl ChunkIter{
    pub fn new(f: File) -> Self{
        ChunkIter{f: f, buffer: [0;CHUNK_SIZE]}
    }
}

impl Iterator for ChunkIter{
    type Item = Result<Vec<u8>, io::Error>;
    fn next(&mut self) -> Option<Result<Vec<u8>, io::Error>>{
        match self.f.read(&mut self.buffer){
            Ok(i) => {
                if i == 0 {
                    return None
                } else {
                    Some(Ok(self.buffer[..i].to_vec()))}
                },
            Err(e) => Some(Err(e))
        }
    }
}

pub fn discombobulate<F: AsRef<Path>>(file: F, chunk_len: u64) -> Result<Box<dyn Iterator<Item = Result<Vec<u8>, io::Error>>>, io::Error>{
    match File::open(file){
        Err(e) => {return Err(e)}
        Ok(mut f) => {
            return Ok(Box::new(ChunkIter::new(f)))
        }
    }
}