2020-05-27 07:04:55 +00:00
|
|
|
use num::bigint::BigInt;
|
|
|
|
use std::convert::From;
|
|
|
|
use crate::value::IOValue;
|
|
|
|
|
2020-05-25 13:34:17 +00:00
|
|
|
#[derive(Debug)]
|
2020-05-27 07:04:55 +00:00
|
|
|
pub enum Error {
|
|
|
|
Io(std::io::Error),
|
|
|
|
Message(String),
|
|
|
|
InvalidUnicodeScalar(u32),
|
|
|
|
NumberOutOfRange(BigInt),
|
|
|
|
CannotDeserializeAny,
|
|
|
|
MissingCloseDelimiter,
|
|
|
|
MissingItem,
|
|
|
|
Expected(ExpectedKind, Received),
|
2020-05-25 13:34:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 07:04:55 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Received {
|
|
|
|
ReceivedSomethingElse,
|
|
|
|
ReceivedRecordWithLabel(String),
|
|
|
|
ReceivedOtherValue(IOValue),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum ExpectedKind {
|
|
|
|
Boolean,
|
|
|
|
Float,
|
|
|
|
Double,
|
|
|
|
|
|
|
|
SignedInteger,
|
|
|
|
String,
|
|
|
|
ByteString,
|
|
|
|
Symbol,
|
|
|
|
|
|
|
|
Record(Option<usize>),
|
|
|
|
SimpleRecord(&'static str, Option<usize>),
|
|
|
|
Sequence,
|
|
|
|
Set,
|
|
|
|
Dictionary,
|
|
|
|
|
|
|
|
SequenceOrSet, // Because of hacking up serde's data model: see open_sequence_or_set etc.
|
|
|
|
|
|
|
|
Option,
|
|
|
|
UnicodeScalar,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::io::Error> for Error {
|
|
|
|
fn from(e: std::io::Error) -> Self {
|
|
|
|
Error::Io(e)
|
2020-05-25 13:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 07:04:55 +00:00
|
|
|
impl From<Error> for std::io::Error {
|
|
|
|
fn from(e: Error) -> Self {
|
|
|
|
match e {
|
|
|
|
Error::Io(ioe) => ioe,
|
|
|
|
Error::Message(str) => std::io::Error::new(std::io::ErrorKind::Other, str),
|
|
|
|
_ => std::io::Error::new(std::io::ErrorKind::Other, e.to_string()),
|
|
|
|
}
|
2020-05-25 13:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl serde::ser::Error for Error {
|
|
|
|
fn custom<T: std::fmt::Display>(msg: T) -> Self {
|
2020-05-27 07:04:55 +00:00
|
|
|
Self::Message(msg.to_string())
|
2020-05-25 13:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 07:04:55 +00:00
|
|
|
impl serde::de::Error for Error {
|
|
|
|
fn custom<T: std::fmt::Display>(msg: T) -> Self {
|
|
|
|
Self::Message(msg.to_string())
|
2020-05-25 13:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for Error {}
|
2020-05-27 07:04:55 +00:00
|
|
|
|
|
|
|
impl std::fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
pub fn is_io_error(e: &Error) -> bool {
|
|
|
|
if let Error::Io(_) = e { true } else { false }
|
|
|
|
}
|
|
|
|
|
|
|
|
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 syntax_error(s: &str) -> Error {
|
|
|
|
Error::Io(io_syntax_error(s))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_syntax_error(e: &Error) -> bool {
|
|
|
|
if let Error::Io(ioe) = e {
|
|
|
|
is_syntax_io_error(ioe)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
pub fn io_eof() -> std::io::Error {
|
|
|
|
std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "EOF")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_eof_io_error(e: &std::io::Error) -> bool {
|
|
|
|
match e.kind() {
|
|
|
|
std::io::ErrorKind::UnexpectedEof => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn io_syntax_error(s: &str) -> std::io::Error {
|
|
|
|
std::io::Error::new(std::io::ErrorKind::InvalidData, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_syntax_io_error(e: &std::io::Error) -> bool {
|
|
|
|
match e.kind() {
|
|
|
|
std::io::ErrorKind::InvalidData => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|