preserves/implementations/rust/preserves/src/value/domain.rs

144 lines
3.4 KiB
Rust

use std::io;
use super::IOValue;
use super::Reader;
use super::Writer;
pub trait Domain: Sized + std::fmt::Debug + Eq + std::hash::Hash + Ord + Clone {
type Decode: DomainDecode<Self> + Default;
type Encode: DomainEncode<Self> + Default;
}
pub trait DomainDecode<D: Domain> {
fn decode_embedded<'de, R: Reader<'de>>(
&mut self,
r: &mut R,
read_annotations: bool,
) -> io::Result<D>;
}
pub trait DomainEncode<D: Domain> {
fn encode_embedded<W: Writer>(
&mut self,
w: &mut W,
d: &D,
) -> io::Result<()>;
}
impl<'a, D: Domain, T: DomainDecode<D>> DomainDecode<D> for &'a mut T {
fn decode_embedded<'de, R: Reader<'de>>(
&mut self,
r: &mut R,
read_annotations: bool,
) -> io::Result<D> {
(**self).decode_embedded(r, read_annotations)
}
}
impl<'a, D: Domain, T: DomainEncode<D>> DomainEncode<D> for &'a mut T {
fn encode_embedded<W: Writer>(
&mut self,
w: &mut W,
d: &D,
) -> io::Result<()> {
(**self).encode_embedded(w, d)
}
}
#[derive(Default)]
pub struct DefaultDomainCodec;
impl<D: Domain> DomainDecode<D> for DefaultDomainCodec {
fn decode_embedded<'de, R: Reader<'de>>(
&mut self,
r: &mut R,
read_annotations: bool,
) -> io::Result<D> {
D::Decode::default().decode_embedded(r, read_annotations)
}
}
impl<D: Domain> DomainEncode<D> for DefaultDomainCodec {
fn encode_embedded<W: Writer>(
&mut self,
w: &mut W,
d: &D,
) -> io::Result<()> {
D::Encode::default().encode_embedded(w, d)
}
}
#[derive(Default)]
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>>(
&mut self,
r: &mut R,
_read_annotations: bool,
) -> io::Result<D> {
r.next_str()?.parse().map_err(|e: Err| e.into())
}
}
impl<D: Domain> DomainEncode<D> for DebugDomainCodec {
fn encode_embedded<W: Writer>(
&mut self,
w: &mut W,
d: &D,
) -> io::Result<()> {
w.write_string(&format!("{:?}", d))
}
}
#[derive(Default)]
pub struct NoEmbeddedDomainCodec;
impl<D: Domain> DomainDecode<D> for NoEmbeddedDomainCodec {
fn decode_embedded<'de, R: Reader<'de>>(
&mut self,
_r: &mut R,
_read_annotations: bool,
) -> io::Result<D> {
Err(io::Error::new(io::ErrorKind::Unsupported, "Embedded values not supported here"))
}
}
impl<D: Domain> DomainEncode<D> for NoEmbeddedDomainCodec {
fn encode_embedded<W: Writer>(
&mut self,
_w: &mut W,
_d: &D,
) -> io::Result<()> {
Err(io::Error::new(io::ErrorKind::Unsupported, "Embedded values not supported here"))
}
}
#[derive(Default)]
pub struct IOValueDomainCodec;
impl Domain for IOValue {
type Decode = IOValueDomainCodec;
type Encode = IOValueDomainCodec;
}
impl DomainDecode<IOValue> for IOValueDomainCodec {
fn decode_embedded<'de, R: Reader<'de>>(
&mut self,
r: &mut R,
read_annotations: bool,
) -> io::Result<IOValue> {
r.demand_next(read_annotations)
}
}
impl DomainEncode<IOValue> for IOValueDomainCodec {
fn encode_embedded<W: Writer>(
&mut self,
w: &mut W,
d: &IOValue,
) -> io::Result<()> {
w.write(self, d)
}
}