pub use crate::parser::common::ParseError;
use crate::{highlevel, lowlevel};
use std::io;
use thiserror::Error;
#[derive(Clone, Debug, Error, PartialEq)]
pub enum DeserializeErrorCause {
#[error("IO error: {0:?}")]
IoError(String),
#[error("Parse error: {0:?}")]
ParseError(String),
#[error("Bad UTF8: {0:?}")]
Utf8Error(std::string::FromUtf8Error),
#[error("Parsed unknown command: {0:?}")]
UnknownCommand(lowlevel::Command),
#[error("Parsed unknown response: {0:?}")]
UnknownResponse(lowlevel::Response),
#[error("Parsed arguments do not match argument definition: {0:?}")]
UnknownArguments(lowlevel::Arguments),
}
impl From<io::Error> for DeserializeErrorCause {
fn from(io_error: io::Error) -> DeserializeErrorCause {
let string = format!("{:?}", io_error);
DeserializeErrorCause::IoError(string)
}
}
impl<RT: pest::RuleType> From<ParseError<RT>> for DeserializeErrorCause {
fn from(parse_error: ParseError<RT>) -> DeserializeErrorCause {
let string = format!("{:?}", parse_error);
DeserializeErrorCause::ParseError(string)
}
}
impl From<std::string::FromUtf8Error> for DeserializeErrorCause {
fn from(utf8_error: std::string::FromUtf8Error) -> DeserializeErrorCause {
DeserializeErrorCause::Utf8Error(utf8_error)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct DeserializeError {
pub cause: DeserializeErrorCause,
pub bytes: Vec<u8>,
}
#[derive(Debug, Error)]
pub enum SerializeErrorCause {
#[error("IO error: {0:?}")]
IoError(io::Error),
}
impl From<io::Error> for SerializeErrorCause {
fn from(io_error: io::Error) -> SerializeErrorCause {
SerializeErrorCause::IoError(io_error)
}
}
pub(crate) mod internal {
use super::{DeserializeError, DeserializeErrorCause, ParseError, SerializeErrorCause};
use crate::lowlevel::write_to::WriteTo;
use crate::parser::{command_grammar, command_parser, response_grammar, response_parser};
use crate::{highlevel, lowlevel, translate};
use std::io;
pub trait SerDeMethods: Sized {
type Lowlevel: WriteTo;
type Rule: pest::RuleType;
fn parse(string: &String) -> Result<Self::Lowlevel, ParseError<Self::Rule>>;
fn raise(lowlevel: &Self::Lowlevel) -> Result<Self, DeserializeErrorCause>;
fn lower(&self) -> Self::Lowlevel;
fn write_to<W: io::Write>(sink: &mut W, lowlevel: &Self::Lowlevel) -> io::Result<()> {
lowlevel.write_to(sink)
}
}
impl SerDeMethods for highlevel::Command {
type Lowlevel = lowlevel::Command;
type Rule = command_grammar::Rule;
fn parse(string: &String) -> Result<lowlevel::Command, ParseError<Self::Rule>> {
command_parser::parse(string)
}
fn raise(lowlevel: &Self::Lowlevel) -> Result<Self, DeserializeErrorCause> {
translate::raise_command(lowlevel)
}
fn lower(&self) -> Self::Lowlevel {
translate::lower_command(self)
}
}
impl SerDeMethods for highlevel::Response {
type Lowlevel = lowlevel::Response;
type Rule = response_grammar::Rule;
fn parse(string: &String) -> Result<lowlevel::Response, ParseError<Self::Rule>> {
response_parser::parse(string)
}
fn raise(lowlevel: &Self::Lowlevel) -> Result<Self, DeserializeErrorCause> {
translate::raise_response(lowlevel)
}
fn lower(&self) -> Self::Lowlevel {
translate::lower_response(self)
}
}
pub trait SerDeOne: Sized {
fn deserialize_one<R: io::Read>(source: &mut R) -> Result<Self, DeserializeError>;
fn serialize_one<W: io::Write>(&self, sink: &mut W) -> Result<(), SerializeErrorCause>;
}
impl<T: SerDeMethods> SerDeOne for T {
fn deserialize_one<R: io::Read>(source: &mut R) -> Result<Self, DeserializeError> {
let mut bytes: Vec<u8> = Vec::new();
let mut string = String::new();
let read_result = source.read_to_end(&mut bytes).map_err(|err| DeserializeError {
cause: DeserializeErrorCause::from(err),
bytes: bytes.clone(),
});
let string_result = read_result.and_then(|_byte_count| {
String::from_utf8(bytes).map_err(|err| {
let bytes = Vec::from(err.as_bytes());
DeserializeError { cause: DeserializeErrorCause::from(err), bytes }
})
});
let lowlevel_result = string_result.and_then(|s| {
string = s;
Self::parse(&string).map_err(|err| DeserializeError {
cause: DeserializeErrorCause::from(err),
bytes: string.clone().into_bytes(),
})
});
let highlevel_result = lowlevel_result.and_then(|lowlevel| {
Self::raise(&lowlevel).map_err(|err| DeserializeError {
cause: DeserializeErrorCause::from(err),
bytes: string.into_bytes(),
})
});
highlevel_result
}
fn serialize_one<W: io::Write>(&self, sink: &mut W) -> Result<(), SerializeErrorCause> {
let lowlevel = Self::lower(self);
Self::write_to(sink, &lowlevel)?;
Ok(())
}
}
} #[derive(Debug)]
pub struct SerializeError<T> {
pub remaining: Vec<T>,
pub failed: T,
pub cause: SerializeErrorCause,
}
#[derive(Debug, PartialEq)]
pub struct DeserializeBytes {
pub(crate) bytes: Vec<u8>,
}
impl DeserializeBytes {
fn add_bytes<R: io::Read>(&mut self, source: &mut R) -> Result<(), DeserializeError> {
let mut more_bytes = Vec::new();
let byte_count_result = source.read_to_end(&mut more_bytes);
self.bytes.append(&mut more_bytes);
match byte_count_result {
Ok(_byte_count) => Ok(()),
Err(err) => Err(DeserializeError { cause: err.into(), bytes: more_bytes }),
}
}
fn from(bytes: &[u8]) -> Self {
DeserializeBytes { bytes: bytes.into() }
}
pub fn new() -> Self {
DeserializeBytes { bytes: Vec::new() }
}
}
impl Default for DeserializeBytes {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, PartialEq)]
pub struct DeserializeResult<T> {
pub values: Vec<T>,
pub error: Option<DeserializeError>,
pub remaining_bytes: DeserializeBytes,
}
pub trait SerDe: Sized {
fn serialize<W: io::Write>(sink: &mut W, values: &[Self]) -> Result<(), SerializeError<Self>>;
fn deserialize<R: io::Read>(
source: &mut R,
existing_bytes: DeserializeBytes,
) -> DeserializeResult<Self>;
}
impl<T: internal::SerDeOne + Clone> SerDe for T {
fn serialize<W: io::Write>(sink: &mut W, values: &[Self]) -> Result<(), SerializeError<Self>> {
let mut iter = values.iter();
for value in &mut iter {
match value.serialize_one(sink) {
Ok(()) => (),
Err(cause) => {
return Err(SerializeError {
remaining: iter.cloned().collect(),
failed: value.clone(),
cause,
})
}
}
}
Ok(())
}
fn deserialize<R: io::Read>(
source: &mut R,
mut existing_bytes: DeserializeBytes,
) -> DeserializeResult<T> {
let mut values = Vec::new();
if let Err(error) = existing_bytes.add_bytes(source) {
return DeserializeResult {
values,
error: Some(error),
remaining_bytes: existing_bytes,
};
}
let mut beginning = 0;
let mut end = 0;
let bytes = &existing_bytes.bytes;
let len = bytes.len();
let should_split = |b| b == &b'\n' || b == &b'\r';
let is_not_whitespace = |b| !should_split(b);
loop {
end += 1;
if end >= len {
break;
};
if !(should_split(&bytes[end])) {
continue;
}
let mut slice = &bytes[beginning..end];
if slice.is_empty() || slice.iter().position(is_not_whitespace).is_none() {
beginning = end;
continue;
}
let value_result = T::deserialize_one(&mut slice);
match value_result {
Ok(value) => values.push(value),
Err(error) => {
return DeserializeResult {
values,
error: Some(error),
remaining_bytes: DeserializeBytes::from(&bytes[end..]),
};
}
};
beginning = end;
}
DeserializeResult {
values,
error: None,
remaining_bytes: DeserializeBytes::from(&bytes[beginning..]),
}
}
}
pub fn success(success: highlevel::Success) -> highlevel::Response {
highlevel::Response::Success(success)
}