Start serde cleanup
This commit is contained in:
parent
1c5ce2d795
commit
ce9d4f2f0c
|
@ -1,54 +1,26 @@
|
||||||
use num::bigint::BigInt;
|
|
||||||
use std::convert::From;
|
use std::convert::From;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
Io(io::Error),
|
Preserves(preserves::Error),
|
||||||
Message(String),
|
Message(String),
|
||||||
InvalidUnicodeScalar(u32),
|
InvalidUnicodeScalar(u32),
|
||||||
NumberOutOfRange(BigInt),
|
|
||||||
CannotDeserializeAny,
|
CannotDeserializeAny,
|
||||||
MissingCloseDelimiter,
|
ExpectedOption,
|
||||||
MissingItem,
|
ExpectedUnicodeScalar,
|
||||||
Expected(ExpectedKind),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
impl From<preserves::Error> for Error {
|
||||||
pub enum ExpectedKind {
|
|
||||||
Boolean,
|
|
||||||
Float,
|
|
||||||
Double,
|
|
||||||
|
|
||||||
SignedIntegerI128,
|
|
||||||
SignedIntegerU128,
|
|
||||||
SignedInteger,
|
|
||||||
String,
|
|
||||||
ByteString,
|
|
||||||
Symbol,
|
|
||||||
|
|
||||||
Record,
|
|
||||||
SimpleRecord(String),
|
|
||||||
Sequence,
|
|
||||||
Set,
|
|
||||||
Dictionary,
|
|
||||||
|
|
||||||
Embedded,
|
|
||||||
|
|
||||||
Option,
|
|
||||||
UnicodeScalar,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<io::Error> for Error {
|
|
||||||
fn from(e: io::Error) -> Self {
|
fn from(e: io::Error) -> Self {
|
||||||
Error::Io(e)
|
Error::Preserves(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Error> for io::Error {
|
impl From<Error> for io::Error {
|
||||||
fn from(e: Error) -> Self {
|
fn from(e: Error) -> Self {
|
||||||
match e {
|
match e {
|
||||||
Error::Io(ioe) => ioe,
|
Error::Preserves(p) => p.into(),
|
||||||
Error::Message(str) => io::Error::new(io::ErrorKind::Other, str),
|
Error::Message(str) => io::Error::new(io::ErrorKind::Other, str),
|
||||||
_ => io::Error::new(io::ErrorKind::Other, e),
|
_ => io::Error::new(io::ErrorKind::Other, e),
|
||||||
}
|
}
|
||||||
|
@ -74,35 +46,3 @@ impl std::fmt::Display for Error {
|
||||||
write!(f, "{:?}", self)
|
write!(f, "{:?}", self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
pub fn is_io_error(e: &Error) -> bool {
|
|
||||||
matches!(e, Error::Io(_))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn eof() -> Error {
|
|
||||||
Error::Io(io_eof())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_eof_error(e: &Error) -> bool {
|
|
||||||
if let Error::Io(ioe) = e {
|
|
||||||
is_eof_io_error(ioe)
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
pub fn io_eof() -> io::Error {
|
|
||||||
io::Error::new(io::ErrorKind::UnexpectedEof, "EOF")
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_eof_io_error(e: &io::Error) -> bool {
|
|
||||||
matches!(e.kind(), io::ErrorKind::UnexpectedEof)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_syntax_io_error(e: &io::Error) -> bool {
|
|
||||||
matches!(e.kind(), io::ErrorKind::InvalidData)
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use preserves::{Value, IOValue, Map, ValueImpl};
|
use preserves::{Value, IOValue, Map, ValueImpl};
|
||||||
//use crate::error::Error;
|
use crate::error::Error;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde::de::{Visitor, SeqAccess, MapAccess, EnumAccess, VariantAccess, DeserializeSeed};
|
use serde::de::{Visitor, SeqAccess, MapAccess, EnumAccess, VariantAccess, DeserializeSeed};
|
||||||
use std::iter::Iterator;
|
use std::iter::Iterator;
|
||||||
|
@ -32,8 +32,8 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de>
|
||||||
let v = self.input.value();
|
let v = self.input.value();
|
||||||
match v {
|
match v {
|
||||||
Value::Boolean(b) => visitor.visit_bool(*b),
|
Value::Boolean(b) => visitor.visit_bool(*b),
|
||||||
Value::Float(Float(f)) => visitor.visit_f32(*f),
|
Value::Float(f) => visitor.visit_f32(*f),
|
||||||
Value::Double(Double(d)) => visitor.visit_f64(*d),
|
Value::Double(d) => visitor.visit_f64(*d),
|
||||||
Value::String(s) => visitor.visit_str(&s),
|
Value::String(s) => visitor.visit_str(&s),
|
||||||
Value::ByteString(_) => self.deserialize_bytes(visitor),
|
Value::ByteString(_) => self.deserialize_bytes(visitor),
|
||||||
Value::Record(_) =>
|
Value::Record(_) =>
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use preserves::IOValue;
|
||||||
|
|
||||||
pub static MAGIC: &str = "$____Preserves_Serde_Magic";
|
pub static MAGIC: &str = "$____Preserves_Serde_Magic";
|
||||||
|
|
||||||
struct IOValueVisitor;
|
struct IOValueVisitor;
|
||||||
|
@ -54,18 +56,6 @@ where F: FnOnce() -> Result<IOValue, crate::error::Error>
|
||||||
// This part is a terrible hack
|
// This part is a terrible hack
|
||||||
|
|
||||||
|
|
||||||
impl serde::Serialize for UnwrappedIOValue {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
|
|
||||||
super::magic::output_value(serializer, self.clone().wrap())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'de> serde::Deserialize<'de> for UnwrappedIOValue {
|
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
|
|
||||||
Ok(super::magic::input_value::<'de, D>(deserializer)?.value_owned())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl serde::Serialize for IOValue {
|
impl serde::Serialize for IOValue {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
|
||||||
super::magic::output_value(serializer, self.clone())
|
super::magic::output_value(serializer, self.clone())
|
||||||
|
|
Loading…
Reference in New Issue