This commit is contained in:
Tony Garnock-Jones 2022-11-03 17:40:30 +01:00
parent dbc2a0f14c
commit 13f1f80b01
3 changed files with 40 additions and 85 deletions

View File

@ -11,7 +11,7 @@ pub trait Domain: std::fmt::Debug + Eq + std::hash::Hash + Ord + Clone {
}
pub trait DomainDecode<D: Domain> {
fn decode_embedded<'de, R: Reader<'de>>(
fn decode_embedded<'de, R: Reader<'de> + ?Sized>(
&mut self,
r: &mut R,
read_annotations: bool,
@ -27,7 +27,7 @@ pub trait DomainEncode<D: Domain> {
}
impl<'a, D: Domain, T: DomainDecode<D>> DomainDecode<D> for &'a mut T {
fn decode_embedded<'de, R: Reader<'de>>(
fn decode_embedded<'de, R: Reader<'de> + ?Sized>(
&mut self,
r: &mut R,
read_annotations: bool,
@ -50,7 +50,7 @@ impl<'a, D: Domain, T: DomainEncode<D>> DomainEncode<D> for &'a mut T {
pub struct DefaultDomainCodec;
impl<D: Domain> DomainDecode<D> for DefaultDomainCodec {
fn decode_embedded<'de, R: Reader<'de>>(
fn decode_embedded<'de, R: Reader<'de> + ?Sized>(
&mut self,
r: &mut R,
read_annotations: bool,
@ -73,7 +73,7 @@ impl<D: Domain> DomainEncode<D> for DefaultDomainCodec {
pub struct DebugDomainCodec;
impl<Err: Into<io::Error>, D: Domain + std::str::FromStr<Err = Err>> DomainDecode<D> for DebugDomainCodec {
fn decode_embedded<'de, R: Reader<'de>>(
fn decode_embedded<'de, R: Reader<'de> + ?Sized>(
&mut self,
r: &mut R,
_read_annotations: bool,
@ -96,7 +96,7 @@ impl<D: Domain> DomainEncode<D> for DebugDomainCodec {
pub struct NoEmbeddedDomainCodec;
impl<D: Domain> DomainDecode<D> for NoEmbeddedDomainCodec {
fn decode_embedded<'de, R: Reader<'de>>(
fn decode_embedded<'de, R: Reader<'de> + ?Sized>(
&mut self,
_r: &mut R,
_read_annotations: bool,
@ -124,7 +124,7 @@ impl Domain for IOValue {
}
impl DomainDecode<IOValue> for IOValueDomainCodec {
fn decode_embedded<'de, R: Reader<'de>>(
fn decode_embedded<'de, R: Reader<'de> + ?Sized>(
&mut self,
r: &mut R,
read_annotations: bool,

View File

@ -7,6 +7,9 @@ use crate::CompoundClass;
use crate::SignedInteger;
use crate::ValueClass;
use crate::boundary as B;
use crate::domain::Domain;
use crate::domain::DomainDecode;
use crate::domain::IOValueDomainCodec;
use crate::error::Error;
use crate::error::ExpectedKind;
use crate::error::io_eof;
@ -18,7 +21,7 @@ use crate::repr::Map;
use crate::repr::Record;
use crate::repr::Set;
use crate::repr::Value;
use crate::repr::iovalue;
use crate::repr::owned;
pub type ReaderResult<T> = std::result::Result<T, Error>;
@ -139,7 +142,12 @@ pub trait Reader<'de> {
}
}
fn next_iovalue(&mut self, read_annotations: bool) -> io::Result<IOValue> {
fn next<D: Domain + 'static, Dec: DomainDecode<D>>(
&mut self,
read_annotations: bool,
dec: &mut Dec,
) -> io::Result<Box<dyn Value<D>>>
{
let (anns, v) = match read_annotations {
true => self.gather_annotations()?.ok_or_else(io_eof)?,
false => (Vec::new(), self.skip_annotations()?.ok_or_else(io_eof)?),
@ -149,7 +157,7 @@ pub trait Reader<'de> {
self.next_atom()?.into_value(),
ValueClass::Embedded => {
self.open_embedded()?;
let v = self.next_iovalue(read_annotations)?;
let v = dec.decode_embedded(self, read_annotations)?;
self.close_embedded()?;
Box::new(Embedded::new(v))
}
@ -158,9 +166,9 @@ pub trait Reader<'de> {
self.open_record()?;
let mut b = B::start(B::Item::RecordLabel);
self.boundary(&b)?;
vs.push(self.next_iovalue(read_annotations)?);
vs.push(self.next(read_annotations, dec)?);
while !self.close_compound(&mut b, &B::Item::RecordField)? {
vs.push(self.next_iovalue(read_annotations)?);
vs.push(self.next(read_annotations, dec)?);
}
Box::new(Record::_from_vec(vs))
}
@ -169,7 +177,7 @@ pub trait Reader<'de> {
self.open_sequence()?;
let mut b = B::Type::default();
while !self.close_compound(&mut b, &B::Item::SequenceValue)? {
vs.push(self.next_iovalue(read_annotations)?);
vs.push(self.next(read_annotations, dec)?);
}
Box::new(vs)
}
@ -178,7 +186,7 @@ pub trait Reader<'de> {
self.open_set()?;
let mut b = B::Type::default();
while !self.close_compound(&mut b, &B::Item::SetValue)? {
s.insert(self.next_iovalue(read_annotations)?);
s.insert(self.next(read_annotations, dec)?);
}
Box::new(s)
}
@ -187,21 +195,26 @@ pub trait Reader<'de> {
self.open_dictionary()?;
let mut b = B::Type::default();
while !self.close_compound(&mut b, &B::Item::DictionaryKey)? {
let k = self.next_iovalue(read_annotations)?;
let k = self.next(read_annotations, dec)?;
b.shift(Some(B::Item::DictionaryValue));
self.boundary(&b)?;
d.insert(k, self.next_iovalue(read_annotations)?);
d.insert(k, self.next(read_annotations, dec)?);
}
Box::new(d)
}
};
if anns.is_empty() {
Ok(value.into())
Ok(value)
} else {
Ok(iovalue(Annotations::new(value, anns)))
Ok(owned(Annotations::new(value, anns)))
}
}
fn next_iovalue(&mut self, read_annotations: bool) -> io::Result<IOValue>
{
Ok(self.next(read_annotations, &mut IOValueDomainCodec)?.into())
}
fn next_boolean(&mut self) -> ReaderResult<bool> {
self.next_iovalue(false)?.as_boolean().ok_or(Error::Expected(ExpectedKind::Boolean))
}
@ -271,64 +284,6 @@ pub trait Reader<'de> {
}
}
impl<'r, 'de, R: Reader<'de>> Reader<'de> for &'r mut R {
fn peek_class(&mut self) -> io::Result<Option<NextToken>> {
(*self).peek_class()
}
fn next_atom(&mut self) -> ReaderResult<Atom<'de>> {
(*self).next_atom()
}
fn boundary(&mut self, b: &B::Type) -> ReaderResult<()> {
(*self).boundary(b)
}
fn open_record(&mut self) -> ReaderResult<()> {
(*self).open_record()
}
fn open_sequence(&mut self) -> ReaderResult<()> {
(*self).open_sequence()
}
fn open_set(&mut self) -> ReaderResult<()> {
(*self).open_set()
}
fn open_dictionary(&mut self) -> ReaderResult<()> {
(*self).open_dictionary()
}
fn close_compound(&mut self, b: &mut B::Type, i: &B::Item) -> ReaderResult<bool> {
(*self).close_compound(b, i)
}
fn open_embedded(&mut self) -> ReaderResult<()> {
(*self).open_embedded()
}
fn close_embedded(&mut self) -> ReaderResult<()> {
(*self).close_embedded()
}
fn open_annotation(&mut self) -> ReaderResult<()> {
(*self).open_annotation()
}
fn close_annotation(&mut self) -> ReaderResult<()> {
(*self).close_annotation()
}
fn mark(&mut self) -> io::Result<usize> {
(*self).mark()
}
fn restore(&mut self, mark: usize) -> io::Result<()> {
(*self).restore(mark)
}
}
pub struct IOValues<'de, R: Reader<'de>> {
pub reader: R,
pub read_annotations: bool,

View File

@ -322,7 +322,7 @@ impl<'a> Atom<'a> {
}
impl<'a, D: Domain> Value<D> for Atom<'a> {
fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode<D>) -> io::Result<()> {
fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode<D>) -> io::Result<()> {
match self {
Atom::Boolean(b) => w.write_bool(*b),
Atom::Float(f) => w.write_f32(*f),
@ -395,14 +395,14 @@ impl<D: Domain> Value<D> for NoValue {
}
impl<D: Domain> Value<D> for bool {
fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_bool(*self) }
fn write(&self, w: &mut dyn Writer, __enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_bool(*self) }
fn value_clone(&self) -> Box<dyn Value<D>> where D: 'static { Box::new(*self) }
fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::Boolean) }
fn as_boolean(&self) -> Option<bool> { Some(*self) }
}
impl<D: Domain> Value<D> for u64 {
fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_u64(*self) }
fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_u64(*self) }
fn value_clone(&self) -> Box<dyn Value<D>> where D: 'static { Box::new(*self) }
fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::SignedInteger) }
fn as_signed_integer(&self) -> Option<SignedInteger> {
@ -411,7 +411,7 @@ impl<D: Domain> Value<D> for u64 {
}
impl<D: Domain> Value<D> for SignedInteger {
fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_signed_integer(self) }
fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_signed_integer(self) }
fn value_clone(&self) -> Box<dyn Value<D>> where D: 'static { Box::new(self.clone()) }
fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::SignedInteger) }
fn as_signed_integer(&self) -> Option<SignedInteger> {
@ -420,7 +420,7 @@ impl<D: Domain> Value<D> for SignedInteger {
}
impl<D: Domain> Value<D> for f32 {
fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_f32(*self) }
fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_f32(*self) }
fn value_clone(&self) -> Box<dyn Value<D>> where D: 'static { Box::new(*self) }
fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::Float) }
fn as_float(&self) -> Option<f32> { Some(*self) }
@ -428,7 +428,7 @@ impl<D: Domain> Value<D> for f32 {
}
impl<D: Domain> Value<D> for f64 {
fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_f64(*self) }
fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_f64(*self) }
fn value_clone(&self) -> Box<dyn Value<D>> where D: 'static { Box::new(*self) }
fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::Float) }
fn as_float(&self) -> Option<f32> { Some(*self as f32) }
@ -436,14 +436,14 @@ impl<D: Domain> Value<D> for f64 {
}
impl<D: Domain> Value<D> for str {
fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_string(self) }
fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_string(self) }
fn value_clone(&self) -> Box<dyn Value<D>> where D: 'static { Box::new(self.to_owned()) }
fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::String) }
fn as_string(&self) -> Option<Cow<'_, str>> { Some(Cow::Borrowed(self)) }
}
impl<D: Domain> Value<D> for String {
fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_string(self) }
fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_string(self) }
fn value_clone(&self) -> Box<dyn Value<D>> where D: 'static { Box::new(self.clone()) }
fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::String) }
fn as_string(&self) -> Option<Cow<'_, str>> { Some(Cow::Borrowed(self)) }
@ -454,7 +454,7 @@ impl<D: Domain> Value<D> for String {
pub struct Bytes<T: AsRef<[u8]>>(T);
impl<T: AsRef<[u8]> + Debug, D: Domain> Value<D> for Bytes<T> {
fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_bytes(self.0.as_ref()) }
fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_bytes(self.0.as_ref()) }
fn value_clone(&self) -> Box<dyn Value<D>> where D: 'static { Box::new(Bytes(self.0.as_ref().to_owned())) }
fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::ByteString) }
fn as_bytestring(&self) -> Option<Cow<'_, [u8]>> { Some(Cow::Borrowed(self.0.as_ref())) }
@ -471,7 +471,7 @@ impl<T: AsRef<str> + Debug> Symbol<T> {
}
impl<T: AsRef<str> + Debug, D: Domain> Value<D> for Symbol<T> {
fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_symbol(self.0.as_ref()) }
fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode<D>) -> io::Result<()> { w.write_symbol(self.0.as_ref()) }
fn value_clone(&self) -> Box<dyn Value<D>> where D: 'static { Box::new(Symbol(self.0.as_ref().to_owned())) }
fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::Symbol) }
fn as_symbol(&self) -> Option<Cow<'_, str>> { Some(Cow::Borrowed(self.0.as_ref())) }