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
use std::io::{BufRead, BufReader, Read};

use clio::Input;
use flate2::bufread::GzDecoder;

pub struct FileInput {
    stream: Box<dyn BufRead>,
    input: Input
}

impl From<Input> for FileInput {
    fn from(value: Input) -> Self {
        let cloned_input = value.clone();
        if let Some(extension) = value.path().extension() {
            if extension == "gz" {
                return Self {
                    stream: Box::new(BufReader::new(GzDecoder::new(BufReader::new(value)))),
                    input: cloned_input
                };
            }
        }

        Self {
            stream: Box::new(BufReader::new(value)),
            input: cloned_input
        }
    }
}

impl From<&Input> for FileInput {
    fn from(value: &Input) -> Self {
        Self::from(value.clone())
    }
}


impl From<&mut Input> for FileInput {
    fn from(value: &mut Input) -> Self {
        Self::from(value.clone())
    }
}

impl Clone for FileInput {
    fn clone(&self) -> Self {
        self.input.clone().into()
    }
}

impl Read for FileInput {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        self.stream.read(buf)
    }
}

impl BufRead for FileInput {
    fn fill_buf(&mut self) -> std::io::Result<&[u8]> {
        self.stream.fill_buf()
    }

    fn consume(&mut self, amt: usize) {
        self.stream.consume(amt)
    }
}

impl<'a> TryFrom<&'a str> for FileInput {
    type Error = <Input as TryFrom<&'a str>>::Error;

    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
        let res = Input::try_from(value)?;
        Ok(res.into())
    }
}