Simplify by removing DelimitedStream

This commit is contained in:
Tony Garnock-Jones 2021-07-21 22:48:00 +02:00
parent 9b88db6790
commit 48412ae7ea
1 changed files with 15 additions and 39 deletions

View File

@ -23,7 +23,6 @@ use super::super::{
reader::{ reader::{
Token, Token,
BinarySource, BinarySource,
ConfiguredReader,
Reader, Reader,
ReaderResult, ReaderResult,
}, },
@ -249,6 +248,13 @@ impl<'de, 'src, D: Embeddable, N: NestedValue<D>, Dec: DomainDecode<D>, S: Binar
} }
Ok(()) Ok(())
} }
fn next_upto_end(&mut self, read_annotations: bool) -> io::Result<Option<N>> {
match self.peekend()? {
true => Ok(None),
false => Ok(Some(self.demand_next(read_annotations)?)),
}
}
} }
impl<'de, 'src, D: Embeddable, N: NestedValue<D>, Dec: DomainDecode<D>, S: BinarySource<'de>> impl<'de, 'src, D: Embeddable, N: NestedValue<D>, Dec: DomainDecode<D>, S: BinarySource<'de>>
@ -314,34 +320,28 @@ impl<'de, 'src, D: Embeddable, N: NestedValue<D>, Dec: DomainDecode<D>, S: Binar
Value::Symbol(decodestr(self.readbytes(count)?)?.into_owned()).wrap() Value::Symbol(decodestr(self.readbytes(count)?)?.into_owned()).wrap()
} }
Tag::Record => { Tag::Record => {
let iter = DelimitedStream { reader: self.configured(read_annotations) }; let mut vs = Vec::new();
let vs = iter.collect::<io::Result<Vec<N>>>()?; while let Some(v) = self.next_upto_end(read_annotations)? { vs.push(v); }
if vs.is_empty() { if vs.is_empty() {
return Err(io_syntax_error("Too few elements in encoded record")) return Err(io_syntax_error("Too few elements in encoded record"))
} }
Value::Record(Record(vs)).wrap() Value::Record(Record(vs)).wrap()
} }
Tag::Sequence => { Tag::Sequence => {
let iter = DelimitedStream { reader: self.configured(read_annotations) }; let mut vs = Vec::new();
let vs = iter.collect::<io::Result<Vec<N>>>()?; while let Some(v) = self.next_upto_end(read_annotations)? { vs.push(v); }
Value::Sequence(vs).wrap() Value::Sequence(vs).wrap()
} }
Tag::Set => { Tag::Set => {
let iter = DelimitedStream { reader: self.configured(read_annotations) };
let mut s = Set::new(); let mut s = Set::new();
for res in iter { s.insert(res?); } while let Some(v) = self.next_upto_end(read_annotations)? { s.insert(v); }
Value::Set(s).wrap() Value::Set(s).wrap()
} }
Tag::Dictionary => { Tag::Dictionary => {
let mut iter = DelimitedStream { reader: self.configured(read_annotations) };
let mut d = Map::new(); let mut d = Map::new();
while let Some(kres) = iter.next() { while let Some(k) = self.next_upto_end(read_annotations)? {
let k = kres?; match self.next_upto_end(read_annotations)? {
match iter.next() { Some(v) => { d.insert(k, v); }
Some(vres) => {
let v = vres?;
d.insert(k, v);
}
None => return Err(io_syntax_error("Missing dictionary value")), None => return Err(io_syntax_error("Missing dictionary value")),
} }
} }
@ -539,30 +539,6 @@ impl<'de, 'src, D: Embeddable, N: NestedValue<D>, Dec: DomainDecode<D>, S: Binar
} }
} }
struct DelimitedStream
<'a, 'de, 'src,
D: Embeddable,
N: NestedValue<D>,
Dec: DomainDecode<D>,
S: BinarySource<'de>>
{
reader: ConfiguredReader<'de, D, N, &'a mut PackedReader<'de, 'src, D, N, Dec, S>>,
}
impl<'a, 'de, 'src, D: Embeddable, N: NestedValue<D>, Dec: DomainDecode<D>, S: BinarySource<'de>>
Iterator
for DelimitedStream<'a, 'de, 'src, D, N, Dec, S>
{
type Item = io::Result<N>;
fn next(&mut self) -> Option<Self::Item> {
match self.reader.reader.peekend() {
Err(e) => Some(Err(e)),
Ok(true) => None,
Ok(false) => Some(self.reader.reader.demand_next(self.reader.read_annotations)),
}
}
}
fn decodestr(cow: Cow<'_, [u8]>) -> io::Result<Cow<'_, str>> { fn decodestr(cow: Cow<'_, [u8]>) -> io::Result<Cow<'_, str>> {
match cow { match cow {
Cow::Borrowed(bs) => Cow::Borrowed(bs) =>