use std::io::Read;
#[macro_use]
mod errors;
mod feature_table;
mod field;
mod location;
mod locus;
mod misc;
mod stream_parser;
use self::stream_parser::StreamParser;
use crate::seq::{Location, Seq};
pub use crate::errors::GbParserError;
#[derive(Debug)]
pub struct SeqReader<T: Read> {
parser: StreamParser<T>,
}
impl<T: Read> Iterator for SeqReader<T> {
type Item = Result<Seq, GbParserError>;
fn next(&mut self) -> Option<Result<Seq, GbParserError>> {
match self.parser.read_one_record() {
Ok(Some(seq)) => Some(Ok(seq)),
Ok(None) => None,
Err(e) => Some(Err(e)),
}
}
}
const READ_BUF_SIZE: usize = 64 * 1024;
impl<T: Read> SeqReader<T> {
pub fn new(data: T) -> SeqReader<T> {
SeqReader {
parser: StreamParser::new(data, READ_BUF_SIZE),
}
}
}
pub fn parse_file<P: AsRef<::std::path::Path>>(path: P) -> Result<Vec<Seq>, GbParserError> {
let file = ::std::fs::File::open(path)?;
SeqReader::new(file).collect()
}
pub fn parse_slice(data: &[u8]) -> Result<Vec<Seq>, GbParserError> {
let res = misc::gb_records(data);
match res {
Ok((_, o)) => Ok(o),
Err(e) => {
Err(GbParserError::SyntaxError(
format!("{:?}", e),
))
}
}
}
pub(crate) fn parse_location(data: &[u8]) -> Result<Location, GbParserError> {
let res = location::location(data);
match res {
Ok((_, o)) => Ok(o),
Err(e) => Err(GbParserError::SyntaxError(format!("{:?}", e))),
}
}