2020-05-25 13:00:58 +00:00
|
|
|
use super::reader::{Reader, is_eof_error};
|
2020-05-26 09:01:33 +00:00
|
|
|
use super::value::IOValue;
|
2019-09-09 21:08:26 +00:00
|
|
|
|
2020-05-25 13:40:01 +00:00
|
|
|
pub use super::reader::{Result, DecodePlaceholderMap};
|
2019-09-09 21:08:26 +00:00
|
|
|
|
2020-05-26 09:01:33 +00:00
|
|
|
pub struct Decoder<'a, R: Reader> {
|
2020-05-25 11:42:06 +00:00
|
|
|
pub read: R,
|
2020-05-26 09:01:33 +00:00
|
|
|
placeholders: Option<&'a DecodePlaceholderMap>,
|
2019-09-15 11:25:23 +00:00
|
|
|
read_annotations: bool,
|
2019-09-09 21:08:26 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 09:01:33 +00:00
|
|
|
impl<'a, R: Reader> Decoder<'a, R> {
|
|
|
|
pub fn new(read: R, placeholders: Option<&'a DecodePlaceholderMap>) ->
|
2020-05-25 11:42:06 +00:00
|
|
|
Self
|
|
|
|
{
|
2020-05-25 13:00:58 +00:00
|
|
|
Decoder {
|
2019-09-09 21:08:26 +00:00
|
|
|
read,
|
2019-09-17 10:27:44 +00:00
|
|
|
placeholders,
|
2019-09-15 11:25:23 +00:00
|
|
|
read_annotations: true,
|
2019-09-09 21:08:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 12:57:04 +00:00
|
|
|
pub fn set_read_annotations(&mut self, read_annotations: bool) {
|
2019-09-15 11:25:23 +00:00
|
|
|
self.read_annotations = read_annotations
|
|
|
|
}
|
|
|
|
|
2020-05-26 09:01:33 +00:00
|
|
|
pub fn next_or_err(&mut self) -> Result<IOValue> {
|
2020-05-25 13:00:58 +00:00
|
|
|
self.read.next(self.placeholders, self.read_annotations)
|
2019-09-09 21:08:26 +00:00
|
|
|
}
|
2020-05-25 11:42:06 +00:00
|
|
|
}
|
2019-09-09 21:08:26 +00:00
|
|
|
|
2020-05-26 09:01:33 +00:00
|
|
|
impl<'a, R: Reader> std::iter::Iterator for Decoder<'a, R> {
|
|
|
|
type Item = Result<IOValue>;
|
2020-05-25 11:42:06 +00:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
match self.next_or_err() {
|
|
|
|
Err(e) if is_eof_error(&e) => None,
|
|
|
|
other => Some(other)
|
2019-09-09 21:08:26 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-25 11:42:06 +00:00
|
|
|
}
|