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::io::{self, BufRead, Seek};

use noodles_core::Region;

use crate::{repository::Adapter, Record};

/// An indexed reader adapter.
pub struct IndexedReader<R> {
    reader: crate::io::IndexedReader<R>,
}

impl<R> IndexedReader<R>
where
    R: BufRead + Seek,
{
    /// Creates an indexed reader adapter.
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::io;
    /// use noodles_fasta::{self as fasta, repository::adapters::IndexedReader};
    /// let index = Vec::new();
    /// let reader = fasta::io::IndexedReader::new(io::empty(), index);
    /// let adapter = IndexedReader::new(reader);
    /// ```
    pub fn new(reader: crate::io::IndexedReader<R>) -> Self {
        Self { reader }
    }
}

impl<R> Adapter for IndexedReader<R>
where
    R: BufRead + Seek + Send + Sync,
{
    fn get(&mut self, name: &[u8]) -> Option<io::Result<Record>> {
        let region = Region::new(name, ..);
        Some(self.reader.query(&region))
    }
}