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::*;

/// All API responses are from this type. Mostly used internal.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub enum ResponseWrapper<T> {
    /// Request was successful.
    Success {
        /// Response result.
        result: T,
    },
    /// Request was unsuccessful.
    Error {
        /// Human-readable description of the result.
        description: String,
        /// Contains information about why a request was unsuccessful.
        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")),
        }
    }
}

/// Directly mapped telegram API response.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
pub struct RawResponse<T> {
    /// If ‘ok’ equals true, the request was successful.
    ok: bool,
    /// Human-readable description of the result.
    description: Option<String>,
    /// Result of the query.
    result: Option<T>,
    /// Information about why a request was unsuccessful.
    parameters: Option<ResponseParameters>,
}

/// Contains information about why a request was unsuccessful.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Deserialize)]
pub struct ResponseParameters {
    /// The group has been migrated to a supergroup with the specified identifier.
    pub migrate_to_chat_id: Option<Integer>,
    /// In case of exceeding flood control, the number of seconds left to wait
    /// before the request can be repeated.
    pub retry_after: Option<Integer>,
}