2021-07-03 21:11:41 +00:00
|
|
|
pub use lazy_static::lazy_static;
|
|
|
|
|
2021-08-28 15:01:36 +00:00
|
|
|
pub use preserves;
|
2022-10-26 16:12:55 +00:00
|
|
|
pub use preserves::value::DomainDecode;
|
2021-07-21 19:52:20 +00:00
|
|
|
pub use preserves::value::Reader;
|
2021-08-02 18:55:44 +00:00
|
|
|
pub use preserves::value::boundary as B;
|
2021-07-21 19:52:20 +00:00
|
|
|
|
2021-11-16 21:10:04 +00:00
|
|
|
pub mod interpret;
|
|
|
|
|
2022-10-26 16:12:55 +00:00
|
|
|
use preserves::value::ConfiguredReader;
|
2021-07-03 21:11:41 +00:00
|
|
|
use preserves::value::Domain;
|
2022-10-26 16:12:55 +00:00
|
|
|
use preserves::value::DomainEncode;
|
2021-07-03 21:11:41 +00:00
|
|
|
use preserves::value::NestedValue;
|
2021-07-05 10:34:29 +00:00
|
|
|
use preserves::value::NoEmbeddedDomainCodec;
|
2022-10-26 16:12:55 +00:00
|
|
|
use preserves::value::Writer;
|
2021-07-03 21:11:41 +00:00
|
|
|
|
2021-06-30 13:50:50 +00:00
|
|
|
use std::convert::From;
|
2021-09-15 13:07:46 +00:00
|
|
|
use std::convert::Into;
|
2021-07-05 10:38:11 +00:00
|
|
|
use std::io;
|
2022-10-26 16:12:55 +00:00
|
|
|
use std::marker::PhantomData;
|
2021-06-30 13:50:50 +00:00
|
|
|
|
2021-07-21 19:52:20 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2021-09-20 13:38:21 +00:00
|
|
|
pub trait NestedValueCodec {} // marker trait
|
|
|
|
impl NestedValueCodec for () {}
|
|
|
|
|
2021-09-17 09:06:53 +00:00
|
|
|
pub trait Parse<L, Value: NestedValue>: Sized {
|
|
|
|
fn parse(language: L, value: &Value) -> Result<Self, ParseError>;
|
2021-09-15 12:03:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 13:38:21 +00:00
|
|
|
impl<'a, T: NestedValueCodec, Value: NestedValue> Parse<&'a T, Value> for Value {
|
|
|
|
fn parse(_language: &'a T, value: &Value) -> Result<Self, ParseError> {
|
2021-09-15 12:03:33 +00:00
|
|
|
Ok(value.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 09:06:53 +00:00
|
|
|
pub trait Unparse<L, Value: NestedValue> {
|
|
|
|
fn unparse(&self, language: L) -> Value;
|
2021-09-15 12:03:33 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 13:38:21 +00:00
|
|
|
impl<'a, T: NestedValueCodec, Value: NestedValue> Unparse<&'a T, Value> for Value {
|
|
|
|
fn unparse(&self, _language: &'a T) -> Value {
|
2021-09-15 12:03:33 +00:00
|
|
|
self.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 09:06:53 +00:00
|
|
|
pub trait Codec<N: NestedValue> {
|
|
|
|
fn parse<'a, T: Parse<&'a Self, N>>(&'a self, value: &N) -> Result<T, ParseError>;
|
|
|
|
fn unparse<'a, T: Unparse<&'a Self, N>>(&'a self, value: &T) -> N;
|
2021-09-15 08:43:31 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 09:06:53 +00:00
|
|
|
impl<L, N: NestedValue> Codec<N> for L {
|
|
|
|
fn parse<'a, T: Parse<&'a L, N>>(&'a self, value: &N) -> Result<T, ParseError> {
|
2021-09-15 13:07:46 +00:00
|
|
|
T::parse(self, value)
|
2021-09-15 08:43:31 +00:00
|
|
|
}
|
2021-09-15 12:03:33 +00:00
|
|
|
|
2021-09-17 09:06:53 +00:00
|
|
|
fn unparse<'a, T: Unparse<&'a L, N>>(&'a self, value: &T) -> N {
|
2021-09-15 13:07:46 +00:00
|
|
|
value.unparse(self)
|
2021-09-15 12:03:33 +00:00
|
|
|
}
|
2021-09-15 08:43:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 12:28:30 +00:00
|
|
|
pub trait Deserialize<N: NestedValue>
|
2021-07-21 19:52:20 +00:00
|
|
|
where
|
|
|
|
Self: Sized
|
|
|
|
{
|
2022-10-26 16:12:55 +00:00
|
|
|
fn deserialize<'de, Dec: DomainDecode<N::Embedded>, R: Reader<'de>>(
|
|
|
|
r: &mut ConfiguredReader<'de, N, R, Dec>,
|
|
|
|
) -> Result<Self, ParseError>;
|
2021-07-21 19:52:20 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 16:12:55 +00:00
|
|
|
pub trait Serialize
|
|
|
|
{
|
|
|
|
fn serialize<W: Writer>(&self, w: &mut W) -> io::Result<()>;
|
2021-07-03 21:11:41 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 16:12:55 +00:00
|
|
|
pub struct SchemaDomainCodec<N: NestedValue>(PhantomData<N>);
|
|
|
|
|
|
|
|
impl<N: NestedValue> Default for SchemaDomainCodec<N> {
|
|
|
|
fn default() -> Self {
|
|
|
|
SchemaDomainCodec(PhantomData)
|
|
|
|
}
|
2021-07-03 21:11:41 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 16:12:55 +00:00
|
|
|
impl<N, T: Deserialize<N> + Domain> DomainDecode<T> for SchemaDomainCodec<N>
|
2021-07-03 21:11:41 +00:00
|
|
|
where
|
2022-10-26 16:12:55 +00:00
|
|
|
for<'value> N: NestedValue<Embedded = T> + 'value
|
2021-07-03 21:11:41 +00:00
|
|
|
{
|
2022-10-26 16:12:55 +00:00
|
|
|
fn decode_embedded<'de, R: Reader<'de>>(
|
|
|
|
&mut self,
|
|
|
|
r: &mut R,
|
|
|
|
read_annotations: bool,
|
|
|
|
) -> io::Result<T> {
|
|
|
|
Ok(T::deserialize(&mut r.configured::<N, T::Decode>(read_annotations, T::Decode::default()))?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N: NestedValue<Embedded = T>, T: /* Serialize + */ Domain> DomainEncode<T> for SchemaDomainCodec<N> {
|
|
|
|
fn encode_embedded<W: Writer>(
|
|
|
|
&mut self,
|
|
|
|
w: &mut W,
|
|
|
|
d: &T,
|
|
|
|
) -> io::Result<()> {
|
|
|
|
todo!()
|
|
|
|
// d.serialize(w)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn decode_lit<N: NestedValue>(bs: &[u8]) -> io::Result<N> {
|
|
|
|
preserves::value::packed::from_bytes(bs, &mut NoEmbeddedDomainCodec)
|
2021-07-03 21:11:41 +00:00
|
|
|
}
|
2021-06-29 20:32:35 +00:00
|
|
|
|
2021-07-21 19:52:20 +00:00
|
|
|
#[derive(Error, Debug)]
|
2021-06-30 13:50:50 +00:00
|
|
|
pub enum ParseError {
|
2021-07-21 19:52:20 +00:00
|
|
|
#[error("Input not conformant with Schema: {0}")]
|
|
|
|
ConformanceError(&'static str),
|
|
|
|
#[error(transparent)]
|
2021-08-10 12:40:20 +00:00
|
|
|
Preserves(preserves::error::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<preserves::error::Error> for ParseError {
|
|
|
|
fn from(v: preserves::error::Error) -> Self {
|
|
|
|
match v {
|
2022-10-26 16:12:55 +00:00
|
|
|
preserves::error::Error::Expected(_) =>
|
2021-08-10 12:40:20 +00:00
|
|
|
ParseError::ConformanceError("preserves::error::Error::Expected"),
|
|
|
|
_ =>
|
|
|
|
ParseError::Preserves(v),
|
|
|
|
}
|
|
|
|
}
|
2021-06-30 13:50:50 +00:00
|
|
|
}
|
|
|
|
|
2021-07-05 11:00:30 +00:00
|
|
|
impl From<io::Error> for ParseError {
|
|
|
|
fn from(v: io::Error) -> Self {
|
2021-07-21 19:52:20 +00:00
|
|
|
preserves::error::Error::from(v).into()
|
2021-06-30 13:50:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-05 11:00:30 +00:00
|
|
|
impl From<ParseError> for io::Error {
|
2021-06-30 13:50:50 +00:00
|
|
|
fn from(v: ParseError) -> Self {
|
|
|
|
match v {
|
2021-07-21 19:52:20 +00:00
|
|
|
ParseError::ConformanceError(_) => io::Error::new(io::ErrorKind::InvalidData, v),
|
2021-06-30 13:50:50 +00:00
|
|
|
ParseError::Preserves(e) => e.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-21 19:52:20 +00:00
|
|
|
|
|
|
|
impl ParseError {
|
|
|
|
pub fn conformance_error(context: &'static str) -> Self {
|
|
|
|
ParseError::ConformanceError(context)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_conformance_error(&self) -> bool {
|
|
|
|
return if let ParseError::ConformanceError(_) = self { true } else { false }
|
|
|
|
}
|
|
|
|
}
|