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
use std::{
    fs::File,
    io::{self, Read},
    ops::Deref,
    path::{Path, PathBuf},
};
#[derive(Default)]
pub struct SeqReader<R> {
    readers: Vec<R>,
    current: usize,
}

impl<R> From<Vec<R>> for SeqReader<R> {
    fn from(value: Vec<R>) -> Self {
        Self::new(value)
    }
}

impl<R> Deref for SeqReader<R> {
    type Target = [R];

    fn deref(&self) -> &Self::Target {
        &self.readers
    }
}

impl<R> AsRef<[R]> for SeqReader<R> {
    fn as_ref(&self) -> &[R] {
        &self.readers
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl SeqReader<File> {
    pub fn from_path_iter<'a>(paths: impl Iterator<Item = &'a Path>) -> std::io::Result<Self> {
        let mut readers = Vec::new();
        for path in paths {
            readers.push(File::open(path)?);
        }
        Ok(Self::new(readers))
    }
}

impl<R> SeqReader<R> {
    pub fn new(readers: Vec<R>) -> Self {
        Self {
            readers,
            current: 0,
        }
    }

    pub fn reader_len(&self) -> usize {
        self.readers.len()
    }
}

impl<R: Read> Read for SeqReader<R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let mut i = 0;
        while self.current < self.readers.len() {
            let r = &mut self.readers[self.current];
            i = r.read(buf)?;
            if i == 0 {
                self.current += 1;
            } else {
                break;
            }
        }

        Ok(i)
    }
}

pub struct SourceReader<R> {
    reader: R,
    size: usize,
    crc: crc::Digest<'static, u32>,
    crc_value: u32,
}

impl<R> From<R> for SourceReader<R> {
    fn from(value: R) -> Self {
        Self::new(value)
    }
}

impl<R: Read> Read for SourceReader<R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let n = self.reader.read(buf)?;
        if self.crc_value == 0 {
            if n > 0 {
                self.size += n;
                self.crc.update(&buf[..n]);
            } else {
                let crc = std::mem::replace(&mut self.crc, crate::reader::CRC32.digest());
                self.crc_value = crc.finalize();
            }
        }
        Ok(n)
    }
}

impl<R> SourceReader<R> {
    pub fn new(reader: R) -> Self {
        Self {
            reader,
            size: 0,
            crc: crate::reader::CRC32.digest(),
            crc_value: 0,
        }
    }

    pub fn read_count(&self) -> usize {
        self.size
    }
    pub fn crc_value(&self) -> u32 {
        self.crc_value
    }
}

pub(crate) struct LazyFileReader {
    path: PathBuf,
    reader: Option<File>,
    end: bool,
}

impl LazyFileReader {
    pub fn new(path: PathBuf) -> Self {
        Self {
            path,
            reader: None,
            end: false,
        }
    }
}

impl Read for LazyFileReader {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        if self.end {
            return Ok(0);
        }
        if self.reader.is_none() {
            self.reader = Some(File::open(&self.path)?);
        }
        let n = self.reader.as_mut().unwrap().read(buf)?;
        if n == 0 {
            self.end = true;
            self.reader = None;
        }
        Ok(n)
    }
}