You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.4 KiB
85 lines
1.4 KiB
3 years ago
|
//! Processing
|
||
|
use super::*;
|
||
|
use std::io;
|
||
|
use std::{
|
||
|
fmt, error
|
||
|
};
|
||
|
|
||
|
macro_rules! processor {
|
||
|
($name:ident: {$($body:tt)*}) => {
|
||
|
#[derive(Debug)]
|
||
|
pub struct $name;
|
||
|
|
||
|
impl Processor for $name{$($body)*}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
pub trait Processor
|
||
|
{
|
||
|
//TODO: How to design this trait??? eh...
|
||
|
fn process_stream(&self, input: &mut dyn serde::Serialize, output: &mut dyn io::Write) -> Result<usize, Error>;
|
||
|
}
|
||
|
|
||
|
processor!(Json: {
|
||
|
|
||
|
});
|
||
|
processor!(Sexpr: {
|
||
|
|
||
|
});
|
||
|
processor!(Cbor: {
|
||
|
|
||
|
});
|
||
|
|
||
|
#[derive(Debug)]
|
||
|
enum ErrorKind
|
||
|
{
|
||
|
IO(io::Error),
|
||
|
Unknown,
|
||
|
}
|
||
|
|
||
|
/// Processing error
|
||
|
pub struct Error(ErrorKind, Option<Box<dyn fmt::Display + 'static>>);
|
||
|
|
||
|
impl fmt::Debug for Error
|
||
|
{
|
||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||
|
{
|
||
|
f.debug_struct("Error")
|
||
|
.field("kind", &self.0)
|
||
|
.field("message_obj", &format_args!("{:?}", self.1.as_ref().map(|x| x.as_ref() as *const _)))
|
||
|
.finish()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
impl From<io::Error> for Error
|
||
|
{
|
||
|
#[inline] fn from(from: io::Error) -> Self
|
||
|
{
|
||
|
Self(ErrorKind::IO(from), None)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl error::Error for Error
|
||
|
{
|
||
|
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
||
|
Some(match &self.0 {
|
||
|
ErrorKind::IO(io) => io,
|
||
|
_ => return None,
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl fmt::Display for Error
|
||
|
{
|
||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
||
|
{
|
||
|
write!(f, "format processing error")?;
|
||
|
if let Some(msg) = self.1.as_ref() {
|
||
|
write!(f, ": {}", msg)
|
||
|
} else {
|
||
|
Ok(())
|
||
|
}
|
||
|
}
|
||
|
}
|