82 lines
2.1 KiB
Rust
82 lines
2.1 KiB
Rust
pub use lazy_static::lazy_static;
|
|
|
|
use preserves::value::ArcValue;
|
|
use preserves::value::Domain;
|
|
use preserves::value::Embeddable;
|
|
use preserves::value::IOResult;
|
|
use preserves::value::IOValue;
|
|
use preserves::value::NestedValue;
|
|
use preserves::value::NoEmbeddedDomainCodec;
|
|
use preserves::value::Value;
|
|
|
|
use std::convert::From;
|
|
use std::convert::TryFrom;
|
|
use std::sync::Arc;
|
|
|
|
pub fn decode_lit<D: Embeddable, N: NestedValue<D>>(bs: &[u8]) -> IOResult<N> {
|
|
preserves::value::packed::from_bytes(bs, NoEmbeddedDomainCodec)
|
|
}
|
|
|
|
pub fn decode_embedded<D: Domain>(
|
|
v: &IOValue,
|
|
) -> Result<ArcValue<Arc<D>>, ParseError>
|
|
where
|
|
for<'a> D: TryFrom<&'a IOValue, Error = ParseError>
|
|
{
|
|
v.copy_via(&mut |d| Ok(Value::Embedded(Arc::new(D::try_from(d)?))))
|
|
}
|
|
|
|
pub fn encode_embedded<D: Domain>(
|
|
v: &ArcValue<Arc<D>>,
|
|
) -> IOValue
|
|
where
|
|
for<'a> IOValue: From<&'a D>
|
|
{
|
|
v.copy_via::<_, _, _, std::convert::Infallible>(
|
|
&mut |d| Ok(Value::Embedded(IOValue::from(d)))).unwrap()
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum ParseError {
|
|
ConformanceError,
|
|
IO(std::io::Error),
|
|
Preserves(preserves::error::Error),
|
|
}
|
|
|
|
const INPUT_NOT_CONFORMANT: &str = "Input not conformant with Schema";
|
|
|
|
impl std::fmt::Display for ParseError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
|
|
match self {
|
|
ParseError::ConformanceError => write!(f, "{}", INPUT_NOT_CONFORMANT),
|
|
ParseError::IO(e) => e.fmt(f),
|
|
ParseError::Preserves(e) => e.fmt(f),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for ParseError {}
|
|
|
|
impl From<std::io::Error> for ParseError {
|
|
fn from(v: std::io::Error) -> Self {
|
|
ParseError::IO(v)
|
|
}
|
|
}
|
|
|
|
impl From<preserves::error::Error> for ParseError {
|
|
fn from(v: preserves::error::Error) -> Self {
|
|
ParseError::Preserves(v)
|
|
}
|
|
}
|
|
|
|
impl From<ParseError> for std::io::Error {
|
|
fn from(v: ParseError) -> Self {
|
|
match v {
|
|
ParseError::ConformanceError =>
|
|
std::io::Error::new(std::io::ErrorKind::InvalidData, INPUT_NOT_CONFORMANT),
|
|
ParseError::IO(e) => e,
|
|
ParseError::Preserves(e) => e.into(),
|
|
}
|
|
}
|
|
}
|