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
use serde::de::{Deserialize, Deserializer, Error};
use crate::types::*;
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub enum ResponseWrapper<T> {
Success {
result: T,
},
Error {
description: String,
parameters: Option<ResponseParameters>,
},
}
impl<'de, T: Deserialize<'de>> Deserialize<'de> for ResponseWrapper<T> {
fn deserialize<D>(deserializer: D) -> Result<ResponseWrapper<T>, D::Error>
where
D: Deserializer<'de>,
{
let raw: RawResponse<T> = Deserialize::deserialize(deserializer)?;
match (raw.ok, raw.description, raw.result) {
(false, Some(description), None) => Ok(ResponseWrapper::Error {
description: description,
parameters: raw.parameters,
}),
(true, None, Some(result)) => Ok(ResponseWrapper::Success { result: result }),
_ => Err(D::Error::custom("ambiguous response")),
}
}
}
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
pub struct RawResponse<T> {
ok: bool,
description: Option<String>,
result: Option<T>,
parameters: Option<ResponseParameters>,
}
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
pub struct ResponseParameters {
pub migrate_to_chat_id: Option<Integer>,
pub retry_after: Option<Integer>,
}