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

67 lines
1.5 KiB
Rust
Raw Normal View History

use std::io;
use super::Embeddable;
use super::IOValue;
use super::Reader;
2021-07-05 10:34:29 +00:00
use super::Writer;
2021-08-02 09:42:48 +00:00
pub trait DomainDecode<D: Embeddable> {
2022-07-10 11:25:35 +00:00
fn decode_embedded<'de, 'r, R: Reader<'de>>(
&mut self,
2022-07-10 11:25:35 +00:00
r: &'r mut R,
read_annotations: bool,
) -> io::Result<D>;
}
2021-07-05 10:34:29 +00:00
pub trait DomainEncode<D: Embeddable> {
fn encode_embedded<W: Writer>(
&mut self,
w: &mut W,
d: &D,
) -> io::Result<()>;
2021-07-05 10:34:29 +00:00
}
pub struct IOValueDomainCodec;
2021-07-05 10:34:29 +00:00
impl DomainDecode<IOValue> for IOValueDomainCodec {
2022-07-10 11:25:35 +00:00
fn decode_embedded<'de, 'r, R: Reader<'de>>(
&mut self,
2022-07-10 11:25:35 +00:00
r: &'r mut R,
read_annotations: bool,
) -> io::Result<IOValue> {
2022-07-10 11:25:35 +00:00
r.demand_next(read_annotations, self)
2021-07-05 10:34:29 +00:00
}
}
impl DomainEncode<IOValue> for IOValueDomainCodec {
fn encode_embedded<W: Writer>(
&mut self,
w: &mut W,
d: &IOValue,
) -> io::Result<()> {
2021-07-05 10:34:29 +00:00
w.write(self, d)
}
}
2021-07-05 10:34:29 +00:00
pub struct NoEmbeddedDomainCodec;
2021-07-05 10:34:29 +00:00
impl<D: Embeddable> DomainDecode<D> for NoEmbeddedDomainCodec {
2022-07-10 11:25:35 +00:00
fn decode_embedded<'de, 'r, R: Reader<'de>>(
&mut self,
2022-07-10 11:25:35 +00:00
_r: &'r mut R,
_read_annotations: bool,
) -> io::Result<D> {
Err(io::Error::new(io::ErrorKind::Unsupported, "Embedded values not supported here"))
}
}
2021-07-05 10:34:29 +00:00
impl<D: Embeddable> 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"))
2021-07-05 10:34:29 +00:00
}
}