1use std::io::Read;
2
3#[macro_use]
4mod errors;
5mod feature_table;
6mod field;
7mod location;
8mod locus;
9mod misc;
10mod stream_parser;
11
12use self::stream_parser::StreamParser;
13use crate::seq::{Location, Seq};
14
15pub use crate::errors::GbParserError;
16
17#[derive(Debug)]
18pub struct SeqReader<T: Read> {
19 parser: StreamParser<T>,
20}
21
22impl<T: Read> Iterator for SeqReader<T> {
23 type Item = Result<Seq, GbParserError>;
24
25 fn next(&mut self) -> Option<Result<Seq, GbParserError>> {
26 match self.parser.read_one_record() {
27 Ok(Some(seq)) => Some(Ok(seq)),
28 Ok(None) => None,
29 Err(e) => Some(Err(e)),
30 }
31 }
32}
33
34const READ_BUF_SIZE: usize = 64 * 1024;
35
36impl<T: Read> SeqReader<T> {
37 pub fn new(data: T) -> SeqReader<T> {
39 SeqReader {
40 parser: StreamParser::new(data, READ_BUF_SIZE),
41 }
42 }
43}
44
45pub fn parse_file<P: AsRef<::std::path::Path>>(path: P) -> Result<Vec<Seq>, GbParserError> {
47 let file = ::std::fs::File::open(path)?;
48 SeqReader::new(file).collect()
49}
50
51pub fn parse_slice(data: &[u8]) -> Result<Vec<Seq>, GbParserError> {
56 let res = misc::gb_records(data);
57 match res {
58 Ok((_, o)) => Ok(o),
59 Err(e) => {
60 Err(GbParserError::SyntaxError(
61 format!("{:?}", e),
62 ))
63 }
64 }
65}
66
67pub(crate) fn parse_location(data: &[u8]) -> Result<Location, GbParserError> {
69 let res = location::location(data);
70 match res {
71 Ok((_, o)) => Ok(o),
72 Err(e) => Err(GbParserError::SyntaxError(format!("{:?}", e))),
73 }
74}