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

use noodles_bgzf as bgzf;

use super::Reader;

/// A FASTA reader builder.
#[derive(Debug, Default)]
pub struct Builder;

impl Builder {
    /// Builds a FASTA reader from a path.
    pub fn build_from_path<P>(self, src: P) -> io::Result<Reader<Box<dyn BufRead>>>
    where
        P: AsRef<Path>,
    {
        let src = src.as_ref();

        let file = File::open(src)?;

        let reader: Box<dyn BufRead> = match src.extension().and_then(|ext| ext.to_str()) {
            Some("gz" | "bgz") => Box::new(bgzf::Reader::new(file)),
            _ => Box::new(BufReader::new(file)),
        };

        self.build_from_reader(reader)
    }

    /// Builds a FASTA reader from a reader.
    pub fn build_from_reader<R>(self, reader: R) -> io::Result<Reader<R>>
    where
        R: BufRead,
    {
        Ok(Reader::new(reader))
    }
}