noodles_fasta/fai/
index.rs

1use std::io;
2
3use noodles_core::Region;
4
5use super::Record;
6
7/// A FASTA index.
8#[derive(Clone, Debug, Default, Eq, PartialEq)]
9pub struct Index(Vec<Record>);
10
11impl Index {
12    /// Returns start position of the given region.
13    pub fn query(&self, region: &Region) -> io::Result<u64> {
14        self.as_ref()
15            .iter()
16            .find(|record| record.name() == region.name())
17            .ok_or_else(|| {
18                io::Error::new(
19                    io::ErrorKind::InvalidInput,
20                    format!("invalid reference sequence name: {}", region.name()),
21                )
22            })
23            .and_then(|record| record.query(region.interval()))
24    }
25}
26
27impl AsRef<[Record]> for Index {
28    fn as_ref(&self) -> &[Record] {
29        &self.0
30    }
31}
32
33impl From<Vec<Record>> for Index {
34    fn from(records: Vec<Record>) -> Self {
35        Self(records)
36    }
37}
38
39impl From<Index> for Vec<Record> {
40    fn from(index: Index) -> Self {
41        index.0
42    }
43}