inth_oauth2_async/client/
response.rs1use std::error::Error;
4use std::fmt;
5
6use serde_json::Value;
7
8pub trait FromResponse: Sized {
10 fn from_response(json: &Value) -> Result<Self, ParseError>;
12
13 #[allow(unused_variables)]
18 fn from_response_inherit(json: &Value, prev: &Self) -> Result<Self, ParseError> {
19 FromResponse::from_response(json)
20 }
21}
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub enum ParseError {
26 ExpectedType(&'static str),
28
29 ExpectedFieldType(&'static str, &'static str),
31
32 ExpectedFieldValue(&'static str, &'static str),
34
35 UnexpectedField(&'static str),
37}
38
39impl fmt::Display for ParseError {
40 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
41 match *self {
42 ParseError::ExpectedType(t) =>
43 write!(f, "Expected response of type {}", t),
44 ParseError::ExpectedFieldType(k, t) =>
45 write!(f, "Expected field {} of type {}", k, t),
46 ParseError::ExpectedFieldValue(k, v) =>
47 write!(f, "Expected field {} to equal {}", k, v),
48 ParseError::UnexpectedField(k) =>
49 write!(f, "Unexpected field {}", k),
50 }
51 }
52}
53
54impl Error for ParseError {
55 fn description(&self) -> &str { "response parse error" }
56}