use std;
use std::fmt;
use std::fmt::Display;
use std::io;
use std::num;
use std::string;
use serde::{de, ser};
pub type ResultE<T> = Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Message(String),
UnsupportedType,
BadFormat,
BadPadding,
Io(io::Error),
BadCast(num::TryFromIntError),
StrParseError(string::FromUtf8Error),
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}
impl From<num::TryFromIntError> for Error {
fn from(e: num::TryFromIntError) -> Self {
Error::BadCast(e)
}
}
impl From<string::FromUtf8Error> for Error {
fn from(e: string::FromUtf8Error) -> Self {
Error::StrParseError(e)
}
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Message(ref msg) => write!(f, "serde_osc error: {}", msg),
Error::UnsupportedType => write!(f, "Unsupported OSC type"),
Error::BadFormat => write!(f, "Bad OSC packet format"),
Error::BadPadding => write!(f, "OSC data not padded to 4-byte boundary"),
Error::Io(ref err) => err.fmt(f),
Error::BadCast(ref err) => err.fmt(f),
Error::StrParseError(_) => write!(f, "OSC string contains illegal (non-ascii) characters"),
}
}
}
impl std::error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::Message(ref msg) => msg,
Error::UnsupportedType => "Unsupported OSC type",
Error::BadFormat => "OSC argument count mismatch",
Error::BadPadding => "Incorrect OSC data padding",
Error::Io(ref io_error) => io_error.description(),
Error::BadCast(ref cast_error) => cast_error.description(),
Error::StrParseError(ref utf_error) => utf_error.description(),
}
}
fn cause(&self) -> Option<&std::error::Error> {
match *self {
Error::Io(ref io_error) => Some(io_error),
Error::BadCast(ref cast_error) => Some(cast_error),
Error::StrParseError(ref utf_error) => Some(utf_error),
_ => None,
}
}
}
impl de::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::Message(msg.to_string())
}
}
impl ser::Error for Error {
fn custom<T: Display>(msg: T) -> Self {
Error::Message(msg.to_string())
}
}