use std::error::Error;
use std::fmt;
use serde_json::Value;
pub trait FromResponse: Sized {
fn from_response(json: &Value) -> Result<Self, ParseError>;
#[allow(unused_variables)]
fn from_response_inherit(json: &Value, prev: &Self) -> Result<Self, ParseError> {
FromResponse::from_response(json)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParseError {
ExpectedType(&'static str),
ExpectedFieldType(&'static str, &'static str),
ExpectedFieldValue(&'static str, &'static str),
UnexpectedField(&'static str),
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
ParseError::ExpectedType(t) =>
write!(f, "Expected response of type {}", t),
ParseError::ExpectedFieldType(k, t) =>
write!(f, "Expected field {} of type {}", k, t),
ParseError::ExpectedFieldValue(k, v) =>
write!(f, "Expected field {} to equal {}", k, v),
ParseError::UnexpectedField(k) =>
write!(f, "Unexpected field {}", k),
}
}
}
impl Error for ParseError {
fn description(&self) -> &str { "response parse error" }
}