1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use crate::errors::{ProtocolError, Result};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto};
use std::fmt::Display;
pub fn json_to_type_or_error<T: DeserializeOwned>(
response: serde_json::Value,
) -> Result<ResponseOrError<T>> {
let string_val = serde_json::to_string(&response)
.map_err(|_| ProtocolError("Unexpected problem serializing JSON to string"))?;
Ok(serde_json::from_str(&string_val)
.map_err(|_| ProtocolError("Could not convert JSON to T"))?)
}
pub fn serializable_to_json<T: Serialize>(obj: &T) -> Result<serde_json::Value> {
let str_val = serde_json::to_string(obj)
.map_err(|_| ProtocolError("Unexpected problem serializing T to string"))?;
Ok(serde_json::from_str(&str_val)
.expect("Transforming to JSON failed. This should never happen"))
}
pub fn try_response_from_json<A, B>(response: serde_json::Value) -> Result<ResponseOrError<A>>
where
A: TryFrom<B>,
<A as TryFrom<B>>::Error: Display,
B: DeserializeOwned,
{
let parse_graphql = json_to_type_or_error::<B>(response)?;
let mapped = parse_graphql.map(Box::new(|data| data.try_into()));
match mapped {
ResponseOrError::Response(DataResponse { data }) => match data {
Ok(data) => Ok(ResponseOrError::from_data(data)),
Err(err) => Err(ProtocolError::coerce_static_from_str(&format!("{}", err))),
},
ResponseOrError::Error(e) => Ok(ResponseOrError::Error(e)),
}
}
pub enum ErrorOrData<T> {
Data(T),
Error(ErrorResponse),
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(untagged)]
pub enum ResponseOrError<T> {
Response(DataResponse<T>),
Error(ErrorResponse),
}
impl<T> ResponseOrError<T> {
pub fn from_data(data: T) -> Self {
Self::Response(DataResponse { data })
}
pub fn consume_match(self) -> ErrorOrData<T> {
match self {
Self::Response(DataResponse { data }) => ErrorOrData::Data(data),
Self::Error(e) => ErrorOrData::Error(e),
}
}
pub fn response(&self) -> Option<&T> {
match self {
Self::Response(DataResponse { data }) => Some(data),
Self::Error(_) => None,
}
}
pub fn consume_response(self) -> Option<T> {
match self {
Self::Response(DataResponse { data }) => Some(data),
Self::Error(_) => None,
}
}
pub fn consume_error(self) -> Option<ErrorResponse> {
match self {
Self::Response(_) => None,
Self::Error(e) => Some(e),
}
}
pub fn response_or_error(self) -> Result<T> {
match self {
Self::Response(DataResponse { data}) => Ok(data),
Self::Error(e) => Err(ProtocolError::coerce_static_from_str(&format!("{:?}", e)))
}
}
pub fn error(&self) -> Option<&ErrorResponse> {
match self {
Self::Response(_) => None,
Self::Error(error) => Some(error),
}
}
pub fn is_error(&self) -> bool {
match self {
Self::Error(_) => true,
Self::Response(_) => false,
}
}
pub fn map<M>(self, f: Box<dyn Fn(T) -> M>) -> ResponseOrError<M> {
match self {
Self::Error(e) => ResponseOrError::Error(e),
Self::Response(r) => ResponseOrError::Response(r.map(f)),
}
}
}
#[derive(Deserialize, Serialize, Debug)]
pub struct DataResponse<T> {
pub data: T,
}
impl<T> DataResponse<T> {
fn map<M>(self, f: Box<dyn Fn(T) -> M>) -> DataResponse<M> {
DataResponse { data: f(self.data) }
}
}
#[derive(Deserialize, Serialize, Debug)]
pub struct ErrorResponse {
pub errors: Vec<Error>,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct Error {
pub message: String,
}
#[derive(Clone, Debug)]
pub struct RequestPayloadSignature {
pub signed_digest: String,
pub public_key: String,
}
impl RequestPayloadSignature {
pub fn empty() -> Self {
Self {
signed_digest: "".to_string(),
public_key: "".to_string(),
}
}
}