1
#[derive(Debug)]
2
pub enum Fetcher {
3
    File(Box<super::File>),
4
    Memory(Box<super::Memory>),
5
}
6

            
7
impl Fetcher {
8
    pub fn new_file(path: &std::path::Path) -> Self {
9
	Self::File(Box::new(super::file::File::new(path)))
10
    }
11

            
12
    #[cfg(test)]
13
2
    pub fn new_memory(buf: &[u8]) -> Self {
14
2
	Self::Memory(Box::new(super::memory::Memory::new(buf)))
15
2
    }
16

            
17
6
    pub fn is_mmaped(&self) -> bool {
18
6
	match self {
19
	    Self::File(f)	=> f.is_mmaped(),
20
6
	    Self::Memory(_)	=> true,
21
	}
22
6
    }
23

            
24
    pub fn open(&mut self) -> crate::Result<()> {
25
	match self {
26
	    Self::File(f)	=> f.open(),
27
	    Self::Memory(m)	=> m.open(),
28
	}
29
    }
30

            
31
    pub fn get_size(&self) -> Option<u64> {
32
	match self {
33
	    Self::File(f)	=> f.get_size(),
34
	    Self::Memory(m)	=> m.get_size(),
35
	}
36
    }
37

            
38
    pub async fn read(&mut self, buf: &mut [u8]) -> crate::Result<usize>
39
    {
40
	match self {
41
	    Self::File(f)	=> f.read(buf).await,
42
	    Self::Memory(m)	=> m.read(buf).await,
43
	}
44
    }
45

            
46
10
    pub fn read_mmap(&mut self, cnt: usize) -> crate::Result<&[u8]>
47
10
    {
48
10
	match self {
49
	    Self::File(f)	=> f.read_mmap(cnt),
50
10
	    Self::Memory(m)	=> m.read_mmap(cnt),
51
	}
52
10
    }
53

            
54
11
    pub fn is_eof(&self) -> bool
55
11
    {
56
11
	match self {
57
	    Self::File(f)	=> f.is_eof(),
58
11
	    Self::Memory(m)	=> m.is_eof(),
59
	}
60
11
    }
61
}